tags:

views:

45

answers:

2

I have a piece of optimized function to get the GMT time. I would like to convert it to local time. I would want to call localtime and gmtime function only once to adjust the time to localtime as calling localtime and gmtime multiple times would defeat the purpose of using the optimized function. My idea is adding the difference in time zone to the GMT time I obtained. However, my problem is how could I adjust my localtime when there is daylight saving? Any ideas on checking that?

Thanks.

A: 

You could work directly with a timezone database but perhaps you do not like to introduce another component.

If I followed your idea, I would store the time differences per week or day in advance and use them later. This is a bit dirty because you will lose the exact time of DST switches. For best precision you could in theory use a binary search on localtime but that seems way excessive compared to directly using a time zone database and what you arrive at here is in effect a time zone zone database entry for your time zone.

Peter G.
The command `zdump -v <timezone>` dumps the time zone data for the selected zone in human readable form. I would parse that at program startup rather than reconstruct it by binary search. Or maybe I would just read the time zone data file directly: its format is documented in `man tzfile`.
Edgar Bonet
A: 

You could use TZ database that is stored most of the time in /usr/share/lib/zoneinfo in most linux distributions. This database manages daylight saving so you do not nees to deal with that.

AmineK