timestamp conversion problem.

Hi all.

I have the following code:

#include<stdio.h>
#include<time.h>
int main()
{
      struct tm tm;
      time_t time = 1262322000; /*Jan, 01, 2010*/
      char temp[128];
      int i = 0;
      while(i < 4)
      {
            memset(temp, 0, 128);
            localtime_r(&time, &tm);
            strftime(temp, 16, "%G%m%d", &tm);
            printf("%s\n", temp);
            ++i;
            time += 86400;
       }
}

The output is:
20090101
20090102
20090103
20100104
Instead of:
20100101
20100102
20100103
20100104
here are the tm values after calling localtime_r for each iteration:

(gdb) p tm
$1 = {tm_sec = 0, tm_min = 0, tm_hour = 0, tm_mday = 1, tm_mon = 0, tm_year = 110, tm_wday = 5, tm_yday = 0, tm_isdst = 0, tm_gmtoff = -18000, 
  tm_zone = 0x1201080 "EST"}
$2 = {tm_sec = 0, tm_min = 0, tm_hour = 0, tm_mday = 2, tm_mon = 0, tm_year = 110, tm_wday = 6, tm_yday = 1, tm_isdst = 0, tm_gmtoff = -18000, 
  tm_zone = 0x1201080 "EST"}
$3 = {tm_sec = 0, tm_min = 0, tm_hour = 0, tm_mday = 3, tm_mon = 0, tm_year = 110, tm_wday = 0, tm_yday = 2, tm_isdst = 0, tm_gmtoff = -18000, 
  tm_zone = 0x1201080 "EST"}
$4 = {tm_sec = 0, tm_min = 0, tm_hour = 0, tm_mday = 4, tm_mon = 0, tm_year = 110, tm_wday = 1, tm_yday = 3, tm_isdst = 0, tm_gmtoff = -18000, 
  tm_zone = 0x1201080 "EST"}

tm_year is always 110.

Can anyone explain what's going on here?
Why the year for first three iterations is 2009?
Thanks!

Compare these two descriptions from the man page of strftime:

Notice the (subtle) difference? %G will format the year as the one that has the Monday of the Week, which in your case is the last week of 2009 (2009-12-28 to be exact). Change your %G to %Y, and you'll be fine.

Thanks a lot!! It fixed the problem!