localtime() and gmtime() giving exactly the same output - c

ADDENDUM:
I followed the suggestion provided and replaced calls to gmtime and localtime with calls to gmtime_r and localtime_r and everything worked as expected. The updated source code is as follows:
int main()
{
time_t tmi;
time(&tmi);
struct tm utcTime, localTime;
gmtime_r(&tmi, &utcTime);
localtime_r(&tmi, &localTime);
displayStructTm(&utcTime, "UTC");
displayStructTm(&localTime, "Local Time");
return (0);
}
Original Post:
I am making calls to gmtime() and localtime(), passing the same time_t value to both. However, I am getting the same result.
Here is my source code:
#include <stdio.h>
#include <time.h>
void displayStructTm(const struct tm* tmPtr, const char*label) {
printf("\n%s\n", label);
printf("tm_sec: %d\n", tmPtr->tm_sec);
printf("tm_hour: %d\n", tmPtr->tm_hour);
printf("tm_min: %d\n", tmPtr->tm_min);
printf("tm_mday: %d\n", tmPtr->tm_mday);
printf("tm_mon: %d\n", tmPtr->tm_mon);
printf("tm_year: %d\n", tmPtr->tm_year);
printf("tm_wday: %d\n", tmPtr->tm_wday);
printf("tm_yday: %d\n", tmPtr->tm_yday);
printf("tm_isdst: %d\n", tmPtr->tm_isdst);
}
int main()
{
time_t tmi;
time(&tmi);
struct tm* utcTime = gmtime(&tmi);
struct tm* localTime = localtime(&tmi);
displayStructTm(utcTime, "UTC");
displayStructTm(localTime, "Local Time");
return (0);
}
The output is as follows. As can be seen, we get the same output in both cases.
UTC
tm_sec: 27
tm_hour: 3
tm_min: 21
tm_mday: 6
tm_mon: 4
tm_year: 122
tm_wday: 5
tm_yday: 125
tm_isdst: 0
Local Time
tm_sec: 27
tm_hour: 3
tm_min: 21
tm_mday: 6
tm_mon: 4
tm_year: 122
tm_wday: 5
tm_yday: 125
tm_isdst: 0

The return value points to a statically-allocated struct which might be overwritten by subsequent calls to any of the date and time functions. The localtime_r() function does the same, but stores the data in a user-supplied struct.
— man localtime
Since you call gmtime, then call localtime, before printing either, you're getting two pointers to the same struct, which only contains the second result. Either print each result immediately after the call, or use localtime_r and gmtime_r with pointers to struct tms that you have allocated yourself.

Two possibilities.
The local time zone of your system currently has an offset of 0.
utcTime and localTime are sharing the same memory.
localtime and gmtime return a pointer to the same struct every time. They may share the same memory. So utcTime and localTime might be the same.
You can check with printf("gm: %p local: %p", utcTime, localTime).
Use localtime_r and gmtime_r to avoid this.

Related

mktime() for non-local timezone

