Detect DST flag of future date in C - c

I have a system where I am provided with date and time in the form of a string, for example "2011-03-13 03:05:00". I may receive this string at "2011-03-13 01:59:00" and I need to know the length of time between now and the time in the string (6 minutes due to DST change).
I have code that parses the string and creates a tm struct which is then converted to a time_t with mktime. The problem is that I have to set the tm_isdst flag manually when I parse the time and so I'm looking for a way to detect whether tm_isdst should be set. Any ideas?
I have some ideas for how to deal with the case where there are 2, 2AMs that would be specific to my application, but I still need a way to say "If this time was the current system time, would DST be in effect?"
Edit: Idea based on Pete's suggestion. What if I:
Check if the time received and current system time have a different hour:
If same hour but in the past, add an hour to current time and see if DST flag changed. (If same hour and in future, assume same DST flag as current time (this is most of the year))
If different hour, we add 1 hour to current system time and see if DST flag changed
Thoughts?

According to man mktime (on Linux, emphasis mine):
The value specified in the tm_isdst
field informs mktime() whether or not
daylight saving time (DST) is in
effect for the time supplied in the tm
structure: a positive value means DST
is in effect; zero means that DST is
not in effect; and a negative value
means that mktime() should (use
timezone information and system
databases to) attempt to determine
whether DST is in effect at the
specified time.
Have you tried that?
(It's "attempt to determine" because some times are fundamentally ambiguous.)
Something you could try for the truly ambiguous times is to see if mktime "corrects" your dst flag or not. I'd wager this is non-portable though. Example code, transition set on 31/10/2010, 3am rolls back to 2am in my timezone (Europe/Paris):
#include <time.h>
#include <stdio.h>
#include <string.h>
void printit(int hour, int isdst)
{
struct tm when;
memset(&when, 0, sizeof(when));
when.tm_sec = 0;
when.tm_min = 30;
when.tm_hour = hour;
when.tm_mday = 31;
when.tm_mon = 9;
when.tm_year = 110;
when.tm_isdst = isdst;
time_t secs = mktime(&when);
fprintf(stdout, "%2d %ld %d %s", isdst, secs, when.tm_isdst, asctime(&when));
}
int main(int argc, char **argv)
{
for (int i=1; i<4; i++) {
fprintf(stdout, "At %dam\n", i);
printit(i, 1);
printit(i, 0);
printit(i, -1);
}
}
Output is:
At 1am
1 1288481400 1 Sun Oct 31 01:30:00 2010
0 1288485000 1 Sun Oct 31 02:30:00 2010
-1 1288481400 1 Sun Oct 31 01:30:00 2010
At 2am
1 1288485000 1 Sun Oct 31 02:30:00 2010
0 1288488600 0 Sun Oct 31 02:30:00 2010
-1 1288488600 0 Sun Oct 31 02:30:00 2010
At 3am
1 1288488600 0 Sun Oct 31 02:30:00 2010
0 1288492200 0 Sun Oct 31 03:30:00 2010
-1 1288492200 0 Sun Oct 31 03:30:00 2010
As you can see, when the time is non-ambiguous, mktime corrects it by setting the right tm_isdst and offsetting the time.
When it is ambiguous, tm_isdst is not changed.

Count on me always for the brute-force way. This might work:
completely parse the time string and put it into a tm structure
call mktime( ) to get a time_t val on that tm struct
add 6 minutes to that new time_t val (using what value for 6 minutes?)
call asctime( ) or similar to get a tm struct (i.e., a "broken-down time") from that time_t val
in this tm struct you just got back from asctime( ), what does tm_isdst tell you ?
-- Pete
Yes, you are right: the whole mess around time is very badly (even maliciously, one at times believes) designed to trip up programmers and to provide perfect closed-book quiz questions for your whoreson lazy / pedantic / idiotic CS instructors.

I have the same question as you, I don't know whether this solution can fix it?
struct tm *local_tm;
time_t t;
t = time(NULL);
local_tm = localtime(&t);
local_tm->tm_year = 2012-1900;
local_tm->tm_mon = 9 - 1;
local_tm->tm_mday = 13;
time_t utc_time = mktime(local_tm);
I think if it you get tm from the system, the tm_isdst field is already set, so the things left is change other fields except tm_isdst, I am not sure whether it is right, but I will try it, if it works for me,I will give further feedback.

Related

C - mktime altering passed time_t value

I need the functionality for a user to enter a timestamp, store that timestamp on the disk, then display that timestamp at a later time. I also need to be able to determine the distance between two timestamps.
My current solution is to use the time.h utility as follows:
To read a given timestamp of the form "MO/DA/YEAR HR:MI"
time_t readable_to_epoch(char* str) {
int mo, da, yr, hr, mi;
sscanf(str, "%d/%d/%d %d:%d", &mo, &da, &yr, &hr, &mi);
struct tm timeinfo;
time_t out;
timeinfo.tm_mon = mo - 1;
timeinfo.tm_mday = da;
timeinfo.tm_year = yr - 1900;
timeinfo.tm_hour = hr;
timeinfo.tm_min = mi;
timeinfo.tm_sec = 0;
out = mktime(&timeinfo);
return out;
}
To convert a given time_t into a human readable form:
// WARNING returned string must be freed;
char* epoch_to_readable(time_t time) {
return asctime(localtime(&time));
}
My issue is that if I pass in the input:
06/19/2018 12:52
The conversion to time_t and back is as desired. Here is the test harness output:
original: 06/19/2018 12:52
epoch time: 1529437920
converted readable: Tue Jun 19 12:52:00 2018
But when I pass in the input:
01/01/2022 10:00
The converted readable seems to be one hour behind:
original: 01/01/2022 10:00
epoch time: 1641056400
converted readable: Sat Jan 1 09:00:00 2022
I have no idea why this is happening, and google isn't helping. What do I need to do to fix this?
You did not set timeinfo.tm_isdst, so you're getting undefined behaviour. You should use -1.
Daylight Saving Time flag. The value is positive if DST is in effect, zero if not and negative if no information is available
Note that working with local times without offset is inherently buggy. Because of Daylight Saving Time, time stamps from two hours each year are indistinguishable from each other.

Why does mktime give me an hour less?

I would like to see if at 00:00:00 on January 1, 1970 it actually corresponds to 0 seconds, and I wrote the following:
#include <stdio.h>
#include <time.h>
int main(void) {
int year = 1970;
struct tm t = {0};
t.tm_mday = 1; // January
t.tm_year = year - 1900;
t.tm_hour = 0;
t.tm_isdst = -1;
printf("%ld\n", mktime(&t));
return 0;
}
it gives me a value of -3600. Where am I wrong?
PS: tested with GCC v.10.1. I tried with another compiler under another architecture and it gives me back the correct value.
The time info you provide to mktime() is in local time, so the timezone matters even if summer time / daylight savings time does not.
You can fool your program by telling it you're in UTC:
$ gcc mytime.c -o mytime
$ ./mytime
28800 <-- Pacific time in the US
$ TZ=GMT0 ./mytime
0
The mktime function takes a time in local time. Apparently, 00:00:00 at your local time was one hour before the epoch. Launch the program with TZ set to UTC.
I would like to see if at 00:00:00 on January 1, 1970 it actually corresponds to 0 seconds, and I wrote the following:
00:00:00 on January 1, 1970 GMT, UTC corresponds to 0 seconds.
00:00:00 on January 1, 1970 Italia time corresponds to -3600 seconds.
Set timezone to UTC and then call mktime(). Unfortunately C does not have a portable way to do this, so the suggested code is only illustrative.
setenv("TZ", "UTC", 1);
tzset();
....
mktime(&t)
time_t does not necessarily match long. Recommend casting to a wide type.
// printf("%ld\n", mktime(&t));
printf("%lld\n", (long long) mktime(&t));
t.tm_mday = 1; // January misleads. .tm_mday is the day of the month, not January.
.tm_mon is the months since January so the initialization to 0 matches January.
Concerns about DST apply here only if the local time was using DST in January.
As other answers indicate, mktime works in your local time zone. However, many operating systems offer a related function timegm that works in UTC. This slight modification of your program prints 0, as expected, on my computer:
#include <stdio.h>
#include <time.h>
int main(void)
{
int year = 1970;
struct tm t = {0};
t.tm_mday = 1; // January
t.tm_year = year - 1900;
t.tm_hour = 0;
t.tm_isdst = -1;
printf("%ld\n", timegm(&t));
return 0;
}
Regrettably, this function is not standardized. You may have to define a special "feature selection macro" to get your time.h to declare it.

time_t wrap around date

I'm trying to find out the date when the time will overflow the time_t value
#include <stdio.h>
#include <time.h>
#include <limits.h>
main(){
time_t now,end;
now=time(NULL);
end=LONG_MAX;
printf("Current date : %s\n",ctime(&now));
printf("Date of death : %s\n",ctime(&end));
}
I found out that on my system time_t is 8 bytes like LONG_MAX. When I run it I get:
Current date : Sun Feb 1 17:29:09 2015 and, End Date of death : (null)
but when I set end=INT_MAX; I get Date of Death : Tue Jan 05:14:07 2038 so why do I get (null) with LONG_MAX? and not a normal date
64-bit long would have the wraparound at somewhere around the year 292,277,026,596, which is ~20 times the current age of the universe from now. As the Earth's rotation would be unlikely to hold until at that time, and neither would people really care much, at that time it is very apt to return (null).
Joking aside most probably the real reason is that the ctime algorithm might use localtime to convert time_t to broken-down time, and that uses int for the year value.
The following python script can be used to find the maximum value of time_t for which ctime does not throw an exception (it is a thin wrapper over the libc ctime, implementation in C is left as an exercise for the reader):
import time
t = 0
for i in range(56, -1, -1):
try:
newt = t + (2 << i)
time.ctime(newt)
t = newt
except ValueError:
pass
print("Maximum value", bin(t))
print("Date at maximum", time.ctime(t))
The code outputs on my computer, glibc 2.19:
Maximum value 0b11110000110000101001110110000110100010111110000101011010
Date at maximum Tue Dec 31 23:59:52 2147483647
2147483647 is 2 ^ 31 - 1.

Get the time zone GMT offset in C

I'm using the standard mktime function to turn a struct tm into an epoch time value. The tm fields are populated locally, and I need to get the epoch time as GMT. tm has a gmtoff field to allow you to set the local GMT offset in seconds for just this purpose.
But I can't figure out how to get that information. Surely there must be a standard function somewhere that will return the offset? How does localtime do it?
Just do the following:
#define _GNU_SOURCE /* for tm_gmtoff and tm_zone */
#include <stdio.h>
#include <time.h>
/* Checking errors returned by system calls was omitted for the sake of readability. */
int main(void)
{
time_t t = time(NULL);
struct tm lt = {0};
localtime_r(&t, &lt);
printf("Offset to GMT is %lds.\n", lt.tm_gmtoff);
printf("The time zone is '%s'.\n", lt.tm_zone);
return 0;
}
Note: The seconds since epoch returned by time() are measured as if in Greenwich.
How does localtime do it?
According to localtime man page
The localtime() function acts as if it called tzset(3) and sets the
external variables tzname with information about the current timezone,
timezone with the difference between Coordinated Universal
Time (UTC) and local standard time in seconds
So you could either call localtime() and you will have the difference in timezone or call tzset():
extern long timezone;
....
tzset();
printf("%ld\n", timezone);
Note: if you choose to go with localtime_r() note that it is not required to set those variables you will need to call tzset() first to set timezone:
According to POSIX.1-2004, localtime() is required to behave as though
tzset() was called, while localtime_r() does not have this
requirement. For portable code tzset() should be called before
localtime_r()
The universal version of obtaining local time offset function is here.
I borrowed pieces of code from this answer in stackoverflow.
int time_offset()
{
time_t gmt, rawtime = time(NULL);
struct tm *ptm;
#if !defined(WIN32)
struct tm gbuf;
ptm = gmtime_r(&rawtime, &gbuf);
#else
ptm = gmtime(&rawtime);
#endif
// Request that mktime() looksup dst in timezone database
ptm->tm_isdst = -1;
gmt = mktime(ptm);
return (int)difftime(rawtime, gmt);
}
I guess I should have done a bit more searching before asking. It turns out there's a little known timegm function which does the opposite of gmtime. It's supported on GNU and BSD which is good enough for my purposes. A more portable solution is to temporarily set the value of the TZ environment variable to "UTC" and then use mktime, then set TZ back.
But timegm works for me.
This is the portable solution that should work on all standard C (and C++) platforms:
const std::time_t epoch_plus_11h = 60 * 60 * 11;
const int local_time = localtime(&epoch_plus_11h)->tm_hour;
const int gm_time = gmtime(&epoch_plus_11h)->tm_hour;
const int tz_diff = local_time - gm_time;
Add std:: namespace when using C++. The result is in hours in the range [-11, 12];
Explanation:
We just convert the date-time "1970-01-01 11:00:00" to tm structure twice - with the local timezone and with the GMT. The result is the difference between hours part.
The "11:00::00" has been chosen because this is the only time point (considering GMT) when we have the same date in the whole globe. Because of that fact, we don't have to consider the additional magic with date changing in the calculations.
WARNING
Previous version of my answer worked only on linux:
// DO NOT DO THAT!!
int timezonez_diff = localtime(&epoch_plus_11h)->tm_hour -
gmtime(&epoch_plus_11h)->tm_hour;
This may not work because the storage for result tm object returned as a pointer from localtime and gmtime may be shared (and it is on windows/msvc). That's whe I've introduced temporaries for calculation.
I believe the following is true in linux at least: timezone info comes from /usr/share/zoneinfo/. localtime reads /etc/localtime which should be a copy of the appropriate file from zoneinfo. You can see whats inside by doing zdump -v on the timezone file (zdump may be in sbin but you don't need elevated permissions to read timezone files with it). Here is a snipped of one:
/usr/share/zoneinfo/EST5EDT Sun Nov 6 05:59:59 2033 UTC = Sun Nov 6 01:59:59 2033 EDT isdst=1 gmtoff=-14400
/usr/share/zoneinfo/EST5EDT Sun Nov 6 06:00:00 2033 UTC = Sun Nov 6 01:00:00 2033 EST isdst=0 gmtoff=-18000
/usr/share/zoneinfo/EST5EDT Sun Mar 12 06:59:59 2034 UTC = Sun Mar 12 01:59:59 2034 EST isdst=0 gmtoff=-18000
/usr/share/zoneinfo/EST5EDT Sun Mar 12 07:00:00 2034 UTC = Sun Mar 12 03:00:00 2034 EDT isdst=1 gmtoff=-14400
/usr/share/zoneinfo/EST5EDT Sun Nov 5 05:59:59 2034 UTC = Sun Nov 5 01:59:59 2034 EDT
I guess you could parse this yourself if you want. I'm not sure if there is a stdlib function that just returns the gmtoff (there may well be but I don't know...)
edit: man tzfile describes the format of the zoneinfo file. You should be able to simply mmap into a structure of the appropriate type. It appears to be what zdump is doing based on an strace of it.
Here's a two-liner inspired by #Hill's and #friedo's answers:
#include <time.h>
...
time_t rawtime = time(0);
timeofs = timegm(localtime(&rawtime)) - rawtime;
Returns offset from UTC in seconds.
Doesn't need _GNU_SOURCE defined, but note that timegm is not a POSIX standard and may not be available outside of GNU and BSD.
Ended up with this. Sure tm_secs is redundant, just for a sake of consistency.
int timezone_offset() {
time_t zero = 0;
const tm* lt = localtime( &zero );
int unaligned = lt->tm_sec + ( lt->tm_min + ( lt->tm_hour * 60 ) ) * 60;
return lt->tm_mon ? unaligned - 24*60*60 : unaligned;
}
Here is my way:
time_t z = 0;
struct tm * pdt = gmtime(&z);
time_t tzlag = mktime(pdt);
Alternative with automatic, local storage of struct tm:
struct tm dt;
memset(&dt, 0, sizeof(struct tm));
dt.tm_mday=1; dt.tm_year=70;
time_t tzlag = mktime(&dt);
tzlag, in seconds, will be the negative of the UTC offset; lag of your timezone Standard Time compared to UTC:
LocalST + tzlag = UTC
If you want to also account for "Daylight savings", subtract tm_isdst from tzlag, where tm_isdst is for a particular local time struct tm, after applying mktime to it (or after obtaining it with localtime ).
Why it works:
The set struct tm is for "epoch" moment, Jan 1 1970, which corresponds to a time_t of 0.
Calling mktime() on that date converts it to time_t as if it were UTC (thus getting 0), then subtracts the UTC offset from it in order to produce the output time_t. Thus it produces negative of UTC_offset.
Here is one threadsafe way taken from my answer to this post:
What is the correct way to get beginning of the day in UTC / GMT?
::time_t GetTimeZoneOffset ()
{ // This method is to be called only once per execution
static const seconds = 0; // any arbitrary value works!
::tm tmGMT = {}, tmLocal = {};
::gmtime_r(&seconds, &tmGMT); // ::gmtime_s() for WINDOWS
::localtime_r(&seconds, &tmLocal); // ::localtime_s() for WINDOWS
return ::mktime(&tmGMT) - ::mktime(&tmLocal);
};

time(NULL) returning different time

I am trying to get current time in C using time_t current_time = time(NULL).
As I understand, it would return me the current time of system.
I am later trying to convert it into GMT time using struct tm* gmt = gmtime(&current_time).
I print both times using ctime() and asctime() functions.
The current time on my system is GMT + 1. But gmtime() returns me the same time as current_time is. I could not understand why gmtime() is returning me same time. Any help will be appreciated.
Ok here is the code and the output: Current time that windows is showing is 17:54 (Stockholm zone; GMT+1). I want something to return me 15:54. Or perhaps my understanding is wrong ...
time_t current_time = time(NULL);
struct tm* gmt = gmtime(&current_time);
struct tm* loc = localtime(&current_time);
printf("current time: %s\n", ctime(&current_time));
printf("gmt time %s\n", asctime(gmt));
printf("local time %s\n", asctime(loc));
Output:
current time: Mon Oct 8 17:54:06 2012
gmt time Mon Oct 8 17:54:06 2012
local time Mon Oct 8 17:54:06 2012
Accepted Solution: From Simes
That's probably your problem. Check the value of your TZ environment variable; if not present, it will default to GMT. Cygwin doesn't automatically pick up the time zone setting from Windows. See also localtime returns GMT for windows programs running on cygwin shells
A time_t type holds a value representing the number of seconds since the UNIX epoch.
A tm type holds a calendar value.
gmtime() just converts system time (which is always UTC) from time_t to tm. That's why the values are the same. If you want a representation of your local time (GMT+1), that's what localtime() is for.
time() returns the number of seconds since epoch. Which is equal to UTC (aka GMT)
Epoch was 1.1.1970, 00:00:00 in Greenwich, UK.
So in fact time() does not return a time, but a time difference.
Run the debugger over these two lines:
struct tm* gmt = gmtime(&current_time);
struct tm* loc = localtime(&current_time);
Break on the second line, watch members of gmt and when you execute the second line - you will see that some gmt members change value. Apparently some static memory is used by the library. So save the results of the first statement before running the second

Resources