Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UTC Time

Status
Not open for further replies.

fergmj

Programmer
Feb 21, 2001
276
US
I have a very newbie question.

I need to get the current time in UTC but I have no idea how to use the time.h.

Can someone walk me down even the most basic of details?

Thanks

fergmj
 
The code from MSDN (with minimal modifications;):
Code:
/* GMTIME.C: This program uses gmtime to convert a long-
 * integer representation of coordinated universal time
 * to a structure named newtime, then uses asctime to
 * convert this structure to an output string.
 */

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

int main( void )
{
   struct tm *newtime;
   long ltime;

   time( &ltime );

   /* Obtain coordinated universal time: */
   newtime = gmtime( &ltime );
   printf( "Coordinated universal time is %s\n", 
                               asctime( newtime ) );
   return 0;
}
See MSDN help about gmtime(0 and struct tm fields (in IDE set cursor on gmtime word and press F1;).
Strictly speaking, in C++ you must include
Code:
#include <ctime>
#include <cstdio>
without .h.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top