Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
How to calculate the difference between Central and Mountain times in Linux, not installing time zone on my computer?
!

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)

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.

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.

What do you mean "no functions for working with timezones"?
'info Libc Calendar'
You can still use glibc functions in a C++ programme!

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.

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);
}

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

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.

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

![]() |
![]() |
![]() |

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