Search code examples
clinuxutcmktime

Why is there a one hour difference between two time_t values when converting to tm struct and back?


When I execute the following code:

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

int main(int argc, char *argv[]) {
  time_t rawtime = 0;
  time_t secs;
  struct tm* timeinfo = gmtime(&rawtime);

  printf("rawtime : %s\n", asctime(timeinfo));

  secs = mktime(timeinfo);

  printf("converted time : %s\n", asctime(gmtime(&secs)));

  return 0;
}

The output is :

rawtime : Thu Jan  1 00:00:00 1970
converted time : Wed Dec 31 23:00:00 1969

Why is this one hour difference?

I am running Ubuntu 14.10 64bit btw.


Solution

  • Because mktime converts a local time, not a system time (gmtime), into a timestamp.