libfaketime icon indicating copy to clipboard operation
libfaketime copied to clipboard

Timezone in fixed timestamp is lost

Open MFAshby opened this issue 2 years ago • 2 comments

This issue was already raised, but closed: https://github.com/wolfcw/libfaketime/issues/175

It seems that the timezone is lost when setting the fake date & time. Example script:

#!/usr/bin/env bash
#
# Sample program showing lack of timezone info in fixed time
# Run, and output looks like: 
# 
# $ ./faketime.sh
# 2022-01-02T03:04:05+00:00
# 
# expected output would be:
# 2022-01-02T03:04:05+06:00
LD_PRELOAD=/usr/lib/faketime/libfaketime.so.1 \
 FAKETIME_FMT="%Y-%m-%dT%T%z" \
 FAKETIME="2022-01-02T03:04:05+06:00" \
 date --iso=seconds

I'm running arch linux, I'll try a couple of other platforms if I can. Note that strptime behaves as expected on my system: the following C program checks it

#define _XOPEN_SOURCE
#include <time.h>
#include <stdio.h>
/*
Example program showing parsing and printing of a timestamp with zone offset
Build & run, it should look like this:
$ gcc -o main main.c && ./main
2022-01-02T03:04:05+0600
*/
int main(int argc, char* argv[]) {
    const char* example_format = "%Y-%m-%dT%T%z";
    const char* example_time = "2022-01-02T03:04:05+06:00";
    struct tm tm = {0};
    char buf[BUFSIZ] = {0};

    strptime(example_time, example_format, &tm);
    strftime(buf, BUFSIZ, example_format, &tm);
    
    printf("%s\n", buf);
    return 0;
}

MFAshby avatar Apr 05 '22 11:04 MFAshby

Having dug into this a bit, it seems like generally there's two function calls required to get a time with a timezone:

    time_t t = time(NULL);
    localtime_r(&t, &tm);
    strftime(buf, BUFSIZ, example_format, &tm);    
    printf("%s\n", buf);

so it'd be necessary for libfaketime to hook into localtime(_r) and friends in order to fake the current system time zone. It would also be necessary to somehow supply a fake value for /etc/localtime, since some applications look at that rather than using libc's localtime (postgres, for example). Hm.

I'm working around this for now by simply converting to UTC before writing to /etc/faketimerc, which is fine.

MFAshby avatar Apr 05 '22 18:04 MFAshby

Not sure whether hooking into localtime is necessary here. Have you tried replacing libfaketime's use of mktime with timegm?

wolfcw avatar Jul 25 '22 18:07 wolfcw