// crt_times.c
//Compile with: /W 1
//This program demonstrates these time and date functions:
//time _ ftime ctime _ s as time _ s
//_ local time 64 _ s _ gmtime 64 _ s mktime _ tz set
//_ str time _ s _ strdate _ s str time
//
//is also a global variable:
// _tzname
//
# include & lttime.h & gt
# include & ltstdio.h & gt
# include & ltsys/types . h & gt;
# include & ltsys/time b . h & gt;
# include & ltstring.h & gt
int main()
{
char tmpbuf[ 128],timebuf[26],ampm[]= " AM ";
Time_t time;
struct _ timeb tstruct
struct tm today,gmt,xmas = { 0,0, 12,25, 1 1,93 };
errno _ t err
//Set the time zone from the TZ environment variable. If TZ is not set,
//Query the operating system for the default value.
//Used for variables.
//
_ tz set();
//Displays the date and time of the operating system style.
_strtime_s( tmpbuf, 128);
printf( "OS time:\t\t\t\t%s\n ",tmpbuf);
_strdate_s( tmpbuf, 128);
printf( "OS date:\t\t\t\t%s\n ",tmpbuf);
//Get UNIX-style time and display it as numbers and strings.
Time (& ampltime);
Printf ("seconds since UTC11/70: \t%ld\n", ltime);
err = ctime_s(timebuf,26 & amp; ltime);
If (wrong)
{
Printf( "ctime_s failed due to invalid parameters. " );
Exit (1);
}
Printf( "UNIX time and date: \t\t\t%s ",timebuf);
//Display UTC.
err = _ gmtime 64 _ s(& amp; gmt,& ampltime);
If (wrong)
{
Printf( "_gmtime64_s) failed due to invalid parameters. );
}
err = asctime_s(timebuf,26 & amp; GMT);
If (wrong)
{
Printf( "asctime_s failed due to invalid parameters. " );
Exit (1);
}
Printf ("Coordinated Universal Time: \t\t%s", timebuf);
//Convert to time structure, and make adjustments for PM if necessary.
err = _ local time 64 _ s(& amp; Today, and. ltime);
If (wrong)
{
Printf( "_localtime64_s) failed due to invalid parameters. );
Exit (1);
}
if(today . TM _ hour & gt; = 12 )
{
strcpy_s( ampm,sizeof(ampm)," PM ");
today . TM _ hour-= 12;
}
If( today.tm_hour == 0) // Adjust if midnight hour.
today . TM _ hour = 12;
//Convert today into an ASCII string
err = asctime_s(timebuf,26 & amp; Today);
If (wrong)
{
Printf( "asctime_s failed due to invalid parameters. " );
Exit (1);
}
//Note how to use pointer addition to skip the first 1 1
//character, printf is used to trim off the termination.
//character.
//
Printf( "12 hour time: \ t \ t% .8s% s \ n",
timebuf + 1 1,ampm);
//Print additional time information.
_ ftime(& amp; t struct); // C4996
//Note: _ftime has been deprecated; Consider using _ftime_s instead.
Printf( "Plus milliseconds: \t\t\t%u\n ",tstruct. millitm);
Printf ("Time zone difference from UTC: \t%u\n",
tstruct.timezone/60 );