-1

I have a trouble about get the UTC time. With the test code, I found that both gmtime and localtime would return the same result

void testTimeLib()
{
    struct tm  utcTime, localTime;
    time_t now;
    char utcStr[80], localStr[80];

    time(&now);
    gmtime_r(&now, &utcTime);
    localtime_r(&now, &localTime);

    memset(utcStr, 0, sizeof(utcStr));
    strftime(utcStr, sizeof(utcStr), "%Y-%m-%dT%H:%M:%S", &utcTime);

    memset(localStr, 0, sizeof(localStr));
    strftime(localStr, sizeof(localStr), "%Y-%m-%dT%H:%M:%S", &localTime);

    printf("UTC Time\tutcTime <%s>\n", utcStr);
    printf("Local Time\tlocalTime <%s>\n", localStr);
}

the result is

-> testTimeLib
UTC Time    utcTime <2014-12-19T10:57:33>
Local Time  localTime <2014-12-19T10:57:33>

By checking the source code of gmtime_r and localtime_r, I found that __getZoneInfo called by localtime_r would return 0 which means the timezone is 0, actually the timezone should be GMT+8.

So my question is how I can set the timezone to system?

Rgls
  • 1
  • 3

1 Answers1

1

The difference between the gmtime and localtime function is that, gmtime function will convert the calendar time into local time. but the localtime function will convert the calendar time into broken-down time which is expressed as UTC.

You can use the hwclock command to set the time zone. only the super user has the permission to execute this command. login as a super user and execute this command.

sharon
  • 734
  • 6
  • 15
  • 1
    Thanks, the OS is vxworks so there is no such command, but I find that the timezone is set with putenv and now the problem is resovled – Rgls Dec 19 '14 at 05:42
  • @Rgls if you are using linux, you can use the `hwclock` command – sharon Dec 19 '14 at 05:56