LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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, 10: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-14-2004 at 12:21 AM.
 
Old 09-13-2004, 10:57 AM   #2
dannybpng
Member
 
Registered: Sep 2003
Location: USA
Distribution: Fedora 20
Posts: 60

Rep: Reputation: 19
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, 06: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, 08:29 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,666
Blog Entries: 54

Rep: Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952
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, 09:46 PM   #5
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora Core 6 XEN
Posts: 202
Blog Entries: 1

Rep: Reputation: 36
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, 09:58 PM   #6
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,199

Rep: Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136
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, 10:17 PM   #7
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora Core 6 XEN
Posts: 202
Blog Entries: 1

Rep: Reputation: 36
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, 10:47 PM   #8
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,199

Rep: Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136
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, 08:39 AM   #9
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,441
Blog Entries: 4

Rep: Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503Reputation: 1503
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, 03: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, 02:59 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,305

Rep: Reputation: 175Reputation: 175
"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, 09:30 PM   #12
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora Core 6 XEN
Posts: 202
Blog Entries: 1

Rep: Reputation: 36
[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, 09:56 PM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
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 09:57 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 12:20 PM
Display the date and time from another time zone lothario Linux - Software 4 07-31-2004 04:27 PM
Setting System Time: kernel in wrong time zone warrenweiss Linux - General 7 05-15-2004 04:25 PM
Changing time zone... tinaa Linux - General 3 07-11-2003 07:07 AM
time zone exigent Linux - General 4 09-17-2002 04:41 PM


All times are GMT -5. The time now is 01:19 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration