另一个时区的日期:Linux上的C.
发布时间:2020-12-14 01:03:06 所属栏目:Linux 来源:网络整理
导读:有没有办法得到日期…最好是YYYYMMDD格式…在澳大利亚/悉尼时区(不仅仅是GMT 11)…..在 Linux上通过C? 谢谢, 罗杰 解决方法 是的,但您只需使用标准的c库机制. 通过创建字符串在环境中设置所需的时区: std::string tz = "TZ=Australia/Sydney";putenv(const
有没有办法得到日期…最好是YYYYMMDD格式…在澳大利亚/悉尼时区(不仅仅是GMT 11)…..在
Linux上通过C?
谢谢, 罗杰 解决方法
是的,但您只需使用标准的c库机制.
通过创建字符串在环境中设置所需的时区: std::string tz = "TZ=Australia/Sydney"; putenv(const_cast<char *>(tz.c_str())); tzset(); // Initialize timezone data time_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的方法,这绝对容易. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |