Tag: mktime

mktime显示输出不一致

在尝试编写比给定时间少24小时的代码时, mktime()显示输出不一致。 我计算它类似于: current_time(GMT) – 86400 ,它应该返回正确的值。 我们需要做的就是根据输入时间计算; 我们使用mktime()来改变时间并获得GMT时间,然后进行常规计算。 我在下面提供了我的代码。 #include #include int main() { time_t currentTime, tempTime; struct tm *localTime; time(&currentTime); //localTime = localtime(&currentTime); localTime = gmtime(&currentTime); //get the time in GMT as we are in PDT printf(“Time %2d:%02d\n”, (localTime->tm_hour)%24, localTime->tm_min); localTime->tm_hour = 19; // Set the time to 19:00 GMT localTime->tm_min = 0; […]

我使用tm / mktime是错误的,如果没有,是否有解决方法?

我认为以下程序应该从每年的第一天输出从1970年到1970年的秒数,然后是它编译的系统上的time_t的大小( CHAR_BIT是一个宏,所以我认为你不能只复制编译可执行文件并假设它是正确的虽然在实践中一切都使用8位char这些天)。 #include #include #include #include #include void do_time(int year) { time_t utc; struct tm tp; memset(&tp, 0, sizeof(tp)); tp.tm_sec = 0; tp.tm_min = 0; tp.tm_hour = 0; tp.tm_mday = 1; tp.tm_mon = 0; tp.tm_year = year – 1900; tp.tm_wday = 1; tp.tm_yday = 0; tp.tm_isdst = -1; printf(“%d %ld\n”,year, mktime(&tp)); } int main(){ printf(“time_t […]

奇怪的mktime逻辑与负秒

我一直在使用mktime / localtime进行时间管理,包括对日期/时间的一些重要算术。 当向mktime提供包含负值的struct tm时,我注意到一些非常奇怪的东西。 请参考下面的代码。 在2013年11月3日,洛杉矶有一个DST更改。如果我指定时间为2013-11-04午夜时间并减去24小时,我得到的值与2013-11-03午夜相同。 它与UTC相差25小时,这很好,因为isdst = -1可以说我们正在看’挂钟时间’。 如果我减去1440分钟(24 * 60)则相同。 但是,如果我减去86400(24 * 60 * 60)秒,我得到2013-11-03凌晨1点。 这是UTC方式的24小时差异。 这是以下代码的输出: 2013-11-03 00:00:00 (gmtoff=0, isdst=-1) -> 2013-11-03 00:00:00 (gmtoff=-25200, isdst=1) -> 1383462000 2013-12–27 00:00:00 (gmtoff=0, isdst=-1) -> 2013-11-03 00:00:00 (gmtoff=-25200, isdst=1) -> 1383462000 2013-11-04 -24:00:00 (gmtoff=0, isdst=-1) -> 2013-11-03 00:00:00 (gmtoff=-25200, isdst=1) -> 1383462000 2013-11-04 00:-1440:00 […]

mktime返回错误的时间戳(关闭整整一个月)

我使用mktime从当前本地时间创建一个unix时间戳: #include int _tmain(int argc, _TCHAR* argv[]) { struct tm info; // 16.05.2014 info.tm_mday = 16; info.tm_mon = 5; info.tm_year = 114; // Years since 1900 // 08:00:00 Uhr info.tm_hour = 8; info.tm_min = 0; info.tm_sec = 0; // Convert to Unix timestamp info.tm_isdst = -1; time_t timestamp = mktime(&info); printf(“Timestamp: %i”, timestamp); } 这给了我: […]