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.
I'm in the UK. I'm using C++ builder 10.2 with the clang compiler. The following code
#include <stdio.h>
#include <conio.h>
#include <time.h>
#ifdef _WIN32
#include <tchar.h>
#else
typedef char _TCHAR;
#define _tmain main
#endif
int _tmain()
{
printf("TZ set = %s\r\n",putenv("TZ=Europe/London")==0 ? "true" : "false");
printf("TZ=%s\r\n",getenv("TZ"));
for (int dst = 0, year = 2017; year <= 2023; year++)
for (int mon = 1; mon <= 12; mon++)
for (int mday = 1; mday <= 31; mday++)
{
struct tm st = { 0, 0, 12, mday, mon - 1, year - 1900 };
st.tm_isdst=-1;
time_t tt = mktime(&st); // this sets the tm_isdst to 1 or 0
if (st.tm_isdst != dst)
{
dst = st.tm_isdst;
printf("%02d/%02d/%d (%ld) ", mday - !dst, mon, year, tt-(!dst)*24*60*60);
if (!dst) printf("\r\n");
}
}
getch();
}
produces the following output
12/03/2017 (1489316400) 04/11/2017 (1509796800)
11/03/2018 (1520766000) 03/11/2018 (1541246400)
10/03/2019 (1552215600) 02/11/2019 (1572696000)
08/03/2020 (1583665200) 00/11/2020 (1604145600)
14/03/2021 (1615719600) 06/11/2021 (1636200000)
13/03/2022 (1647169200) 05/11/2022 (1667649600)
12/03/2023 (1678618800) 04/11/2023 (1699099200)
(The 00/11/2020 should be 30/10/2020 but I don't see the point of complicating the code to correct it).
The problem is the above dates are totally at odds with British Summer Time as listed by wiki -
2017 26 March 29 October
2018 25 March 28 October
2019 31 March 27 October
2020 29 March 25 October
2021 28 March 31 October
2022 27 March 30 October
2023 26 March 29 October
The BST starting dates provided by my code (left hand side) return unix timestamps that are 3600 secs (1 hour) out. From comments below it seems the output would be all correct if my TZ was set to Canadian-American but it's set to London.
EDIT: I'm rephrasing the question. HITF do you get the code above to use the time zone as set in the Windows 10 settings? No matter what I set the time zone to it still comes up with similar dates. The only time I get a correct answer is if I specifically make the time zone (UTC-8.00) Pacific Time (US & Canada). It seems to use that time zone regardless of the one selected in settings. It's been bad enough waking up during this lockdown and not knowing what day it is. Now I don't even know what time zone it is.
EDIT2: I added the lines
printf("TZ set = %s\r\n",putenv("TZ=Europe/London")==0 ? "true" : "false");
printf("TZ=%s\r\n",getenv("TZ"));
to the code and while they printed
TZ set = true
TZ=Europe/London
nothing changed.
Those dates are the first and last date of Canadian-American DST. Check what time zone is specified by the TZ environment variable.
Other issues:
You assume the order of fields in struct tm, but the order isn't specified by the language.
You don't initialize the tm_isdst field correctly. -1 should be used for if it's unknown whether DST is being used or not. The value is presumably used to handle the overlapped ("fall back") hours in a change out of DST.
Your code assumes the switch to DST happens earlier in the year than the switch from DST, but it would be the opposite in the southern hemisphere.
Program with these issues fixed:
#include <stdio.h>
#include <time.h>
int main(void) {
int dst = -1, dst_syear, dst_smon, dst_smday;
for (int year=2017; year<=2023; ++year) {
for (int mon=1; mon<=12; ++mon) {
for (int mday=1; mday<=31; ++mday) {
// Note that using .tm_isdst = -1 instead of the proper value
// will cause mktime to fail during one of the overlapped hours
// of a "fall back" change from DST.
struct tm st = {
.tm_year = year-1900,
.tm_mon = mon-1,
.tm_mday = mday,
.tm_hour = 12,
.tm_isdst = -1,
};
mktime(&st); // This sets the tm_isdst to 1 or 0
if (dst == -1) {
if (st.tm_isdst == 0) {
dst = 0;
}
} else {
if (st.tm_isdst != dst) {
dst = st.tm_isdst;
if (st.tm_isdst) {
dst_syear = year;
dst_smon = mon;
dst_smday = mday;
} else {
printf("%d-%02d-%02d %d-%02d-%02d\n",
dst_syear, dst_smon, dst_smday,
year, mon, mday);
}
}
}
}
}
}
return 0;
}
Output:
$ TZ=Europe/London ./a
2017-03-26 2017-10-29
2018-03-25 2018-10-28
2019-03-31 2019-10-27
2020-03-29 2020-10-25
2021-03-28 2021-10-31
2022-03-27 2022-10-30
2023-03-26 2023-10-29
$ TZ=America/Toronto ./a
2017-03-12 2017-11-05
2018-03-11 2018-11-04
2019-03-10 2019-11-03
2020-03-08 2020-11-01
2021-03-14 2021-11-07
2022-03-13 2022-11-06
2023-03-12 2023-11-05
$ TZ=Australia/Sydney ./a
2017-09-31 2018-04-01
2018-10-07 2019-04-07
2019-10-06 2020-04-05
2020-10-04 2021-04-04
2021-10-03 2022-04-03
2022-10-02 2023-04-02
Below I have four functions.
first() and second() initialize only the year, mon & mday of structure tm.
first_p() and second_p allocate memory using malloc, then assign year, mon & mday.
All functions call mktime() at the end.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void first()
{
int year1 = 2020, month1 = 4, day1 = 23;
struct tm date1 = {.tm_year = year1-1900, .tm_mon = month1-1, .tm_mday = day1};
mktime(&date1);
printf("%s", asctime(&date1));
}
void second()
{
int year2 = 2021, month2 = 5, day2 = 24;
struct tm date2 = {.tm_year = year2-1900, .tm_mon = month2-1, .tm_mday = day2};
mktime(&date2);
printf("%s", asctime(&date2));
}
void first_p()
{
int year1 = 2020, month1 = 4, day1 = 23;
struct tm *date1 = (struct tm *) malloc (sizeof(struct tm));
date1->tm_year = year1 - 1900;
date1->tm_mon = month1 -1;
date1->tm_mday = day1;
mktime(date1);
printf("%s", asctime(date1));
}
void second_p()
{
int year2 = 2021, month2 = 5, day2 = 24;
struct tm *date2 = (struct tm *) malloc (sizeof(struct tm));
date2->tm_year = year2 - 1900;
date2->tm_mon = month2 - 1;
date2->tm_mday = day2;
mktime(date2);
printf("%s", asctime(date2));
}
I tried different permutation when calling these functions in main():
1) first_p() and second_p() show random date and time.
int main()
{
first();
second();
first_p();
second_p();
return 0;
}
Thu Apr 23 00:00:00 2020
Mon May 24 00:00:00 2021
Thu Sep 30 23:09:20 66488
Wed Aug 31 14:44:48 66489
2) only second_p() shows random date and time.
int main()
{
first_p();
second_p();
first();
second();
return 0;
}
Thu Apr 23 00:00:00 2020
Sun Dec 8 01:26:16 -103880
Thu Apr 23 00:00:00 2020
Mon May 24 00:00:00 2021
3) Only second_p() shows random date and time.
int main()
{
first_p();
first();
second();
second_p();
return 0;
}
Thu Apr 23 00:00:00 2020
Thu Apr 23 00:00:00 2020
Mon May 24 00:00:00 2021
Thu Oct 9 04:53:52 60110
4) first_p() and second_p() show random date and time.
int main()
{
first();
first_p();
second_p();
second();
return 0;
}
Thu Apr 23 00:00:00 2020
Sat Sep 25 12:05:36 182934
Fri Aug 26 03:41:04 182935
Mon May 24 00:00:00 2021
What I observe is:
Directly initializing the structure and then passing it to mktime() anywhere has no strange behavior.
Allocating memory using malloc, then assigning the values and later passing it to mktime() has two behaviours:
If mktime() is being called for the first time, then there is no strange behavior.
else it shows shows some random date and time. Sometimes years are negative!
Am I missing something about mktime() that's responsible for this behavior?
Edit:
I added free(date1); and free(date2); at the end of first_p() and second_p(). Now calling *_p() functions one after another doesn't display random date and time. However calling first() or second() function before them shows random date and time for the first *_p() function while the functions the come after it doesn't, i.e, for cases 1) , 3) and 4) listed above.
However, can't free it as I need them somewhere else (why I had to use malloc in the first place). Is there a way to achieve it?
You've got quasi-random (indeterminate) values in the hours, minutes, seconds and daylight saving flag in the malloc() data, so you get indeterminate results back from mktime().
Use calloc() instead of malloc(), or zero the fields yourself.
Also, consider setting the tm_isdst member to -1 to let the system determine whether daylight saving or standard time was appropriate for the dates.
Environment: Ubuntu 14.04.
I am trying to store into struct tm broken down time specified in GMT. Here is my C++ function:
void mygmt(int year, int month, int day, int hour, int minute, int second) {
struct tm gmtTime;
memset(&gmtTime, 0, sizeof(struct tm));
gmtTime.tm_mday = day;
gmtTime.tm_mon = month - 1; // 0-based
gmtTime.tm_year = year - 1900;
gmtTime.tm_hour = hour;
gmtTime.tm_min = minute;
gmtTime.tm_sec = second;
gmtTime.tm_zone = "GMT";
char buffer [80];
strftime (buffer,80,"GMT time: %b %d, %G %I:%M:%S %p %Z.", &gmtTime);
puts(buffer);
time_t rawtime = timegm(&gmtTime);
struct tm* timeinfo = localtime (&rawtime);
strftime (buffer,80,"Local time: %b %d, %G %I:%M:%S %p %Z.",timeinfo);
puts(buffer);
}
Here's how I am calling it:
// June 11, 2016 23:34:03 (in GMT)
mygmt(2016, 6, 11, 23, 34, 3);
And here's the output:
GMT time: Jun 11, 2015 11:34:03 PM GMT.
Local time: Jun 11, 2016 04:34:03 PM PDT.
I expect local time to be -7 hours from GMT and that part of the output seems to be right. What I am confused about is the output for GMT time. It shows me a difference of one year from the value I specified. How do I fix it?
Before calling strftime, call timegm to fill tm-fields set to zero:
// ...
time_t rawtime = timegm(&gmtTime);
char buffer [80];
strftime (buffer,80,"GMT time: %b %d, %G %I:%M:%S %p %Z.", &gmtTime);
puts(buffer);
struct tm* timeinfo = localtime (&rawtime);
strftime (buffer,80,"Local time: %b %d, %G %I:%M:%S %p %Z.",timeinfo);
// ...
Note: You might consider %Y instead of %G (which is a bit complicated).
See also: std::mktime and timezone info and Easy way to convert a struct tm (expressed in UTC) to time_t type.
It's a simple question, but the solution appears to be far from simple. I would like to know how to convert from UTC to local time. I am looking for a solution in C that's standard and more or less guaranteed to work on any computer at any location.
I have read the following links carefully but I can't find a solution there:
Converting string containing localtime into UTC in C
Converting Between Local Times and GMT/UTC in C/C++
I have tried a number of variations, such as (datetime is a string with time and date in UTC):
strptime(datetime, "%A %B %d %Y %H %M %S", tp);
strftime(printtime, strlen(datetime), "%A %B %d %Y %H %M %S", tp);
Or
strptime(datetime, "%A %B %d %Y %H %M %S", tp);
lt=mktime(tp);
printtime=ctime(<);
No matter what I try printtime ends up being the same as UTC.
Edit 11-29-2013: based on the very helpful answer by "R" below I finally got around to create a working example. I found it to be working correct in the two timezones I tested it, CET and PST:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
long long diff_tm(struct tm *a, struct tm *b)
{
return a->tm_sec - b->tm_sec
+60LL*(a->tm_min - b->tm_min)
+3600LL*(a->tm_hour - b->tm_hour)
+86400LL*(a->tm_yday - b->tm_yday)
+(a->tm_year-70)*31536000LL
-(a->tm_year-69)/4*86400LL
+(a->tm_year-1)/100*86400LL
-(a->tm_year+299)/400*86400LL
-(b->tm_year-70)*31536000LL
+(b->tm_year-69)/4*86400LL
-(b->tm_year-1)/100*86400LL
+(b->tm_year+299)/400*86400LL;
}
int main()
{
time_t utc, local;
char buf[100];
const char datetime[]="2013 11 30 23 30 26 UTC"; /* hard coded date and time in UTC */
struct tm *tp=malloc(sizeof(struct tm));
if(tp==NULL)
exit(-1);
struct tm *localt=malloc(sizeof(struct tm));
if(localt==NULL)
exit(-1);
memset(tp, 0, sizeof(struct tm));
memset(localt, 0, sizeof(struct tm));
printf("UTC date and time to be converted in local time: %s\n", datetime);
/* put values of datetime into time structure *tp */
strptime(datetime, "%Y %m %d %H %M %S %z", tp);
/* get seconds since EPOCH for this time */
utc=mktime(tp);
printf("UTC date and time in seconds since EPOCH: %d\n", utc);
/* lets convert this UTC date and time to local date and time */
struct tm e0={ .tm_year = 70, .tm_mday = 1 }, e1, new;
/* get time_t EPOCH value for e0 (Jan. 1, 1970) */
time_t pseudo=mktime(&e0);
/* get gmtime for this value */
e1=*gmtime(&pseudo);
/* calculate local time in seconds since EPOCH */
e0.tm_sec += utc - diff_tm(&e1, &e0);
/* assign to local, this can all can be coded shorter but I attempted to increase clarity */
local=e0.tm_sec;
printf("local date and time in seconds since EPOCH: %d\n", local);
/* convert seconds since EPOCH for local time into localt time structure */
localt=localtime(&local);
/* get nicely formatted human readable time */
strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S %Z", localt);
printf("local date and time: %s\n", buf);
}
It should compile without problems on most systems. I hard coded a time and date in UTC which then will be converted to the local time and date.
If you can assume POSIX (and thus the POSIX specification of time_t as seconds since the epoch), I would first use the POSIX formula to convert to seconds since the epoch:
tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
(tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
Next, use localtime((time_t []){0}) to get a struct tm representing the epoch in local time. Add the seconds since the epoch to the tm_sec field of this struct tm, then call mktime to canonicalize it.
Edit: Actually the only POSIX dependency is having a known epoch which (time_t)0 corresponds to. Perhaps you can find a way around that if you really need to... for instance using calls to both gmtime and localtime at time_t 0..
Edit 2: A sketch of how to do this:
#include <time.h>
#include <stdio.h>
long long diff_tm(struct tm *a, struct tm *b)
{
return a->tm_sec - b->tm_sec
+60LL*(a->tm_min - b->tm_min)
+3600LL*(a->tm_hour - b->tm_hour)
+86400LL*(a->tm_yday - b->tm_yday)
+(a->tm_year-70)*31536000LL
-(a->tm_year-69)/4*86400LL
+(a->tm_year-1)/100*86400LL
-(a->tm_year+299)/400*86400LL
-(b->tm_year-70)*31536000LL
+(b->tm_year-69)/4*86400LL
-(b->tm_year-1)/100*86400LL
+(b->tm_year+299)/400*86400LL;
}
int main(int argc, char **argv)
{
char buf[100];
struct tm e0 = { .tm_year = 70, .tm_mday = 1 }, e1, new;
time_t pseudo = mktime(&e0);
e1 = *gmtime(&pseudo);
e0.tm_sec += atoi(argv[1]) - diff_tm(&e1, &e0);
mktime(&e0);
strftime(buf, sizeof buf, "%c", &e0);
puts(buf);
}
Please don't mind the ugly output code. This program takes an argument in the form of "seconds relative to the POSIX epoch" and outputs the resulting time in local time. You can convert any UTC time to seconds since the epoch using the formula I cited above. Note that this code does not in any way depend on POSIX, but it does assume the offset returned by diff_tm combined with the seconds-since-the-epoch value does not overflow int. A fix for this would be to use a long long offset and a loop that keeps adding increments no larger than INT_MAX/2 (or smaller than INT_MIN/2) and calling mktime to renormalize until the offset reaches 0.
Ahm ... I might just be a beginner in C, but I got this working example:
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t abs_ts,loc_ts,gmt_ts;
struct tm loc_time_info,gmt_time_info;
/*Absolute time stamp.*/
time(&abs_ts);
/*Now get once the local time for this time stamp,
**and once the GMT (UTC without summer time) time stamp.*/
localtime_r(&abs_ts,&loc_time_info);
gmtime_r(&abs_ts,&gmt_time_info);
/*Convert them back.*/
loc_ts=mktime(&loc_time_info);
gmt_ts=mktime(&gmt_time_info);
/*Unfortunately, GMT still has summer time. Get rid of it:*/
if(gmt_time_info.tm_isdst==1)
{gmt_ts-=3600;}
printf("Local timestamp: %lu\n"
"UTC timestamp: %lu\n"
"Difference in hours: %lu\n\n",
loc_ts,
gmt_ts,
(loc_ts-gmt_ts)/3600);
return 0;
}
Which produces this output:
Local timestamp: 1412554119
GMT timestamp: 1412546919
Difference in hours: 2
Now you have the difference between UTC and local time in seconds. That should be enough to convert it.
One note to your code, aseq: you are using malloc without need here (you can memset values on the stack as well, and malloc can be expensive while stack allocation is often much faster), and you do not free it. That's very, very bad practise.
Another thing:
memset(tp, 0, sizeof(struct tm));
Would be better done if you'd pass sizeof(*tp) (or, if you put tp on the stack, sizeof(tp)) to memset. That ensures that even if the type of your object changes, it will still be fully memset.
To sum-up: the conversion of a broken down date (struct tm) in UTC to a (local) calendar time (time_t) is achieved with timegm() - the opposite of mktime() - BUT timegm() is not a standard function (how logic is that).
The C standard leaves us with only time(), gmtime(), mktime() and difftime().
A workaround found in other docs advises to emulate timegm() by setting first the environment variable TZ to a null string, then calling mktime() resulting in an UTC calendar time, then resetting TZ to its initial value, but once again, this is not standard.
Basically, as I understand it, the difference between a local time and UTC time is just an offset so if we can evaluate that offset, we can adjust the result of mktime(), so here's my proposition:
time_t my_timegm(struct tm *tm) {
time_t epoch = 0;
time_t offset = mktime(gmtime(&epoch));
time_t utc = mktime(tm);
return difftime(utc, offset);
}
A quick test:
int main(void) {
time_t now = time(0);
struct tm local = *localtime(&now);
struct tm utc = *gmtime(&now);
time_t t1 = mktime(&local);
time_t t2 = my_timegm(&utc);
assert(t1 == t2);
printf("t =%lu\nt1=%lu\nt2=%lu\n",now,t1,t2);
return 0;
}
//working stand alone function adjusting UTC to local date and time
//globals(unsigned integers): gps.Mth, gps.Yr, gps.Hm (eg:2115 for 21:15)
//adjust date and time according to UTC
//tz(timezone) eg: 1100, for 11 hours, tzdir: 1 forward, 0 backwards
void AdjustUTCToTimeZone(u16 tz, u8 tzdir){
u8 maxDayInAnyMonth[13] = {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //gps.Mth 1-12 (not zero)
if(gps.Yr%4==0){maxDayInAnyMonth[2]=29;}//adjust for leapyear
u8 maxDayUtcMth =maxDayInAnyMonth[gps.Mth];
u8 maxDayPrevMth=maxDayInAnyMonth[gps.Mth-1];
if(!maxDayPrevMth){maxDayPrevMth=31;} //month before utc month
u16 hr=(gps.Hm/100)*100;u16 m=gps.Hm-hr; //2115 --> 2100 hr and 15 min
if(tzdir){//adjusting forwards
tz+=gps.Hm;
if(tz>2400){gps.Hm=tz-2400;gps.Day++; //spill over to next day
if(gps.Day>maxDayUtcMth){ gps.Day=1;gps.Mth++; //spill over to next month
if(gps.Mth>12){gps.Mth=1; gps.Yr++; //spill over to next year
}
}
}else{gps.Hm=tz;}
}else{//adjusting backwards
if(tz>gps.Hm){gps.Hm=(2400-(tz-hr))+m;gps.Day--; // back to previous day
if(gps.Day==0){ //back to previous month
gps.Mth--;gps.Day=maxDayPrevMth;
if(!gps.Mth){gps.Mth=12; //back to previous year
gps.Yr--;
}
}
}else{gps.Hm-=tz;}
}
}
I think it's easier than that; time.h defines three variables:
extern int daylight;
extern long timezone;
extern char *tzname[];
which are loaded based on the TZ env variable when you call
tzset();
if you have a utc time in
struct tm date;
date.tm_isdst = 0;
convert it to a time_t using mktime
time_t utc = mktime( &date );
then convert it to local time
time_t local = utc - timezone + ( daylight?3600:0 );
timezone is the number of seconds away from utc for the current timezone and daylight is 1 to indicate daylight savings time is in play and zero for not.
A small caution: When I coded this for a microcontroller and cross compiled, it's time.h defined those variables with initial underscores.
See the man page for time.h
I found that the solution the OP gave did not work in cases when DST applies. For example, in my case, at the current time, DST was not in effect, but if I set the initial date which should convert to local time with DST, then it would not work, i.e. today's date is 3/1/2018 and DST is not in effect, but if I set the date for conversion to, say, 8/1/2018 0:00:00 when DST is in effect, then the solution given would convert to local time, but would not take DST into account. I found that initializing e0 to the date and hour of the initial date/time string and its member tm_isdst to -1 solved the problem. I then created the following program with complementary functions which you can include in your code. The initial format of the date and time is the same that MySQL uses, because I needed it for such purposes.
#include <stdio.h>
#include <time.h>
#include <string.h>
long long diff_tm(struct tm *a, struct tm *b) {
return a->tm_sec - b->tm_sec
+ 60LL * (a->tm_min - b->tm_min)
+ 3600LL * (a->tm_hour - b->tm_hour)
+ 86400LL * (a->tm_yday - b->tm_yday)
+ (a->tm_year - 70) * 31536000LL
- (a->tm_year - 69) / 4 * 86400LL
+ (a->tm_year - 1) / 100 * 86400LL
- (a->tm_year + 299) / 400 * 86400LL
- (b->tm_year - 70) * 31536000LL
+ (b->tm_year - 69) / 4 * 86400LL
- (b->tm_year - 1) / 100 * 86400LL
+ (b->tm_year + 299) /400 * 86400LL;
}
void localToUTC(char *buf, const char *localTime) {
struct tm tp;
strptime(localTime, "%Y-%m-%d %H:%M:%S", &tp);
tp.tm_isdst = -1;
time_t utc = mktime(&tp);
struct tm res = *gmtime(&utc);
strftime(buf, 20, "%Y-%m-%d %H:%M:%S", &res);
}
void utcToLocal(char *buf, const char *utcTime) {
struct tm tp;
strptime(utcTime, "%Y-%m-%d %H:%M:%S", &tp);
tp.tm_isdst = -1;
time_t utc = mktime(&tp);
struct tm e0 = { .tm_year = tp.tm_year, .tm_mday = tp.tm_mday, .tm_mon = tp.tm_mon, .tm_hour = tp.tm_hour, .tm_isdst = -1 };
time_t pseudo = mktime(&e0);
struct tm e1 = *gmtime(&pseudo);
e0.tm_sec += utc - diff_tm(&e1, &e0);
time_t local = e0.tm_sec;
struct tm localt = *localtime(&local);
strftime(buf, 20, "%Y-%m-%d %H:%M:%S", &localt);
}
int main(void) {
char mytime_1[20] = "2018-02-28 13:00:00";
char utctime_1[20], back_1[20];
localToUTC(utctime_1, mytime_1);
utcToLocal(back_1, utctime_1);
printf("My time: %s\n", mytime_1);
printf("UTC time: %s\n", utctime_1);
printf("Back: %s\n", back_1);
printf("-------------------------------------------\n");
char mytime_2[20] = "2018-07-28 17:00:00";
char utctime_2[20], back_2[20];
localToUTC(utctime_2, mytime_2);
utcToLocal(back_2, utctime_2);
printf("My time: %s\n", mytime_2);
printf("UTC time: %s\n", utctime_2);
printf("Back: %s\n", back_2);
printf("-------------------------------------------\n");
return 0;
}
I followed the answer by #Dachschaden and I made an example which also shows human-readable output and I remove the DST option for the difference in seconds between UTC and local time. Here it is:
#include <time.h>
#include <stdio.h>
#define DATE_MAX_STR_SIZE 26
#define DATE_FMT "%FT%TZ%z"
int main() {
time_t now_time, now_time_local;
struct tm now_tm_utc, now_tm_local;
char str_utc[DATE_MAX_STR_SIZE];
char str_local[DATE_MAX_STR_SIZE];
time(&now_time);
gmtime_r(&now_time, &now_tm_utc);
localtime_r(&now_time, &now_tm_local);
/* human readable */
strftime(str_utc, DATE_MAX_STR_SIZE, DATE_FMT, &now_tm_utc);
strftime(str_local, DATE_MAX_STR_SIZE, DATE_FMT, &now_tm_local);
printf("\nUTC: %s", str_utc);
printf("\nLOCAL: %s\n", str_local);
/* seconds (epoch) */
/* let's forget about DST for time difference calculation */
now_tm_local.tm_isdst = 0;
now_tm_utc.tm_isdst = 0;
now_time_local = now_time + (mktime(&now_tm_local) - mktime(&now_tm_utc));
printf("\nUTC in seconds: %lu", now_time);
printf("\nLOCAL in seconds: %lu\n", now_time_local);
return 0;
}
Output on my machine is:
UTC: 2016-05-05T15:39:11Z-0500
LOCAL: 2016-05-05T11:39:11Z-0400
UTC in seconds: 1462462751
LOCAL in seconds: 1462448351
Note that DST is on in this case (there's a 1 hour time zone offset difference between UTC and LOCAL).
try this, test output:
utcEpochTime: 1487652688, localEpochTime: 1487699488, diff: 46800
$ python
>>>46800 / 60 / 60
13
the diff is 13 hours, which is good, as my timezone is UTC+8.
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[])
{
time_t utcEpochTime = time(0);
time_t localEpochTime = 0;
struct tm tm = {0};
localtime_r(&utcEpochTime, &tm);
tm.tm_isdst = -1;
localEpochTime = timegm(&tm);
printf("utcEpochTime: %d, localEpochTime: %d, diff: %d\n", (int)utcEpochTime, (int)localEpochTime, (int)(localEpochTime - utcEpochTime));
return 0;
}
A simple and effective way: Add (or subtract) the number of seconds between your time zone and UTC (considering daylight saving time).
As an example that worked just fine a minute ago, on December 30, 2017, with U.S. Mountain Standard Time (no DST), which is 7 hours behind UTC:
time_t current_time_UTC;
time_t current_time_MST;
struct tm *current_broken_time_MST;
uint32_t seven_hours_in_seconds = 25200; // Get this any way you want
current_time_UTC = time (NULL); // UTC
current_time_MST = current_time_UTC - seven_hours_in_seconds; // MST
current_broken_time_MST = localtime (¤t_time_MST); // MST
Enjoy.
void CTestDlg::OnBtnTest()
{
HANDLE hFile;
WIN32_FIND_DATA wfd;
SYSTEMTIME systime;
FILETIME localtime;
char stime[32]; //
memset(&wfd, 0, sizeof(wfd));
if((hFile=FindFirstFile( "F:\\VC\\MFC\\Test\\Release\\Test.exe ", &wfd))==INVALID_HANDLE_VALUE)
{
char c[2];
DWORD dw=GetLastError();
wsprintf(c, "%d ", dw);
AfxMessageBox(c);
return ;//
}
FileTimeToLocalFileTime(&wfd.ftLastWriteTime,&localtime);
FileTimeToSystemTime(&localtime,&systime);
sprintf(stime, "%4d-%02d-%02d %02d:%02d:%02d ",
systime.wYear,systime.wMonth,systime.wDay,systime.wHour,
systime.wMinute,systime.wSecond);
AfxMessageBox(stime);
}