[SOLVED] Function for conversion of GMT to Unix epoch
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How about declaring extern long timezone and then adjusting t_of_day by subtracting adding the value in timezone?
edit: in fact, it looks like time.h defines it already so you don't even need to declare.
edot2: actually, mktime() will return a t_of_day that will have the assumed timezone subtracted, so you need to put it back on if the original values were already specified in GMT0, so adding, not subtracting when you do it this way around.
Something like this maybe?
Code:
#include <time.h>
// extern long timezone;
time_t mktimegm(struct tm *tm_gmt)
{
time_t t;
t = mktime(tm_gmt);
return ( t == -1 ) ? t : t + timezone ;
}
edit3: Ah nutz! Apparently mktime() will still adjust for DST, even if you set isdst to 0 before calling, so looks like this is too simplistic and one also needs to adjust for dst. Maybe temporarily setting the timezone to GMT was the better idea after all. Or, just write your own routines like Neve' did.
cannot thank enough @NevemTeve , looks like this is what i need , have to do some tests with different dates.
looks like you are calculating exact number days that are elapsed from 1900 and then multiplying with 24*60*60.
Well i dont get some part of the code, what is the purpose of this part?
Code:
// is it number of days in nonleap years ??
ytmp = year - 1601;
dtmp = ytmp*365 + ytmp/4 - ytmp/100 + ytmp/400;
// is it number of days in leap years ??
ytmpe = 1970 - 1601;
dtmpe = ytmpe*365 + ytmpe/4 - ytmpe/100 + ytmpe/400; /* = 134685 + 92 - 3 + 0 = 134774 */
If I remember correctly (file dated 2018-07-24), this program uses 1601-01-01 as base, as it is the start of an 400-year period of leap-years, so the 'days passed before the actual year (since the base)' can be calculated as (year*365)+(year/4)-(year/100)+(year/400).
Then this has to be converted to the unix-epoch, that's where the number of days between 1601-01-01 and 1970-01-01 matters (dtmpe=134774)
If I remember correctly (file dated 2018-07-24), this program uses 1601-01-01 as base, as it is the start of an 400-year period of leap-years, so the 'days passed before the actual year (since the base)' can be calculated as (year*365)+(year/4)-(year/100)+(year/400).
Then this has to be converted to the unix-epoch, that's where the number of days between 1601-01-01 and 1970-01-01 matters (dtmpe=134774)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.