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: 1182
Registered: March 2006
Location: Italy
Senior 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: 1182
Registered: March 2006
Location: Italy
Senior 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 and DropDate format
Next Topic: DropTime without calendar
Goto Forum:
  


Current Time: Thu Mar 28 17:55:33 CET 2024

Total time taken to generate the page: 0.01122 seconds