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.

Recommended Answers

All 13 Replies

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.

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()

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

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.

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.

commented: Excellent +1

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.

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.

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.

commented: Ah ~~ SunnyPalSingh +3

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.

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`

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.

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`

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.