Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if daylight saving time is active in C?

Tags:

c

dst

I have a C application running on cross platforms. In this program, I need to write a function which determines if the given date is DST or not.
Actually, i try to find DST begin-DST end dates in pure C. Is there any simple and standard way to do this?

like image 615
Fer Avatar asked Aug 18 '11 11:08

Fer


2 Answers

time.h provides tm structs with a tm_isdst flag. Use time to get the current time, localtime to get a tm struct with the time adjusted to the current locale and read the tm_isdst flag.

From the manpage:

tm_isdst  A flag that indicates whether daylight saving time is in effect at the
time described.  The value is positive if daylight saving time is in effect, zero 
if it is not, and negative if the information is not available.
like image 126
pmr Avatar answered Sep 20 '22 12:09

pmr


The code is:

time_t rawtime;
struct tm timeinfo;  // get date and time info
time(&rawtime);
localtime_s(&timeinfo, &rawtime);
int isdaylighttime = timeinfo.tm_isdst;
like image 29
pollaris Avatar answered Sep 20 '22 12:09

pollaris