C - mktime altering passed time_t value - c

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.

Related

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.

converting milliseconds to date in C

Is there any way of converting milliseconds to date in C?
What I am trying to do is write a small application in C that can return the financial year and the like(quarter, week) given the start month and isCurentYear bool, where the input might be milliseconds or a date!
In the first place, is there any way by which this can be achieved in C?
And if so, in the process of finding out a way of converting milliseconds to date
I have found out that the use of time_t takes the current millis of our system and by creating a structure pointing to it,it permits us to extract the year,month, date, sec etc!
Refer the below code:
#include <sys/time.h>
#include<stdio.h>
#include<time.h>
void main()
{
time_t t = time(000);
//time_t t = time(0);
struct tm tm = *localtime(&t);
printf("now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
}
And also, can time_t be used to store millisecond values so that it can be converted to date using tm struct?
Function time_t time(time_t* timer) returns the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC. In addition, if the input argument timer != NULL, then the function also sets this argument to the same value (so you probably have no reason to call it with anything else but NULL).
Function struct tm* localtime(const time_t* timer) takes the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC, and returns a structure that represents the equivalent time & date. If you're working on a multi-threaded application, then please note that this function is not thread safe.
As to your question - is there any way for converting milliseconds to time & date - yes, but:
Take into consideration that the milliseconds will be considered as of 00:00 hours, Jan 1, 1970 UTC.
Since the time_t type is 32-bit long, you will not be able to convert 4G*1000 milliseconds or more.
Here is a function for converting milliseconds to time & date:
struct tm* GetTimeAndDate(unsigned long long milliseconds)
{
time_t seconds = (time_t)(milliseconds/1000);
if ((unsigned long long)seconds*1000 == milliseconds)
return localtime(&seconds);
return NULL; // milliseconds >= 4G*1000
}
For those of us who were searching the web for an answer to apply to embedded c applications, think pic 32 programming here is the mathematical calculation:
Date in Epoch_seconds = ( (epoch_seconds / 1000) / 86400 ) + 25569
Resulting in a 5 digit answer which is 10 bits long format dd/MM/yyyy
(Note: the slashes are encoded in the result here so when converting to human readable date please account for it)
Where one day = 86400 ms
and the date 1970/1/1 = 25569
example:=( (1510827144853/1000) / 86400 ) + 25569 = 43055
put 43055 in excel and format cell to date dd/MM/yyyy and it gives you 16/11/2017
Perhaps, you are looking for strftime function.
char text[100];
time_t now = time(NULL);
struct tm *t = localtime(&now);
strftime(text, sizeof(text)-1, "%d %m %Y %H:%M", t);
printf("Current Date: %s", text);

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

UTC time from a timestamp

I'm trying to get the UTC time from a localtime. Vice versa it works OK. I only have problems when I want to convert a localtime to a UTC one in order to update an RTC clock. This is what I'm doing:
Say that t is a local timestamp.
char *tz = "GMT-2GMT,M3.5.0/3,M10.5.0/4";
t = 1311444000; // 23/07/11 18:00:00
set_TZ(tz);
gmt_time = gmtime(&t);
mktime(gmt_time);
printf("GMT Time: %s\r\n",asctime (gmt_time));
This gives me 18:00:00, when it should be minus the timezone.
How do I fix this problem?
The problem here is that time_t is supposed to represent the number of seconds (simplification, see footnote) since the epoch. This is not affected by time zones. If you add a time zone offset to a time_t, none of the functions will work as expected. Part of the problem is that it is sometimes impossible to know how to correctly convert such a value to a POSIX timestamp -- when the clocks roll back in the fall, where I live the clocks will read 1:30 AM twice during the same day, and without more information you can't figure out what the UTC time is.
You'll need to convert to local calendar time before you can convert to a POSIX timestamp, at which point you can convert to UTC calendar time.
// NOTE: not reentrant
time_t local_to_posix(time_t t) {
struct tm *tm;
time_t tt;
tm = gmtime(&t); // No timezone compensation
tm->tm_isdst = -1; // Let mktime figure out daylight savings
// NOTE: this WILL be wrong for one hour each year
tt = mktime(tm); // Converts local time to POSIX timestamp
return tt;
}
You can then pass the result to gmtime, which will give you the UTC calendar time.
Footnote: Strictly speaking, these timestamps are not UTC and do not measure the number of seconds since epoch. They are POSIX timestamps, which count seconds since epoch as if there were no leap seconds. This is probably irrelevant unless your realtime clock is an atomic clock.
After further tests the following code this seems to work. Im in UTC+2 and currently in DST. The returned time for UTC is correct. I will to further test without DST and see what is returned.
struct tm tm;
struct tm *local_time;
char *tz = "GMT-2GMT,M3.5.0/3,M10.5.0/4";
time_t t;
tm.tm_hour = 18;
tm.tm_min = 0;
tm.tm_sec = 0;
tm.tm_wday = 0;
tm.tm_yday = 0;
tm.tm_year = (2011) - 1900;
tm.tm_mday = 22;
tm.tm_mon = 7 - 1;
tm.tm_isdst = -1;
set_TZ(tz);
t = mktime(&tm);
local_time = localtime(&t);
printf("Local Time: %s\r\n",asctime (local_time ));
local_time = gmtime(&t);
printf("UTC Time: %s\r\n",asctime (local_time ));
Output:
Local Time: Fri Jul 22 18:00:00 2011
UTC Time: Fri Jul 22 15:00:00 2011

Detect DST flag of future date in 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.

Resources