Search code examples
ctimetimezonelocaltime

localtime with very old dates


I've been messing around with time.h and have come across the following strangeness.

time_t t;
struct tm loc, utc;
t = -11676066452; /* This time is mostly arbitrary. */
loc = *localtime(&t);
utc = *gmtime(&t);
printf("localtime: %s", asctime(&loc));
printf("gmtime:    %s", asctime(&utc));

The output is

 localtime: Sat Jan  1 00:00:00 1600
 gmtime:    Sat Jan  1 08:12:28 1600

So, apparently, the local absolute offset from UTC at that time is 8 hours, 12 minutes, and 28 seconds. The chosen date is well before timezones were even established, so how is localtime determining this offset?

My timezone is America/Vancouver (Pacific Daylight Time, or a UTC offset of -7 hours, currently) and I am using glibc 2.27.


Solution

  • You are simply observing the offset for the LMT (Local Mean Time) entry for America/Vancouver from the IANA TZ Database.

    # Zone    NAME               STDOFF    RULES    FORMAT  [UNTIL]
    Zone      America/Vancouver  -8:12:28  -        LMT     1884
                                 -8:00     Vanc     P%sT    1987
                                 -8:00     Canada   P%sT
    

    Source here: https://github.com/eggert/tz/blob/2020a/northamerica#L2134

    You can read more about LMT (and lots more) in the theory file in the tz database.