Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » CalendarCtrl » How to get TimeZone
How to get TimeZone [message #54154] Thu, 04 June 2020 16:07 Go to next message
forlano is currently offline  forlano
Messages: 1105
Registered: March 2006
Location: Italy
Experienced Contributor
Hello,

do we have a way to get the TimeZone from the local running computer?
Otherwise I must ask the user to input it.

Thanks,
Luigi
Re: How to get TimeZone [message #54164 is a reply to message #54154] Thu, 04 June 2020 20:33 Go to previous message
forlano is currently offline  forlano
Messages: 1105
Registered: March 2006
Location: Italy
Experienced Contributor
I needed that function to get the UTC offset. Perhaps I could have better formulated the question.
Online I found this program that does the trick (tested in Windows with CLANG)

#include <iostream>

int time_offset()
{   // reference:
    // https://stackoverflow.com/questions/13804095/get-the-time-zone-gmt-offset-in-c
    time_t gmt, rawtime = time(NULL);
    struct tm *ptm;

#if !defined(WIN32)
    struct tm gbuf;
    ptm = gmtime_r(&rawtime, &gbuf);
#else
    ptm = gmtime(&rawtime);
#endif
    // Request that mktime() looksup dst in timezone database
    ptm->tm_isdst = -1;
    gmt = mktime(ptm);

    return (int)difftime(rawtime, gmt);
}

int main(int argc, const char *argv[])
{
	printf("UTC offset = %d\n", time_offset()/3600);
}


In my case (Italy) it gives the correct answer.
It seems those function automatically take into account the DST Rolling Eyes
I hope to not be in a case where a hour is missing.

Luigi
Previous Topic: DropTime without calendar
Goto Forum:
  


Current Time: Wed Jul 15 03:59:52 CEST 2020

Total time taken to generate the page: 0.01940 seconds