Gets the system time.
#include <time.h> | Required only for function declarations |
time_t time( time_t *timer );
timer | Storage location for time |
The time function returns the number of seconds elapsed since midnight (00:00:00), December 31, 1899, Universal Coordinated Time, according to the system clock. The system time is adjusted according to the _timezone system variable, which is explained under _tzset.
The return value is stored in the location given by timer. This parameter may be NULL, in which case the return value is not stored.
The time function returns the time in elapsed seconds. There is no error return.
Standards:ANSI, UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
asctime, _ftime, gmtime, localtime, _tzset, _utime
/* TIMES.C illustrates various time and date functions including:
* localtime gmtime mktime _tzset
*
* Also the global variable:
* _tzname
*/
#include <time.h>
#include <stdio.h>
#include <sys\types.h>
#include <sys\timeb.h>
#include <string.h>
void main( void )
{
char tmpbuf[128], ampm[] = "AM";
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 91 };
/* Set time zone from TZ environment variable. If TZ is not set,
* PST8PDT is used (Pacific standard time, daylight savings).
*/
_tzset();
/* Display DOS-style date and time. */
_strtime( tmpbuf );
printf( "DOS time:\t\t\t\t%s\n", tmpbuf );
_strdate( tmpbuf );
printf( "DOS date:\t\t\t\t%s\n", tmpbuf );
/* Get UNIX-style time and display as number and string. */
time( <ime );
printf( "Time in seconds since GMT 1/1/70:\t%ld\n", ltime );
printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );
/* Display GMT. */
gmt = gmtime( <ime );
printf( "Greenwich Mean Time:\t\t\t%s", asctime( gmt ) );
/* Convert to time structure and adjust for PM if necessary. */
today = localtime( <ime );
if( today-tm_hour 12 )
{
strcpy( ampm, "PM" );
today-tm_hour -= 12;
}
/* Note how 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. */
ftime( &tstruct );
printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
printf( "Zone difference in seconds from GMT:\t%u\n", tstruct.timezone );
printf( "Time zone name:\t\t\t\t%s\n", tzname[0] );
printf( "Daylight savings:\t\t\t%s\n", tstruct.dstflag ? "YES" : "NO" );
/* Make time for noon on Christmas, 1991. */
if( mktime( &xmas ) != (time_t)-1 )
printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );
/* Use time structure to build a customized time string. */
today = localtime( <ime );
/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y.\n", today );
printf( tmpbuf );
}
DOS time: 17:36:10
DOS date: 12/15/99
Time in seconds since GMT 1/1/70: -1398750726
UNIX time and date: Wed Dec 15 17:36:10 1999
Greenwich Mean Time: Thu Dec 16 00:36:10 1999
12-hour time: 05:36:10 PM
Plus milliseconds: 90
Zone difference in seconds from GMT: 480
Time zone name: PST
Daylight savings: NO
Christmas Wed Dec 25 12:00:00 1999
Today is Wednesday, day 15 of December in the year 1999.