2.16 Time

The time functions allow you to obtain the current time, then convert and store it according to your particular needs. The current time is always taken from the system time.

Routine Use

asctime Converts time from type struct tm to a character string
clock Returns the elapsed CPU time for a process
ctime Converts time from type time_t to a character string
difftime Computes the difference between two times
_ftime Puts current system time in variable of type struct _timeb
gmtime Converts time from type time_t to struct tm
localtime Converts time from type time_t to struct tm with local correction
mktime Converts time to a calendar value
_strdate Returns the current system date as a string
strftime Formats a date and time string
_strtime Returns the current system time as a string
time Gets current system time as type time_t
_tzset Sets external time variables from the environment time variable
_utime Sets file-modification time

The time and _ftime functions return the current time as the number of seconds elapsed since midnight, on December 31, 1899, Universal Coordinated Time. This value can be converted, adjusted, and stored in a variety of ways by using the

asctime, ctime, gmtime, localtime, and mktime functions. The _utime function sets the modification time for a specified file, using either the current time or a time value stored in a structure.

Note:

In versions of Microsoft C/C++ prior to 7.0, the time and _ftime functions return the current time as the number of seconds elapsed since midnight, on January 1, 1970.

The clock function returns the elapsed CPU time for the calling process.

The _ftime function requires two files: SYS\TYPES.H and SYS\TIMEB.H. It is declared in SYS\TIMEB.H. The _utime function also requires two include files: SYS\TYPES.H and SYS\UTIME.H. It is declared in SYS\UTIME.H. The remainder of the time functions are declared in the include file TIME.H.

When you want to use _ftime or localtime to make adjustments for local time, you must define an environment variable named TZ. For more information on TZ and the global variables _daylight, _timezone, and _tzname, refer to “_daylight, _timezone, and _tzname”. TZ is also described on the _tzset reference page in Part 2 of this book.

The _strdate and _strtime routines return strings containing the current date and time, respectively, in the DOS and Windows date and time format rather than in the UNIX-style formats.

The strftime function is useful for creating international versions of a program. See “Internationalization”.