Computing.Net > Forums > Linux > Time Zones in Linux

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Time Zones in Linux

Reply to Message Icon

Name: skaa
Date: June 25, 2004 at 09:49:31 Pacific
OS: Linux Mandrake
CPU/Ram: AMD, 256
Comment:

How to calculate the difference between Central and Mountain times in Linux, not installing time zone on my computer?

!



Sponsored Link
Ads by Google

Response Number 1
Name: Wolfbone
Date: June 26, 2004 at 17:48:28 Pacific
Reply:

It is not clear what you want: the difference between (American) CST and MST is a constant (1 hour). There is ambiguity in saying "Central and Mountain" times because they depend on which country and city you are in and also on whether or not daylight saving time is in effect.

If you just want to know what the date and time are in different places, do something like this (in a bash shell):

( TZ="America/Phoenix" date )
( TZ="America/Swift_Current" date)


0

Response Number 2
Name: skaa
Date: June 28, 2004 at 09:32:10 Pacific
Reply:

Thanx a lot! You helped me.


0

Response Number 3
Name: skaa
Date: June 28, 2004 at 13:50:19 Pacific
Reply:

Another question... How can I use this from my program?
Thank you.


0

Response Number 4
Name: Wolfbone
Date: June 28, 2004 at 16:33:22 Pacific
Reply:

If you mean a script, just put the results into variables:

phoenix=$( TZ="America/Phoenix" date )
swiftcurrent=$( TZ="America/Swift_Current" date)

You can use different formats for the 'date' command; date '+%k' gives just the hour (see the full documentation with 'info date'). If you used that format in the above two variable assignments, you could then do:

diff=$((swiftcurrent - phoenix))
echo "Phoenix is $diff hours behind Swift Current"

...which would make a correct statement most of the time.

You can find all the places whose time zones are known to your system in the file: /usr/share/zoneinfo/zone.tab. See for example the script /usr/bin/tzselect which comes with glibc for how to use awk to extract information from the file.


0

Response Number 5
Name: skaa
Date: June 29, 2004 at 06:42:09 Pacific
Reply:

Actually, I need to do this in C++. It is strange there are no functoins for working with timezones... Maybe, I should try to use pipes to direct shell answers to my program... I shall try.
Thank you.


0

Related Posts

See More



Response Number 6
Name: Wolfbone
Date: June 29, 2004 at 08:16:15 Pacific
Reply:

What do you mean "no functions for working with timezones"?

'info Libc Calendar'

You can still use glibc functions in a C++ programme!



0

Response Number 7
Name: skaa
Date: June 29, 2004 at 08:27:49 Pacific
Reply:

OK!!! I found. It can be done this way:

FILE *fp;
char so[4096];

fp=popen("TZ=\"Europe/Moscow\" date","r");
if(fp!=NULL)
{
fgets(so,80,fp);
pclose(fp);
}

Thank you.


0

Response Number 8
Name: Wolfbone
Date: June 30, 2004 at 02:06:06 Pacific
Reply:

Fair enough but it is just as easy to use the library functions:

time_t now;
struct tm *now_tm;
putenv("TZ=Europe/Moscow");
now=time(NULL);
now_tm=localtime(&now));

Now you've got access to the members of now_tm which have all been set to Moscow local values and also the extern tzname string which localtime() sets to the timezone name (%Z in the example below).

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#define SIZE 256
int main (int argc, char *argv[]) {
time_t now;
struct tm *now_tm;
char tzsave[64]="TZ";
char buff[SIZE];
if (getenv("TZ") != (char *) NULL) {
strcat(tzsave,"=");
strcat(tzsave,getenv("TZ"));
}
if (argv[1] != (char *) NULL) setenv("TZ",argv[1],1);

now=time(NULL);
now_tm=localtime(&now);
strftime(buff,SIZE,"%A, %B %d %Y: %r %Z\n",now_tm);
fputs(buff,stdout);
putenv(tzsave);
}



0

Response Number 9
Name: pkn123
Date: July 14, 2004 at 05:54:54 Pacific
Reply:


Hi ,

I am using setenv("TZ", zone, 1); ,to get the a time of that zone .But i am facing memory leak ,even if i use unsetenv("TZ").
I am going in a loop and giving diffenrent time zones .

How to avoid this ?

Thanks
Kannan


0

Response Number 10
Name: Wolfbone
Date: July 14, 2004 at 08:15:05 Pacific
Reply:

Without seeing the code, I couldn't say exactly - it depends on how you are dealing with the strings. A trivial modification of the previous programme to do something similar to what you are describing would be:

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#define SIZE 256
int main (int argc, char *argv[]) {

time_t now;
struct tm *now_tm;
char tzsave[64]="TZ";
char buff[SIZE];
int i=1;

if (argv[1] == (char *) NULL) {
fprintf(stderr,"Usage: %s timezone1 [timezone2] ...\n",argv[0]);
return EXIT_FAILURE;
}

if (getenv("TZ") != (char *) NULL) {
strcat(tzsave,"=");
strcat(tzsave,getenv("TZ"));
}

now=time(NULL);

while (argv[i] != (char *) NULL) {
setenv("TZ",argv[i],1);
now_tm=localtime(&now);
strftime(buff,SIZE,"%a. %B %d %Y, %r %Z\t",now_tm);
strcat(buff," [");
strcat(buff,argv[i]);
strcat(buff,"]\n");
fputs(buff,stdout);
i++;
}
putenv(tzsave);
}

Of course the string array argv[] is automatically large enough to hold all the timezone strings (up to the maximum length of the command line) but if you are getting them from elsewhere you must malloc the necessary space yourself.

BTW, I don't usually look at the messages that aren't on the first page and I suspect most people don't, so if a thread is that old it's probably better to start a new one.


0

Response Number 11
Name: pkn123
Date: July 14, 2004 at 22:24:18 Pacific
Reply:

Hi,

Thanx for the answer ...

Here is the code what i am using ...
Originally TimeStamp(); is the code i am
using ...The function is called just as in
main. This is a similar scenario.

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

main(int argc, char *argv[])
{
unsigned char getTimeStamp[6] ;
char zone[80], i = 0;

while (argv[i] != (char *) NULL)
{
strcpy(zone, argv[i]);
TimeStamp(getTimeStamp, zone);
i++ ;
}
}

int
TimeStamp(unsigned char *getTimeStamp ,
char *zone)
{
time_t now;
struct tm tnow;
int curYear ;

setenv("TZ" ,zone ,1);

time(&now);
tnow = *localtime(&now);

curYear = tnow.tm_year + 1900 ;

getTimeStamp[0] = curYear - 2000 ;
getTimeStamp[1] = tnow.tm_mon + 1 ;
getTimeStamp[2] = tnow.tm_mday ;
getTimeStamp[3] = tnow.tm_hour ;
getTimeStamp[4] = tnow.tm_min ;
getTimeStamp[5] = tnow.tm_sec ;

unsetenv("TZ");

return 0;
}

Thanks
Kannan


0

Response Number 12
Name: Wolfbone
Date: July 15, 2004 at 03:03:09 Pacific
Reply:

I can't see anything very wrong with it and it compiles and runs without error on my system.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: Time Zones in Linux

need timezone value dynamically www.computing.net/answers/linux/need-timezone-value-dynamically/29440.html

Laptop Power Management in Linux www.computing.net/answers/linux/laptop-power-management-in-linux/24675.html

What's a good 56k modem to use in Linux? www.computing.net/answers/linux/whats-a-good-56k-modem-to-use-in-linux/3659.html