我有一段优化的功能来获得GMT时间。我想把它转换成当地时间。我想只调用一次localtime和gmtime函数来调整本地时间的时间,因为多次调用localtime和gmtime会破坏使用优化函数的目的。我的想法是将时区差异添加到我获得的GMT时间。但是,我的问题是如何在夏令时调整我的本地时间?关于检查的任何想法?
感谢。
答案 0 :(得分:1)
您可以在大多数Linux发行版中使用大部分时间存储在/ usr / share / lib / zoneinfo中的TZ数据库。这个数据库管理夏令时,所以你不需要处理它。
答案 1 :(得分:0)
您可以直接使用时区数据库,但也许您不想介绍其他组件。
如果我按照你的想法,我会提前每周或每天存储时差,并在以后使用。这有点脏,因为你将失去DST开关的确切时间。为了获得最佳精度,您理论上可以在本地时间使用二进制搜索,但与直接使用时区数据库相比,这看起来有点过分,而您在此处到达的实际上是您所在时区的时区区域数据库条目。
答案 2 :(得分:0)
我的应用程序需要获取本地标准时间(在本地时区但未针对夏令时进行调整)。我不确定这是不是你想要的,但我相信问题是类似的。这就是我自己完成的方法。我知道它可以在windows和linux之间移植:
LgrDate rtn = local();
int8 adjustment = 0;;
#ifdef _WIN32
TIME_ZONE_INFORMATION zone_info;
uint4 rcd;
rcd = GetTimeZoneInformation(&zone_info);
if(rcd == TIME_ZONE_ID_DAYLIGHT)
adjustment = zone_info.DaylightBias*nsecPerMin;
#else
// there is no portable means of obtaining the daylight savings time bias directly. We can,
// however, obtain the local time, and, if daylight savings time is enabled, we can use mktime
// to find out what the bias would be.
struct timeval time_of_day;
struct tm local_tm;
gettimeofday(&time_of_day,0);
localtime_r(
&time_of_day.tv_sec,
&local_tm);
if(local_tm.tm_isdst > 0)
{
local_tm.tm_isdst = 0;
adjustment = (mktime(&local_tm) - time_of_day.tv_sec) * nsecPerSec * -1;
}
#endif
return rtn + adjustment;
答案 3 :(得分:0)
您可以调用tzset()然后检查_daylight值。