Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ Getting timestamp with time zone offset info

Tags:

c++

c

I need to get timestamp in the following format that includes the timezone offset from UTC:

2014-03-07T10:03:50+02:00

I'm trying this approach but getting it as UTC without the timezone info formatted correctly:

time_t now;
time(&now);
char ts[sizeof "1970-01-01T00:00:00+00:00"];
strftime(ts, sizeof ts, "%FT%T%z", gmtime(&now));
printf("Timestamp: %s\n\n", ts);

And this is the result:

2014-03-07T09:29:40+0200

like image 958
Dmitry R Avatar asked Mar 07 '14 09:03

Dmitry R


People also ask

How do I get a time zone offset?

The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.

Can I get timezone from TimeStamp?

You cannot “ get a TimeZone ID from a certain TimeStamp”, that is impossible. Your count-from-epoch was made while accounting for a certain time zone, usually UTC. If must know that intended zone used in creating that count-from-epoch, it cannot be deduced.

How do I get timezone with datetime?

You can get the current time in a particular timezone by using the datetime module with another module called pytz . You can then check for all available timezones with the snippet below: from datetime import datetime import pytz zones = pytz. all_timezones print(zones) # Output: all timezones of the world.

Does timezone offset change?

Breaking Down the Difference An offset is the number of hours or minutes a certain time zone is ahead of or behind GMT**. A time zone's offset can change throughout the year because of Daylight Saving Time. Sometimes laws change a time zone's offset or daylight savings pattern.


1 Answers

Man 7 strftime formats the time tm according to the format specification format. And one of the format %z outputs the numeric timezone by default as hhmm without colon : between hours and minutes,

%z : The +hhmm or -hhmm numeric timezone (that is, the hour and minute offset from UTC). (SU)

like image 117
Sunil Bojanapally Avatar answered Oct 25 '22 23:10

Sunil Bojanapally