LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2004, 09:35 AM   #1
chowdhary_g
LQ Newbie
 
Registered: Sep 2004
Posts: 1

Rep: Reputation: 0
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

Last edited by chowdhary_g; 09-13-2004 at 11:21 PM.
 
Old 09-13-2004, 09:57 AM   #2
dannybpng
Member
 
Registered: Sep 2003
Location: USA
Distribution: Fedora 35
Posts: 79

Rep: Reputation: 22
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
 
Old 08-23-2012, 05:59 AM   #3
leprofessionnel
LQ Newbie
 
Registered: Dec 2007
Posts: 1

Rep: Reputation: 0
Displaying Time Zone Info

You can also use


#cat /etc/sysconfig/clock

ZONE="Europe/Paris"
UTC=true
ARC=false
 
Old 08-23-2012, 07:29 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by chowdhary_g View Post
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'?
 
Old 10-24-2013, 08:46 PM   #5
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
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 */
 
Old 10-24-2013, 08:58 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by Lsatenstein View Post
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 View Post
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.
 
Old 10-24-2013, 09:17 PM   #7
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
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?
 
Old 10-24-2013, 09:47 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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);
}
 
Old 10-25-2013, 07:39 AM   #9
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
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.
 
Old 04-03-2014, 02:30 AM   #10
siul72
LQ Newbie
 
Registered: Apr 2014
Posts: 1

Rep: Reputation: Disabled
Keep it simple

Quote:
Originally Posted by suicidaleggroll View Post
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;
 
Old 04-04-2014, 01:59 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
"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
$
 
Old 04-04-2014, 08:30 PM   #12
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
[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
 
Old 04-04-2014, 08:56 PM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by suicidaleggroll View Post

> 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.

Last edited by jlliagre; 04-04-2014 at 08:57 PM.
 
Old 02-14-2018, 11:15 PM   #14
A.Thyssen
Member
 
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 158

Rep: Reputation: 44
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'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Time Zone cherukuri_sr Linux - Newbie 2 02-02-2005 11:20 AM
Display the date and time from another time zone lothario Linux - Software 4 07-31-2004 03:27 PM
Setting System Time: kernel in wrong time zone warrenweiss Linux - General 7 05-15-2004 03:25 PM
Changing time zone... tinaa Linux - General 3 07-11-2003 06:07 AM
time zone exigent Linux - General 4 09-17-2002 03:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration