Help to get time zone between GMT & new york

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2006
Posts: 19
Reputation: jobra is an unknown quantity at this point 
Solved Threads: 0
jobra jobra is offline Offline
Newbie Poster

Help to get time zone between GMT & new york

 
0
  #1
Dec 15th, 2006
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

  1. time_t t = time(0);
  2. struct tm* data;
  3. data = localtime(&t);
  4. data->tm_isdst = 0;
  5. time_t a = mktime(data);
  6. data = gmtime(&t);
  7. data->tm_isdst = 0;
  8. time_t b = mktime(data);
  9. int gmtoffset = (a - b)/3600;

But I am not able to New York local time. Thanks a lot in advance.
Last edited by ~s.o.s~; Dec 15th, 2006 at 12:53 pm. Reason: Added code tags learn to use them yourself.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,699
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 271
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Help to get time zone between GMT & new york

 
0
  #2
Dec 15th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help to get time zone between GMT & new york

 
0
  #3
Dec 15th, 2006
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()
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 19
Reputation: jobra is an unknown quantity at this point 
Solved Threads: 0
jobra jobra is offline Offline
Newbie Poster

Re: Help to get time zone between GMT & new york

 
0
  #4
Dec 15th, 2006
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??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help to get time zone between GMT & new york

 
0
  #5
Dec 15th, 2006
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,398
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 245
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help to get time zone between GMT & new york

 
2
  #6
Dec 15th, 2006
Beaten to the punch, but I might have said something like this...
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. int main (void)
  5. {
  6. time_t t = time(0);
  7. if ( t != (time_t)-1 )
  8. {
  9. struct tm* data = gmtime(&t);
  10. if ( data )
  11. {
  12. data->tm_hour -= 5 + data->tm_isdst;
  13. t = mktime(data);
  14. if ( t != (time_t)-1 )
  15. {
  16. char buffer[32];
  17. if ( strftime(buffer, sizeof buffer, "%X", data) )
  18. {
  19. printf("NY time = %s\n", buffer);
  20. }
  21. }
  22. }
  23. }
  24. return 0;
  25. }
  26.  
  27. /* my output
  28. NY time = 00:36:52
  29. */
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.
Last edited by Dave Sinkula; Dec 15th, 2006 at 1:34 am. Reason: Updated time and added disclaimer.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 19
Reputation: jobra is an unknown quantity at this point 
Solved Threads: 0
jobra jobra is offline Offline
Newbie Poster

Re: Help to get time zone between GMT & new york

 
0
  #7
Dec 15th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,699
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 271
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Help to get time zone between GMT & new york

 
0
  #8
Dec 15th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Help to get time zone between GMT & new york

 
1
  #9
Dec 17th, 2006
UNIX only:
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 19
Reputation: jobra is an unknown quantity at this point 
Solved Threads: 0
jobra jobra is offline Offline
Newbie Poster

Re: Help to get time zone between GMT & new york

 
0
  #10
Dec 18th, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC