Reputation: 43
I can printout time in UTC and local time like this:
time_t now;
struct tm ts, tm;
char buf[80];
now = time(NULL);
ts = *gmtime(&now);
tm = *localtime(&now);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
printf("%s\n", buf);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &tm);
printf("%s\n", buf);
But how do I printout time in a specified timezone that is different than UTC or my current timezone? Also, would it be OS/distro dependent?
Upvotes: 4
Views: 110
Reputation: 54118
You can use the approach documented here How can I set the time zone before calling strftime?, so use setenv
and tzset
before calling strftime:
setenv("TZ", "PST8PDT", 1);
tzset();
mtt = time(NULL);
mt = localtime(&mtt);
strftime(ftime,sizeof(ftime),"%Z %H%M",mt);
Upvotes: 2