In C the function mktime() returns the epoch time according to the local timezone (the input struct is locally formatted).
The function timegm() returns the epoch time according to the UTC time (the input struct is formatted based off of UTC time).
The function localtime_r () takes in an epoch time and returns a local timezone formatted struct.
The function gmtime_r () takes in an epoch time and returns a UTC formatted struct.
I need to find out if a non-local timezone is currently daylight savings time or not, which would work with the localtime_r() function if it were local, but what if it were not local?
The gmtime_r() function always sets the tm_isdst field to zero, which won't work here.
Maybe there's some other function I am not aware of. Not sure.
If you (a) don't want to muck around with a global environment variable and (b) have the "NetBSD inspired" time functions available to you, there's an additional possibility: mktime_z() and localtime_rz(), which let you explicitly specify the zone you want to use. So you're not limited to your default local zone, or UTC.
Here's an example:
int main(int argc, char **argv)
{
timezone_t tzp = tzalloc(argv[1]);
if(tzp == NULL) return 1;
time_t now = time(NULL);
struct tm tm;
struct tm *tmp = localtime_rz(tzp, &now, &tm);
char tmpbuf[20];
strftime(tmpbuf, sizeof(tmpbuf), "%H:%M:%S", tmp);
printf("right now in zone %s is %s\n", argv[1], tmpbuf);
tm.tm_year = 1976 - 1900;
tm.tm_mon = 7 - 1;
tm.tm_mday = 4;
tm.tm_hour = 12;
tm.tm_min = tm.tm_sec = 0;
tm.tm_isdst = -1;
time_t t = mktime_z(tzp, &tm);
printf("in zone %s, %d-%02d-%02d %d:%02d was %ld\n", argv[1],
1900+tm.tm_year, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min, t);
}
When I invoke tzt America/New_York I see
right now in zone America/New_York is 11:58:23
in zone America/New_York, 1976-07-04 12:00 was 205344000
and when I invoke tzt America/Los_Angeles I see
right now in zone America/Los_Angeles is 08:58:49
in zone America/Los_Angeles, 1976-07-04 12:00 was 205354800
Now, with that said, two further comments, tied to my opening "if"s:
a. If you don't want to muck around with a global environment variable, I don't blame you one tiny bit. I positively hate mucking around with global variables (let alone environment variables) to affect how a function like mktime or localtime behaves. Unfortunately, however, this is the recommended way, in C, in this situation — see this question's other answers 1, 2 for details.
b. Chances are unfortunately quite good that you don't, in fact, "have the NetBSD inspired time functions available to you". They're nonstandard and not even very popular. I was able to compile the test program above only because I had a copy of the IANA tz database and its code handy, which includes those functions if you also define NETBSD_INSPIRED. (That's why I broke the rules and didn't show a complete example, with all #include lines, since mine were weird and idiosyncratic.)
I need to find out if a non-local timezone is currently daylight savings time or not
Get the current time as a time_t from time.
Set env var TZ to the target time zone using putenv.
Call tzset. (I don't know if this is required, but there's surely no harm in calling it.)
Use localtime to convert the time_t into a struct tm according to the (modified) local time zone.
Check the tm_isdst field of that struct tm.
Here's some code I wrote a decade ago that does what ikegami suggests in their answer:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
static void time_convert(time_t t0, char const *tz_value)
{
char old_tz[64] = "-none-";
char *tz = getenv("TZ");
if (tz != 0)
strcpy(old_tz, tz);
setenv("TZ", tz_value, 1);
tzset();
char new_tz[64];
strcpy(new_tz, getenv("TZ"));
char buffer[64];
struct tm *lt = localtime(&t0);
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", lt);
if (strcmp(old_tz, "-none-") == 0)
unsetenv("TZ");
else
setenv("TZ", old_tz, 1);
tzset();
printf("%lld = %s (TZ=%s, DST = %d)\n",
(long long)t0, buffer, new_tz, lt->tm_isdst);
}
int main(void)
{
time_t t0 = time(0);
char *tz = getenv("TZ");
if (tz != 0)
time_convert(t0, tz);
time_convert(t0, "UTC0");
time_convert(t0, "IST-5:30");
time_convert(t0, "EST5");
time_convert(t0, "EST5EDT");
time_convert(t0, "PST8");
time_convert(t0, "PST8PDT");
}
The output I get is:
1650647033 = 2022-04-22 17:03:53 (TZ=UTC0, DST = 0)
1650647033 = 2022-04-22 22:33:53 (TZ=IST-5:30, DST = 0)
1650647033 = 2022-04-22 12:03:53 (TZ=EST5, DST = 0)
1650647033 = 2022-04-22 13:03:53 (TZ=EST5EDT, DST = 1)
1650647033 = 2022-04-22 09:03:53 (TZ=PST8, DST = 0)
1650647033 = 2022-04-22 10:03:53 (TZ=PST8PDT, DST = 1)
Note that some of the time zones are specified without a daylight saving time, and the code reports DST = 0 for those zones.
Be wary of changing the time zone like this in multi-threaded applications. And be cautious about resetting the environment in case you fork() and exec() other programs with an unexpected value for the TZ environment variable.
Note: I've modified the code to:
Use long long instead of long for printing the time_t value. One of the annoying things is that there is no standard format string for printing the time_t type as an integer (indeed, the C standard doesn't even guarantee that the type is an integer, but it actually is on most systems, and POSIX requires it to be an integer type — see <sys/types.h>). This change should allow 32-bit systems to work in 2038. That assumes that time_t is a 64-bit value even though it is a 32-bit system; if the system still uses a 32-bit time_t, it is terminally broken when time_t wraps around to -231 — but that shouldn't still be my problem then.
Print lt->tm_isdst which is the information wanted in the question.

How to compare GMT time and local time in C?

my server uses local time of Prague (+ 2 hours) and visitor's request uses GMT time.
In code I want to compare these times but for that I need to convert them to the same time zone. How to do it? When I try to use gmtime() and localtime() they returns same result.
struct tm time;
struct stat data;
time_t userTime, serverTime;
// this time will send me user in GMT
strptime("Thu, 15 Apr 2021 17:20:21 GMT", "%a, %d %b %Y %X GMT", &time)
userTime = mktime(&time); // in GMT
// this time I will find in my server in another time zone
stat("test.txt", &data);
serverTime = data.st_mtimespec.tv_sec; // +2 hours (Prague)
// it's not possible to compare them (2 diferrent time zones)
if(serverTime < userTime) {
// to do
}
Thank you for answer.
On linux with glibc you can just use %Z with strptime to read GMT.
#define _XOPEN_SOURCE
#define _DEFAULT_SOURCE
#include <time.h>
#include <assert.h>
#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
int main() {
// this time will send me user in GMT
struct tm tm;
char *buf = "Thu, 15 Apr 2021 17:20:21 GMT";
char *r = strptime(buf, "%a, %d %b %Y %X %Z", &tm);
assert(r == buf + strlen(buf));
time_t userTime = timegm(&tm);
// this time represents time that has passed since epochzone
struct stat data;
stat("test.txt", &data);
// be portable, you need only seconds
// see https://pubs.opengroup.org/onlinepubs/007904875/basedefs/sys/stat.h.html
time_t serverTime = data.st_mtime;
// it's surely is possible to compare them
if (serverTime < userTime) {
// ok
}
}
// it's not possible to compare them (2 diferrent time zones)
But it is!
The time that has passed since an event can't be in a timezone. Count of seconds since epoch is how many seconds have passed since that event, it's relative time that has passed, it's distance in time. No matter in which timezone you are, no matter if daylight saving time or not, the time that has passed since an event is the same in every location (well, excluding relativistic effects, which we don't care about). Timezone is irrelevant. mktime returns the number of seconds since epoch. stat returns timespec which represents time that have passed since epoch. Timezone has nothing to do here. Once you represent time as relative to some event (ie. since epoch), then just compare them.

year not displaying on linux

Hope someone can help. I am fixing a problem in someone’s’ C code that was written a long time ago and he has since moved on.
The piece of code outputs the timestamp of a particular file. The code works fine when run on windows but when it is run on Linux it displays the Year incorrectly. The year is not displaying on linux, it shows 35222. Does anyone have any idea what is the problem here?
Thanks
Windows output:
Source file: test.dtl, Created: Mon, 27 May, 2013 at 16:13:20
Linux output:
Source file: test.dtl, Created: Mon, 27 May, 35222 at 16:13:20
The function in C code:
void SummaryReport ( report_t *report, char *dtlName)
{
LogEntry(L"SummaryReport entry\n");
int i;
wchar_t *rootStrType,*localStr,timeStr[48];
wchar_t fileBuff[64];
struct tm *timeVals;
timeVals = localtime (&logHdr.date);
wcsftime (timeStr,47,L"%a, %#d %b, %Y at %X",timeVals);
/* Print the header information */
DisplayReportFile (report);
ReportEntry (report,L" Filesystem Audit Summary Report\n\n");
ReportEntry (report,L"Source file: %s, Created: %ls\n\n",dtlName,timeStr);
ReportEntry (report,L"Server: %ls",srvrName);
…
}
Verified on a minimal example and it "works-for-me". Does this show the right time?
#include <wchar.h>
#include <time.h>
int main() {
wchar_t timeStr[48];
struct tm *timeVals;
time_t now = time(NULL);
timeVals = localtime(&now);
wcsftime(timeStr, 47, L"%a, %#d %b, %Y at %X", timeVals);
wprintf(timeStr);
return 0;
}
If yes, check the file itself - if you're sharing the filesystem, maybe there's some weird issue with the file timestamp itself? (or with understanding the fs metadata)
In case wcsftime() itself calls localtime(), insure the results of your call are not corrupted.
struct tm timeVals;
timeVals = *localtime (&logHdr.date);
wcsftime (timeStr,47,L"%a, %#d %b, %Y at %X", &timeVals);
localtime() saves its results in a static struct tm somewhere. The address (pointer) to that location is returned. Subsequent calls to localtime() or gmtime() alter the struct tm. I suspect the call to wcsftime() indirectly does that in Linux.
BTW: localtime() could return NULL, so safer code would check the localtime() return value.
Your may want to look into the localtime_s()

Difference between mktime and timelocal

What is the difference between these two functions? It was my understanding that those should be the same: http://www.gnu.org/software/libc/manual/html_node/Broken_002ddown-Time.html.
I wrote this code to test the conversion (the Qt part is only for comparison):
#include <QCoreApplication>
#include <QDateTime>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDateTime datetime(QDate(2012, 3, 25), QTime(5, 15));
qDebug("Timestamp: %lld.", datetime.toMSecsSinceEpoch()/1000L);
time_t timestamp;
tm* timeinfo = localtime(&timestamp);
timeinfo->tm_hour = 5;
timeinfo->tm_mday = 25;
timeinfo->tm_min = 15;
timeinfo->tm_mon = 2;
timeinfo->tm_year = 112;
timeinfo->tm_sec = 0;
qDebug("Timestamp: %ld.", timelocal(timeinfo));
return 0;
}
and found out that the output is:
Timestamp: 1332645300.
Timestamp: 1332645300.
which is what I'd expect. Then I replaced timelocal with mktime and found out that this was the output:
Timestamp: 1332645300.
Timestamp: 1332648900.
It seems like an hour was added (consider that my current timezone is GMT+2:00 and my locale is set to Italy). Why? What is the difference between the two and why mktime adds 1 hour to the date I set?
EDIT: I tested again and it seems that on Mac OS X (and iOS) timelocal is returning the same hour placed in the timeinfo structure, while mktime is actually adding an hour both in the returned time_t value and in the structure tm.
On Linux Kubuntu instead, with both functions I get that an hour is added to both the tm structure and the returned value.
Anyone who can explain why?
The man of OpenBSD's timelocal states:
timelocal is a deprecated interface that is equivalent to calling
mktime() with a negative value for tm_isdst
A negative value for tm_isdst means that timelocal doesn't take daylight saving time (DST) into account. It seems that QDateTime has troubles with DST as well.
mktime on the other hand, handles DST and this might explain the 1-hour difference on OS X.
Since timelocal is deprecated, it might have newer imlementations that deal with DST properly.

