简体   繁体   English

在Windows中手动更改UTC时区时,time函数将返回之前时区值的相同值(即time_t secs)

[英]when UTC time zone is changed manually in windows time function returns the same value( i.e., time_t secs ) of the before time zone value

Step:1 Set the time zone to (UTC-08:00) Pacific Time (US & Canada) in windows. 步骤:1在Windows中将时区设置为(UTC-08:00)太平洋时间(美国和加拿大)。

Step:2 Checked the value of time(&secs) using a C Program, I got secs = 1386043600 . 步骤:2使用C程序检查time(&secs)的值,我得到secs = 1386043600

Step:3 Changed the time zone to (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi manually. 步骤:3手动将时区更改为(UTC + 05:30)钦奈,加尔各答,孟买,新德里。

Step:4 Checked the value of time(&secs). 步骤:4检查时间值(秒)。 Again I got the same value, secs = 1386043600 . 同样,我得到了相同的值, secs = 1386043600

After this I have rebooted the machine and again checked the value of time(&secs) . 之后,我重新启动了机器,并再次检查time(&secs)的值。 Now the value has changed to secs = 1386046505. Does this value gets effected only after rebooting the system? 现在,该值已更改为secs =1386046505。此值仅在重新引导系统后才生效吗?

This is expected behavior. 这是预期的行为。 A time_t value is the number of seconds since midnight (GMT) on 1 January 1970 — as if every computer had a counter counting up by 1 every second from then, and the value is what time() returns. time_t值是自1970年1月1日午夜(GMT)以来的秒数,就好像每台计算机从那时起每秒都有一个计数器加1一样,该值就是time()返回的值。 It is intentionally independent of the time zone your computer is set to use. 有意独立于计算机要使用的时区。

Trying running this sample code 尝试运行此示例代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>

int main(int argc, char* argv[])
{
    time_t t,prev_t;
    int index=1;
    SYSTEMTIME st, lt;
    printf("\n\tSetting the Time Zone to \"Central Standard Time\"");
    system("RunDLL32.exe shell32.dll,Control_RunDLL timedate.cpl,,/Z Central Standard Time");

    prev_t = time(NULL) ;
    printf("\nStarting Timetick %d", prev_t);
    while (index<=9) 
     {

        if( index==5)
        {
            printf("\n\tSetting the Time Zone to \"Eastern Standard Time\"");
            system("RunDLL32.exe shell32.dll,Control_RunDLL timedate.cpl,,/Z Eastern Standard Time");
        }
        memset(&st,'\0',sizeof(st));
        memset(&lt,'\0',sizeof(lt));

        Sleep(1000);
        t=time(NULL);
        GetSystemTime(&st); /* UTC */
        GetLocalTime(&lt);  /* local */

        printf("\n[%d] system(utc): %02d:%02d:%02d, local: %02d:%02d:%02d, time()=%ld (diff=%ld)", index,st.wHour, st.wMinute,st.wSecond,lt.wHour,lt.wMinute,lt.wSecond,t, t- prev_t);

        prev_t=t;
        index++;

    }
}

1) At startup, sample sets the current Time Zone to "Central Standard Time". 1)在启动时,示例将当前时区设置为“中央标准时间”。 And, also prints the current time tick as returned by time(). 并且,还打印time()返回的当前时间刻度。

2) Then it executes a loop 9 times. 2)然后执行9次循环。 Every iteration of the loop finds UTC time, localtime and time ticks using function GetSystemTime(), GetLocalTime() and time() respectively. 循环的每个迭代都分别使用函数GetSystemTime(),GetLocalTime()和time()查找UTC时间,本地时间和时间滴答。 These values are printed along with the difference in the previous time tick. 这些值与前一时间刻度的差值一起打印。

3) At 5th iteration, the Time Zone is changed to "Eastern Standard Time". 3)在第5次迭代中,时区更改为“东部标准时间”。

4) Every iteration is executed with a time gap of one second, using function Sleep(). 4)使用函数Sleep(),以一秒的时间间隔执行每次迭代。

5) The Time Zone change in the sample is achieved with the function system(). 5)样本中的时区变化通过system()函数实现。

This out put of the code 代码的输出

Setting the Time Zone to "Central Standard Time"
Starting Timetick 1385974984
[1] system(utc): 09:03:05, local: 14:33:05, time()=1385974985 (diff=1)
[2] system(utc): 09:03:06, local: 14:33:06, time()=1385974986 (diff=1)
[3] system(utc): 09:03:07, local: 14:33:07, time()=1385974987 (diff=1)
[4] system(utc): 09:03:08, local: 14:33:08, time()=1385974988 (diff=1)
        Setting the Time Zone to "Eastern Standard Time"
[5] system(utc): 09:03:12, local: 14:33:12, time()=1385974992 (diff=4)
[6] system(utc): 09:03:13, local: 14:33:13, time()=1385974993 (diff=1)
[7] system(utc): 09:03:14, local: 14:33:14, time()=1385974994 (diff=1)
[8] system(utc): 09:03:15, local: 14:33:15, time()=1385974995 (diff=1)
[9] system(utc): 09:03:16, local: 14:33:16, time()=1385974996 (diff=1)
Process returned 1385974996 (0x529C4CD4)   execution time : 13.397 s
Press any key to continue.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM