Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does _get_timezone work?

Tags:

c++

c

time

I'm trying to correct a timestamp class that's acting up really weird. What it has to do is to get the current time upon construction or the time passed as a string.

I only want to work with UTC as these dates are not really displayed anywhere but the difference between one timestamp and another is.

In any case, I call the strptime(timestamp, m_format.c_str(), &time); to get the tm time from a string. This string is UTC time so if I want to call mktime to get time as time_t I have to modify it with the current timezone so I do this time.tm_hour-=tz/3600 where tz is retrieved by calling _get_timezone. The problem is that this function seems to return 28800 which is the default value but if I call it later in the code it will give me -7200 which is the right value.

Do I have to do anything to get this value initialized? We have a big codebase and I don't know everything that happens in between.

Furthermore if anyone has a link to a similar class I'd really appreciate it as I'd probably notice my mistake there.

Thank you.

like image 249
Sanctus2099 Avatar asked Aug 15 '26 15:08

Sanctus2099


1 Answers

I would define my own function time_offset as follows

#include <time.h>
double time_offset()
{
    time_t now = time(NULL);

    struct tm *gm = gmtime(&now);
    time_t gmt = mktime(gm);

    struct tm *loc = localtime(&now);
    time_t local = mktime(loc);

    return difftime(local, gmt);
}

This was taken from here Hope this helps

like image 148
Triclops200 Avatar answered Aug 18 '26 05:08

Triclops200



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!