-2

I am using Ubuntu 10.04 and set the system timezone "Europe/London". I want to get system timezone set in above format only not "IST,EST etc.". Is there anyway i can get the timezone string in same format we set like "Europe/London".

As above link is given for duplication but actually it doesn't answer my question. Before posting this question i have checked the above link.

Please suggest.

Thanks in Advance.

Neel Patel
  • 315
  • 2
  • 6
  • 17
  • check this out: http://www.boost.org/doc/libs/1_54_0/doc/html/date_time.html but I'm not sure if there is such a thing. – Dino Aug 19 '13 at 10:40
  • 1
    See [How do I find the current system timezone?](http://stackoverflow.com/questions/3118582/how-do-i-find-the-current-system-timezone?rq=1) – elmov Aug 19 '13 at 10:55
  • Thanks for update. I have checked the above link but in the link there is no proper solution i found that'w why i have asked this as second time. Is anyone has any idea than please share it. – Neel Patel Aug 19 '13 at 12:15

2 Answers2

1

time.h defines a function to retrieve the system timezone and save it in two string variables:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main() {
    char * TZ = getenv("TZ");
    if (TZ)
        printf("TZ: %s\n", TZ);
    tzset();
    printf("Timezone: %s/%s\n", tzname[0], tzname[1]);
    printf("GMT %s%ld\n", timezone >= 0 ? "+" : "", timezone);
    printf("DST %s\n", daylight ? "ON" : "OFF");
}

Please note that on some systems (e.g. Linux) this function retrieves it's information from the TZ environment variable. If it is not set then an approximation will be given or the variables will remain uninitialised.

On my OS X this info seems to come from somewhere else.

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • I ran your program and it gave me this. amrith@amrith-vbox:~/so$ ./tz Timezone: EST/EDT GMT +18000 DST ON amrith@amrith-vbox:~/so$ Not what the question asked for, I don't think ... – amrith Aug 19 '13 at 11:31
  • Yes above program will not give the output that asked for. Output should come in string like "Asia/Calcutta" or "Europe/London". – Neel Patel Aug 19 '13 at 12:12
-1

Why not just cat /etc/timezone?

amrith@amrith-vbox:~/so$ more /etc/timezone
America/New_York
amrith
  • 953
  • 6
  • 17
  • Thanks. As i know /etc/timezone will not exists for all types of linux OS ( Red hat, fedora, cent os, ubuntu etc. ) so i think it is not generic solution. – Neel Patel Aug 19 '13 at 12:05
  • Really? Should I remind you that your question reads, "I am using Ubuntu 10.04 and ...". – amrith Sep 13 '13 at 00:48