Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DST restoration under Linux in C

I have date/time information from Berlin,Germany. For example: 2011-05-16 12:24:23, 2011-01-21 17:00:31

Unfortunatly, these times have NO daylight saving information. I know that the first date is CEST and the second time is CET.

I could write a check whether the date/time is daylight saving or not.

The summertime starts at the last sunday in march at 2:00 AM, and ends at the last sunday in october at 3 AM.

Self developing such a check seems error prone to me. Is there something that can be already used under Linux in C?

Thank you for your help.

like image 961
Casi Avatar asked Oct 24 '22 11:10

Casi


2 Answers

You can convert the time to a time_t with mktime() and then pass it back in to localtime() which will set tm_isdst. Both mktime() and localtime() utilize your local system's time configuration, so you're really discovering if the timestamp is in DST where you are. The obvious problem here is that you have a timestamp string without geographical information so the best you can do is assume it's for your current location.

like image 137
Jesse Sipprell Avatar answered Oct 27 '22 09:10

Jesse Sipprell


You can pass the %Z format argument to the date command to obtain the timezone from a date, then determine if DST was in effect from the timezone's name:

$ date -d '2011-05-16' '+%Z'
CEST

$ date -d '2011-01-21' '+%Z'
CET
like image 27
Frédéric Hamidi Avatar answered Oct 27 '22 09:10

Frédéric Hamidi