另一个时区的日期:Linux上的C.

另一个时区的日期:Linux上的C.,第1张

概述有没有办法得到日期…最好是YYYYMMDD格式…在澳大利亚/悉尼时区(不仅仅是GMT 11)…..在 Linux上通过C? 谢谢, 罗杰 是的,但您只需使用标准的c库机制. 通过创建字符串在环境中设置所需的时区: std::string tz = "TZ=Australia/Sydney";putenv(const_cast<char *>(tz.c_str()));tzset(); // I 有没有办法得到日期…最好是YYYYMMDD格式…在澳大利亚/悉尼时区(不仅仅是GMT 11)…..在 Linux上通过C?

谢谢,

罗杰

解决方法 是的,但您只需使用标准的c库机制.

通过创建字符串在环境中设置所需的时区:

std::string tz = "TZ=Australia/Sydney";putenv(const_cast<char *>(tz.c_str()));tzset(); // Initialize timezone datatime_t aTime = time(NulL); // get the time - this is GMT based.struct tm retTime;localtime_r(aTime,&retTime); // Convert time into current timezone.char destString[1024];strftime(destString,1023,"%Y%m%d %Z",&retTime); // Format the output in the local time.std::cout << destString << std::endl;

问题是这段代码不是线程安全的 – 更改时区信息的多个线程并不能很好地结束.

This Answer为您提供了一种使用boost的方法,这绝对容易.

总结

以上是内存溢出为你收集整理的另一个时区的日期:Linux上的C.全部内容,希望文章能够帮你解决另一个时区的日期:Linux上的C.所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1025884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存