How can I convert IANA time zone name to UTC offset at present in Ubuntu C/C++ -


in python or java can utc offset (at present time) given iana name of timezone ("america/los_angeles" instance). see get utc offset time zone name in python example.

how can same using c/c++ on ubuntu 14.04?

edit: preferably in thread-safe way (no environment variables).

you alluded fact, it's important note offset between utc , time in time zone not constant. if time zone performs daylight saving (summer) time adjustments, offset vary depending on time of year.

one way find offset take time you're interested in, hand localtime() function, @ tm_gmtoff field. tz environment variable set zone name you're interested in. here's example current time:

#include <time.h> #include <stdio.h>  int main() {     setenv("tz", "america/los_angeles", 1);     time_t t = time(null);     struct tm *tmp = localtime(&t);     printf("%ld\n", tmp->tm_gmtoff); } 

at moment prints -25200, indicating los angeles 25200 seconds, or 420 minutes, or 7 hours west of greenwich. next week (actually tomorrow) u.s goes off of dst, @ point code start printing -28800.

this isn't guaranteed work, since tm_gmtoff field not portable. believe versions of linux have it. (you might have compile -d_bsd_source or something, or refer field __tm_gmtoff. in experience tends work default, plain tm_gmtoff.)

the other way go , forth gmtime , mktime, described in sam varshavchik's answer.


addendum: asked not using environment variables. there way, unfortunately it's less standard. there bsd functions tzalloc , localtime_rz job, not exist on linux. here's how code looks:

    timezone_t tz = tzalloc("america/los_angeles");     if(tz == null) return 1;     time_t t = time(null);     struct tm tm, *tmp = localtime_rz(tz, &t, &tm);     printf("%ld\n", tmp->tm_gmtoff); 

for me prints -28800 (because pdt fell pst few minutes ago).

if had it, use localtime_rz along mktime in sam varshavchik's answer. , of course howard hinnant's library pretty thread-safe, not requiring mucking tz @ all.

edit (op): code localtime_rz , tzalloc can downloaded https://www.iana.org/time-zones , works on ubuntu 14.04.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -