1. Definition of variables:
Struct TM { Int tm_sec;/* (0-61 )*/ Int tm_min;/* (0-59 )*/ Int tm_hour;/* (0-23 )*/ Int tm_mday;/* (1-31 )*/ Int tm_mon;/* (0-11 )*/ Int tm_year;/* Past 1900 */ Int tm_wday;/* (0-6 )*/ Int tm_yday;/* (0-365 )*/ Int tm_isdst;/* Daylight Savings flag */ }; |
2. One instance: reads the system time, and displays:
/*
Example of a function related to system time in C Language
*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <time. h>
# Include <dos. h>
Void main (void)
{
/* Time */
Time_t timer;
/* Pointer to struct TM */
Struct TM * ptrtime;
/* Call the time () function to obtain the current time */
Timer = Time (null );
/* Call the localtime () function to convert the obtained system time to the struct directed to the struct TM pointer */
Ptrtime = localtime (& timer );
/* Use asctime () to convert the struct to string output */
Printf ("now is: % s", asctime (ptrtime ));
Getch ();
}
3. Related header files
# Include <time. h>
4. Other functions
Asctime () |
Related functions |
Time, ctime, gmtime, localtime
|
Header file |
# Include <time. h>
|
Define functions |
Char * asctime (const struct TM * timeptr );
|
Function Description |
Asctime () converts the information in the TM structure referred to by the timeptr parameter to the time and date representation method used in the real world, and then returns the result in string form. This function has been converted from the time zone to the local time. The string format is "wed Jun 30 21:49:08 1993/N"
|
Return Value |
If you call the related time and date functions again, this string may be damaged. The difference between this function and ctime is that the input parameters are different structures.
|
Additional instructions |
Returns a string representing the current local time and date.
Gettimeofday (get current time) |
Related functions |
Time, ctime, ftime, settimeofday
|
Header file |
# Include <sys/time. h> # Include <unistd. h>
|
Define functions |
Int gettimeofday (struct timeval * TV, struct timezone * tz)
|
Function Description |
Gettimeofday () will return the current time with the structure indicated by TV, and put the information of the local time zone in the structure indicated by tz.
|
|