0

I am trying to get current time in C using time_t current_time = time(NULL). As I understand, it would return me the current time of system. I am later trying to convert it into GMT time using struct tm* gmt = gmtime(&current_time).

I print both times using ctime() and asctime() functions.

The current time on my system is GMT + 1. But gmtime() returns me the same time as current_time is. I could not understand why gmtime() is returning me same time. Any help will be appreciated.

Ok here is the code and the output: Current time that windows is showing is 17:54 (Stockholm zone; GMT+1). I want something to return me 15:54. Or perhaps my understanding is wrong ...

time_t current_time = time(NULL);

struct tm* gmt = gmtime(&current_time);
struct tm* loc = localtime(&current_time);

printf("current time: %s\n", ctime(&current_time));
printf("gmt time %s\n", asctime(gmt));
printf("local time %s\n", asctime(loc));

Output:

current time: Mon Oct  8 17:54:06 2012

gmt time Mon Oct  8 17:54:06 2012

local time Mon Oct  8 17:54:06 2012

Accepted Solution: From Simes

That's probably your problem. Check the value of your TZ environment variable; if not present, it will default to GMT. Cygwin doesn't automatically pick up the time zone setting from Windows. See also localtime returns GMT for windows programs running on cygwin shells

Community
  • 1
  • 1
ata
  • 8,853
  • 8
  • 42
  • 68

3 Answers3

1

time() returns the number of seconds since epoch. Which is equal to UTC (aka GMT)

Epoch was 1.1.1970, 00:00:00 in Greenwich, UK.

So in fact time() does not return a time, but a time difference.

alk
  • 69,737
  • 10
  • 105
  • 255
  • UTC is not aka GMT, they are 2 different things ,even they are almost same value, but not necessary. – rkosegi Oct 08 '12 at 15:54
1

A time_t type holds a value representing the number of seconds since the UNIX epoch. A tm type holds a calendar value.

gmtime() just converts system time (which is always UTC) from time_t to tm. That's why the values are the same. If you want a representation of your local time (GMT+1), that's what localtime() is for.

Simes
  • 453
  • 5
  • 8
  • I am stumped. The only thing I can think of is some weirdness with the compiler or similar that you're using, but the code you describe, as far as I can see, ought to do what you're expecting of it. Unless it's running in a virtual machine, in which case it's the system and local time settings of the VM which are relevant here. – Simes Oct 08 '12 at 16:13
  • I am compiling the program using cygwin (gcc). – ata Oct 08 '12 at 16:17
  • That's probably your problem. Check the value of your TZ environment variable; if not present, it will default to GMT. Cygwin doesn't automatically pick up the time zone setting from Windows. See also http://stackoverflow.com/questions/11655003/localtime-returns-gmt-for-windows-programs-running-on-cygwin-shells – Simes Oct 08 '12 at 16:19
0

Run the debugger over these two lines:

struct tm* gmt = gmtime(&current_time);

struct tm* loc = localtime(&current_time);

Break on the second line, watch members of gmt and when you execute the second line - you will see that some gmt members change value. Apparently some static memory is used by the library. So save the results of the first statement before running the second