Converting between timezones in C

I need to convert time between timezones in C (on linux, so anything specific would do too).
I know my current time, local and UTC, I have the offset of the target time. I am trying to use mktime, gmtime, localtime and similar set of functions but still can't figure it out.
Thanks in advance.
As comments do not allow posting the code, posting as a separate answer.. If you know "local" time and "UTC" time, you can calculate the offset of the "other" time from your "local" time. Then you convert the struct tm into calendar time, add the desired number of seconds (being the offset of the target time), and convert it back to struct tm:
(edited to account for another scenario to use mktime's normalization)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
int main(int argc, char *argv) {
struct timeval tv_utc;
struct tm *local_tm, *other_tm;
/* 'synthetic' time_t to convert to struct tm for the other time */
time_t other_t_synt;
/* Other time is 1 hour ahead of local time */
int other_local_delta = 1*3600;
/* the below two lines are just to set local_tm to something */
gettimeofday(&tv_utc, NULL);
local_tm = localtime(&tv_utc.tv_sec);
printf("Local time: %s", asctime(local_tm));
#ifdef DO_NOT_WRITE_TO_LOCAL_TM
other_t_synt = mktime(local_tm) + other_local_delta;
#else
local_tm->tm_sec += other_local_delta;
/* mktime will normalize the seconds to a correct calendar date */
other_t_synt = mktime(local_tm);
#endif
other_tm = localtime(&other_t_synt);
printf("Other time: %s", asctime(other_tm));
exit(0);
}
You can use gmtime() and the tm structure to directly set this, provided you know the offsets.
If you know your local time and UTC, you know your local offset. Provided you also know the target offset, it's just a matter of setting tm_hour appropriate (and potentially flipping the day, too, if you go <0 or >23).
For some sample code, see this gmtime reference page. It shows offsetting based off time zone offsets.
Edit:
In response to the comments - you can also let mktime handle the shifting for you, which allows you to simplify this by converting back to a time_t. You can use something like:
time_t currentTime;
tm * ptm;
time ( &currentTime );
ptm = gmtime ( &rawtime );
ptm->tm_hour += hours_to_shift;
ptm->tm_minutes += minutes_to_shift; // Handle .5 hr timezones this way
time_t shiftedTime = mktime( ptm );
// If you want to go back to a tm structure:
tm * pShiftedTm = gmtime( &shiftedTime );
In all likelyhood, your operating system provides some support for this.
In unix derived OSs you might want to look at the man pages for asctime, asctime_r, ctime, ctime_r, difftime, gmtime, gmtime_r, localtime, localtime_r, mktime, timegm.

Resources