LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to get Time Zone information from the system (https://www.linuxquestions.org/questions/programming-9/how-to-get-time-zone-information-from-the-system-230064/)

chowdhary_g 09-13-2004 09:35 AM

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

dannybpng 09-13-2004 09:57 AM

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

leprofessionnel 08-23-2012 05:59 AM

Displaying Time Zone Info
 
You can also use


#cat /etc/sysconfig/clock

ZONE="Europe/Paris"
UTC=true
ARC=false

unSpawn 08-23-2012 07:29 AM

Quote:

Originally Posted by chowdhary_g (Post 1172481)
In windows platform we use Windows API for the same.
Please let me know if any Linux API is available for the same.

See 'man 2 gettimeofday'?

Lsatenstein 10-24-2013 08:46 PM

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 */

suicidaleggroll 10-24-2013 08:58 PM

Quote:

Originally Posted by Lsatenstein (Post 5051843)
TZ environment variable. One standard, not in Fedora, is the TZ variable.

Then it's not exactly a standard, is it?


Quote:

Originally Posted by Lsatenstein (Post 5051843)
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 */

That just seems like an incredibly inefficient and roundabout alternative to just using the time() function.

Lsatenstein 10-24-2013 09:17 PM

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?

suicidaleggroll 10-24-2013 09:47 PM

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>
#include <stdio.h>

int main(){
  time_t currtime = time(NULL);
  struct tm * ptm;
  int uthr,lthr,offset;
  ptm = gmtime(&currtime);
  uthr = ptm->tm_hour;
  ptm = localtime(&currtime);
  lthr = ptm->tm_hour;
  if (uthr>lthr){
      offset = uthr-lthr;
  }else{
      offset = lthr-uthr-24;
  }
  printf("%d\n",offset);
}


onebuck 10-25-2013 07:39 AM

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.

siul72 04-03-2014 02:30 AM

Keep it simple
 
Quote:

Originally Posted by suicidaleggroll (Post 5051857)
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>
#include <stdio.h>

int main(){
  time_t currtime = time(NULL);
  struct tm * ptm;
  int uthr,lthr,offset;
  ptm = gmtime(&currtime);
  uthr = ptm->tm_hour;
  ptm = localtime(&currtime);
  lthr = ptm->tm_hour;
  if (uthr>lthr){
      offset = uthr-lthr;
  }else{
      offset = lthr-uthr-24;
  }
  printf("%d\n",offset);
}


Don't need to compare, use gmt offset from the tm stucture:

time_t currentTime = time(NULL);
struct tm *localTime;
localTime = localtime(&currentTime);

return localTime->tm_gmtoff;

bigearsbilly 04-04-2014 01:59 AM

"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
Fri Apr  4 06:52:38 UTC 2014

$ export TZ=Europe/London
$ date
Fri Apr  4 07:52:53 BST 2014

$ export TZ=America/Chicago
$ date
Fri Apr  4 01:53:16 CDT 2014

$ export TZ=Asia/Calcutta
$ date
Fri Apr  4 12:26:29 IST 2014
$


Lsatenstein 04-04-2014 08:30 PM

[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

jlliagre 04-04-2014 08:56 PM

Quote:

Originally Posted by suicidaleggroll (Post 5051846)

> Originally Posted by Lsatenstein
> TZ environment variable. One standard, not in Fedora, is the TZ variable.


Then it's not exactly a standard, is it?

Actually TZ is a standard, a POSIX one precisely. I'm not sure why Lsatenstein is stating Fedora doesn't support it.

A.Thyssen 02-14-2018 11:15 PM

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
Timezone as GMT offset
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.