954,135 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help to get time zone between GMT & new york

Please can anyone help me to get the time zone between GMT & New York including the daylight saving.

I am able to get the timezone between GMT & localtime including the daylight saving. My program goes like this

time_t t = time(0);
struct tm* data;
data = localtime(&t);
data->tm_isdst = 0;
time_t a = mktime(data);
data = gmtime(&t);
data->tm_isdst = 0;
time_t b = mktime(data);
int gmtoffset = (a - b)/3600;


But I am not able to New York local time. Thanks a lot in advance.

jobra
Newbie Poster
19 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

Why not subtract the difference in hours between GMT and New York from the hour in GMT, adjusting for MN when necessary? I believe hour is one of the members of the tm struct.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

what operating system? There are no standard C or C++ functions to do that. If you are using MS-Windows you can call the win32 api function GetTimeZoneInformation ()

Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

I am using Linux operating system. so I dont think I can call the win32 api function GetTimeZoneInformation ().

Is there a possibility to get current time in new york each time the program runs, so that i can calculate the timezone??

jobra
Newbie Poster
19 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

EST is GMT - 5. So if you know what time zone your program is running in then adjust accordingly. use gmtime() then subtract 5 hours for eastern.

Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

Beaten to the punch, but I might have said something like this...

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

int main (void)
{
   time_t t = time(0);
   if ( t != (time_t)-1 )
   {
      struct tm* data = gmtime(&t);
      if ( data )
      {
         data->tm_hour -= 5 + data->tm_isdst;
         t = mktime(data);
         if ( t != (time_t)-1 )
         {
            char buffer[32];
            if ( strftime(buffer, sizeof buffer, "%X", data) )
            {
               printf("NY time = %s\n", buffer);
            }
         }
      }
   }
   return 0;
}

/* my output
NY time = 00:36:52
*/


Of course, I don't know whether or not gmtime will give you the correct Daylight Savings Time for New York. But it's a start.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Thanxs a lot for your reply. I understand. Right now I am using the same idea what you have explained in my program.

But what I want to make is automatically generate the timezone as the EST changes to EDT (daylight saving). I dont want to go to the program each time when EST changes to EDT to change the variable from -5 to -4. Hope I have made it clear.

jobra
Newbie Poster
19 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

First I'd convert GMT to EST. Then I'd see if the EST date falls withing the interval that DST starts and ends. If so, then I'd add an hour to EST time to convert from not DST to DST. Or something like that. You may need to fine tune it to the hour that DST starts in addition to the date that DST starts, but I think you get the picture.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

UNIX only:

getenv("TZ");


will return the timezone setting. The result is in the form
xxxNxxx, example MST7MDT. TZ has to be set for localtime() to return the correct time. MST is GMT +7. If TZ is not set the system is running GMT by default. Assuming time has been set.....
NY is EST5EDT. It's GMT+5.

jim mcnamara
Junior Poster
180 posts since May 2004
Reputation Points: 62
Solved Threads: 10
 

Thanxs a lot. So you mean to say that this works only in unix. How about in Linux?? If it can be used in Linux, can you elaborate by giving an example code?? Thanxs once again.

jobra
Newbie Poster
19 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

linux is unix.

It doesn't work in cygwin, however - a linux emulation that runs under windows and calls the Windows API for time stuff.

If for some reason TZ is not set for your login process then

#include <stdio.h>
#include <string.h
char *mytz(char *dest)
{
     char result[32]={0x0};
     FILE *cmd=popen("cat /etc/timezone","r");

     if( cmd!=NULL)
     {
           fgets(result,sizeof(result),cmd);
           pclose(cmd);
           strcpy(dest,result);
           return dest;
      }
      return NULL;
}

In linux /etc/timezone will show up something like "US/Mountain" rather
than the MDT thing most other unixes have.

Normally your login .bashrc or .profile (preferably /etc/profile) will have a statement like:

export TZ=`cat /etc/timezone`
jim mcnamara
Junior Poster
180 posts since May 2004
Reputation Points: 62
Solved Threads: 10
 

Isn't the question somewhat of the reverse? That is, if I am in South Africa and I have my program running, how can it tell me with correct reference to DST what the time is in New York? OP: Correct me if I am leading this astray.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

I am using cygwin, I tried to find /etc/profile in linux but of no use. Is there a simple way to do this? :rolleyes:

linux is unix.

It doesn't work in cygwin, however - a linux emulation that runs under windows and calls the Windows API for time stuff.

If for some reason TZ is not set for your login process then

#include <stdio.h>
#include <string.h
char *mytz(char *dest)
{
     char result[32]={0x0};
     FILE *cmd=popen("cat /etc/timezone","r");
 
     if( cmd!=NULL)
     {
           fgets(result,sizeof(result),cmd);
           pclose(cmd);
           strcpy(dest,result);
           return dest;
      }
      return NULL;
}

In linux /etc/timezone will show up something like "US/Mountain" rather
than the MDT thing most other unixes have.

Normally your login .bashrc or .profile (preferably /etc/profile) will have a statement like:

export TZ=`cat /etc/timezone`
jobra
Newbie Poster
19 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 
I am using cygwin,


Quoted from the Cywin website:
"Windows ports of many of the popular GNU software tools, including the BASH and tcsh shells."

So WHY in the world do you need cygwin when you are using Linux? It simply makes no sense to emulate tools you already have at your disposal.I tried to find /etc/profile in linux but of no use. Is there a simple way to do this? :rolleyes: I think the /etc/profile file is for compiler optimization - the ~/.profile file is the usually the one that contains timezone information, which is in /etc/timezone as previously stated.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You