943,928 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 10845
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 15th, 2006
0

Help to get time zone between GMT & new york

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jobra is offline Offline
19 posts
since Dec 2006
Dec 15th, 2006
0

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

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Dec 15th, 2006
0

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

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()
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 15th, 2006
0

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

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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jobra is offline Offline
19 posts
since Dec 2006
Dec 15th, 2006
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 15th, 2006
2

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

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 15th, 2006
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jobra is offline Offline
19 posts
since Dec 2006
Dec 15th, 2006
0

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

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Dec 17th, 2006
1

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

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.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Dec 18th, 2006
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jobra is offline Offline
19 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: reading a file using fscanf
Next Thread in C Forum Timeline: Loop validation in C - enlightenment needed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC