[Interest] Getting the timezone from Qt?

Nikos Chantziaras realnc at gmail.com
Tue Jul 31 05:28:47 CEST 2012


On 30/07/12 10:36, Konrad Rosenbaum wrote:
> On Sunday 29 July 2012 22:30:32 Nikos Chantziaras wrote:
>> [...] I'm looking for /etc/timezone and
>> /etc/localtime.  In case the latter is a symlink, it's possible to get
>> the timezone from the path string.
>
> You are right of course - I should remember to read my own code properly.
> However, /etc/localtime is usually not a symlink. If you are missing
> /etc/timezone, you can also try /usr/local/etc/timezone (that's the default
> location where the timezone library installs itself on a system that does not
> have it per default).

I wonder if it's possible to deduce the timezone from the system's UTC 
offset?  The offset can be determined through gmtime() and localtime(), 
both in C89 so nothing platform-specific here:

   #include <iostream>
   #include <ctime>

   int main ()
   {
       time_t t;
       tm utc_tm;
       tm local_tm;
       tm* tmp;

       time(&t);

       // Get current local time.
       local_tm = *localtime(&t);

       // Get current UTC time.
       utc_tm = *gmtime(&t);

       std::cout << "Local: " << asctime(&local_tm);
       std::cout << "UTC: " << asctime(&utc_tm);
   }

Here, this prints:

   Local: Sun Jun 24 12:32:39 2012
   UTC: Sun Jun 24 09:32:39 2012

That's a +3:00 offset.




More information about the Interest mailing list