GMT Time

This is a discussion on GMT Time within the C++ Programming forums, part of the General Programming Boards category; I need to get the time in GMT, so I used this: Code: #include <iostream> #include <windows.h> #include <string> #include ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    GMT Time

    I need to get the time in GMT, so I used this:
    Code:
    #include <iostream>
    #include <windows.h>
    #include <string>
    #include <time.h> 
    
    
    using namespace std;
    
    int main()
    {
    	time_t now;
    	struct tm *tm_now;
    	struct tm *tm_gmt;
    	char buff[BUFSIZ];
    
    	now = time ( NULL );
    	tm_now = gmtime(&now);
    
    	strftime ( buff, sizeof buff, "%a, %d %b %Y %H:%M:%S %Z", tm_now );
    	printf ( "%s\n%s\n", buff );
    
    	return 0; 
    }
    This code is straight from the FAQ, and it said to use gmtime() to get the time as GMT. But when I run that code, I get it in australian standard time. Does anyone know what went wrong, or an alternative way to get the time as GMT?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Maybe this example will help:
    Code:
    #include <iostream>
    #include<ctime>
    
    using namespace std; 	
    
    int main()
    {
    	struct tm *newtime;
    	long time_var;
    	
    	time(&time_var);
    	newtime = gmtime( &time_var );
    
    	printf( "Coordinated universal time is %s\n", 
                                   asctime( newtime ) );
    	return 0;
    }
    asctime() converts a tm time structure to a character string.
    Last edited by 7stud; 08-04-2003 at 02:48 AM.

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Well Salem I compiled that and got this:

    GMT=Mon, 04 Aug 2003 09:01:08 Cen. Australia Standard Time
    Local=Mon, 04 Aug 2003 18:31:08 Cen. Australia Standard Time

    I'm running Windows 2000 and compiling with MSVCC 6.0

    Damn australian computers, we're so secluded on our little island, we know nothing of the outside world, including time zones.

    Do you know of any other functions/API's I could use to get the time? I'm about to search MSDN, and if that fails I'll try google... but I always thought the time functions were standard C++, no idea why they dont work.

    Edit: Hey, I never realised the numbers were different. It must use GMT but still says the australian time zone. Oh well no matter, it works well enough so thanks very much guys!

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The code from the faq works. I ran this:
    Code:
    #include<iostream>
    #include <ctime>
    #include <cstdio>
    
    using namespace std; 	
    
    int main ()
    {
      time_t now;
      struct tm *tm_now;
      char buff[BUFSIZ];
    
      now = time ( NULL );
      tm_now = gmtime ( &now );
    
      strftime ( buff, sizeof buff, "%A, %x %X %p", tm_now );
      printf ( "%s\n", buff );
    
      return 0;
    }
    And, I checked the result against the GMT clock at the bottom of this page:

    http://greenwichmeantime.com/

    and the results were correct. Where does the result say anything about Australian standard time?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I ran that on VC++ 6.0.

  6. #6
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    Curious, I get the same results as you Stovellp.. the times are correct, yet it insists on adding the Cen. Australia Standard Time

    GMT=Mon, 04 Aug 2003 09:15:02 Cen. Australia Standard Time
    Local=Mon, 04 Aug 2003 18:45:02 Cen. Australia Standard Time

    Using Visual C++ .NET and Windows XP and my current location... Australia.

  7. #7
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    Yes, setting ones clock to GMT +05:00 Ekaterinburg (don't ask me, I have no idea where it is), produces these results.
    GMT=Mon, 04 Aug 2003 09:43:00 Ekaterinburg Standard Time
    Local=Mon, 04 Aug 2003 15:43:00 Ekaterinburg Daylight Time
    Does sound like somethings broken doesn't it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Representing floats with color?
    By DrSnuggles in forum C++ Programming
    Replies: 113
    Last Post: 12-30-2008, 08:11 AM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  3. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21