Program example for time management functions


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: Displaying time in various format

To show: Program example for time management functions


/* Playing with time. Program example for time management functions.

Many already deprecated, unsafe functions used here. You can try changing them to the safe versions */

#include <time.h>

#include <stdio.h>

#include <sys.h>

#include <string.h>


int main(void)


char buff[128], ampm[] = "AM";

__time64_t lgtime;

struct __timeb64 timstruct;

struct tm *today, *thegmt, xmas = {0, 0, 12, 25, 11, 90};


/* Set time zone from TZ environment variable. If TZ is not set,

* the operating system is queried to obtain the default value

* for the variable.*/



/* Get UNIX-style time and display as number and string. */



printf("Time in seconds since UTC 1/1/70:\t%ld seconds\n", lgtime);

printf("UNIX time and date:\t\t\t%s", _ctime64(&lgtime));


/*Display UTC.*/

thegmt = _gmtime64(&lgtime);

printf("Coordinated universal time, UTC:\t%s", asctime(thegmt));


/* Display operating system-style date and time. */


printf("OS time:\t\t\t\t%s\n", buff);


printf("OS date:\t\t\t\t%s\n", buff);


/* Convert to time structure and adjust for PM if necessary. */

today = _localtime64(&lgtime);

if(today->tm_hour >= 12)


strcpy(ampm, "PM");

today->tm_hour -= 12;


/* Adjust if midnight hour. */

if(today->tm_hour == 0)

today->tm_hour = 12;


/* Pointer addition is used to skip the first 11 characters and printf() is used to trim off terminating characters.*/

printf("12-hour time:\t\t\t\t%.8s %s\n", asctime(today) + 11, ampm);


/* Print additional time information. */


printf("Plus milliseconds:\t\t\t%u\n", timstruct.millitm);

printf("Zone difference in hours from UTC:\t%u hours\n", timstruct.timezone/60);

printf("Time zone name:\t\t\t\t%s\n", _tzname[0]);

printf("Daylight savings:\t\t\t%s\n", timstruct.dstflag ? "YES" : "NOT SET");


/* Make time for noon on Christmas, 1990. */

if(_mktime64(&xmas) != (__time64_t)-1)

printf("Christmas\t\t\t\t%s", asctime(&xmas));


/* Use time structure to build a customized time string. */

today = _localtime64(&lgtime);


/* Use strftime to build a customized time string. */

strftime(buff, 128, "Today is %A, day %d of %B in the year %Y.\n", today);



return 0;



Output example:


Time in seconds since UTC 1/1/70: 1165933610 seconds

UNIX time and date: Wed Dec 13 00:26:50 2006

Coordinated universal time, UTC: Tue Dec 12 14:26:50 2006

OS time: 00:26:50

OS date: 12/13/06

12-hour time: 12:26:50 AM

Plus milliseconds: 890

Zone difference in hours from UTC: 4294967286 hours

Time zone name: E. Australia Standard Time

Daylight savings: NOT SET

Christmas Tue Dec 25 12:00:00 1990

Today is Wednesday, day 13 of December in the year 2006.

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index