How to get Time Zone information from the system
Hi All,
Please let me know how to get the Time Zone information from the system, In windows platform we use Windows API for the same. Please let me know if any Linux API is available for the same. I have a C++ Class through which I want to get TimeZone Information of the system. Please let me know how can I proceed Thanks in Advance |
The date command can give you the timezone name or offset from GMT.
From the terminal command prompt type: date +%Z # timezone name date +%z # GMT offset (non-standard) Other computer languages will have calls to the OS to get this information. Dan |
Displaying Time Zone Info
You can also use
#cat /etc/sysconfig/clock ZONE="Europe/Paris" UTC=true ARC=false |
Quote:
|
Is there really a standard (UNIX/LINUX/BSD) standard for TZ
TZ environment variable. One standard, not in Fedora, is the TZ variable.
Format TZ "EST5EDT,M3,2,0,M11.1.0" I found http://www.gnu.org/software/libc/man...-Variable.html I did not find code to automatically set up this variable, Functions in time.h do explain in a way, how to go about it. Since I needed a system that was current between Unix (all) and Linux(all) and BSD(all), I wrote a quick and simple bit of code that provides me the number of hours of difference, I had to write two lines to a file, One being UTC, and the other, localtime, and subtract one from the other. I also had to accomodate midnight crossing. Here is the snippit or code. int getTimeOffset() { FILE *file; int hr1, hr2, rc; char buffer[64]; system ("date +%H >/tmp/TZ"); system ("date -u +%H >>/tmp/TZ"); file = fopen ("/tmp/TZ", "rb"); fgets (buffer, sizeof(buffer), file); hr1 = atoi(buffer); fgets (buffer, sizeof(buffer), file); hr2 = atoi (buffer); rc = fclose (file); rc = unlink ("/tmp/TZ"); /* printf("H1=%d,H2=%d\n",hr1,hr2); */ if (hr2 > hr1) return (hr2 - hr1); else hr1 = -((24 - hr1) + hr2); return (hr1); } /* end of file */ |
Quote:
Quote:
|
I looked at the time.h stuff, and it may be the way to go. The above took me all of 10 minutes,
I needed the value, once per execution. I am scanning a full directory tree, and the time shown when you do ls -l is UTC with offset of (4 * 3600) seconds. Its also very short. (grin) Can you post your solution? |
There is probably a better way to do this, and note that I borrowed your code to handle midnight crossing (but simplified it a bit):
Code:
#include <time.h> |
Moderator Response
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
|
Keep it simple
Quote:
time_t currentTime = time(NULL); struct tm *localTime; localTime = localtime(¤tTime); return localTime->tm_gmtoff; |
"If you can't explain it simply, you don't understand it well enough."
-- Albert Einstein the kernel should be set to GMT time is relative to the observer. Code:
$ date |
[leslie@Fedora20 ~]$ date
Fri Apr 4 20:29:04 CDT 2014 [leslie@Fedora20 ~]$ date -u Sat Apr 5 01:29:08 UTC 2014 [leslie@Fedora20 ~]$ I reside at gmt-5 so 20:30 is actually 1:30 gmt time |
Quote:
|
In continuation of...
https://www.linuxquestions.org/quest...4/#post4762042 Converting your current timezone, or the time zone set using TZ environment variable. Timezone as text Code:
date +%Z Code:
date +%z Timezone as offset to GMT (add this to convert seconds since epoch, to GMT) Code:
date +%s -d '1 Jan 1970' |
All times are GMT -5. The time now is 06:41 AM. |