Header file # include
Define the function char * asctime (conststructm * timeptr);
This function shows that asctime () converts the information in the tm structure pointed by the parameter timeptr into the time and date representation method used in the real world, and then returns the result as a string. This function has been converted from time zone to local time, and the string format is "wedjun 3021:49: 081993 \ n".
If the return value calls the related time and date function again, this string may be destroyed. The difference between this function and ctime is that the parameters passed in have different structures.
Additional Description Returns a string indicating the current local time and date.
Example # includes
Master ()
{
Time_t time;
Time (& amptimep);
printf("%s ",ASC time(gmtime(& amp; time p)));
}
Execute 20001Saturday, October 28th at 02: 10: 06.
Ctime (time and date in string format) related functions time, asctime, gmtime, localtime.
Header file # include
Define the function char * ctime (consttime _ t * timep);
The function description ctime () converts the information in the time_t structure pointed by the parameter timep into the time and date representation method used in the real world, and then returns the result in the form of a string. This function has been converted from time zone to local time, and the string format is "wedjun 3021:49: 081993 \ n". If the related time-date function is called again, the string may be destroyed.
Return Value Returns a string representing the current local time and date.
Example # includes
Master ()
{
Time_t time;
Time (& amptimep);
printf("%s ",ctime(& amp; time p));
}
Executed on Saturday, October 28th, 2000 10:12: 05.
Gettimeofday related functions time, ctime, ftime, settimeofday.
Header file # include
# include & ltunistd.h & gt
Define the function int gettime of day (struct timeval * TV, struct timezone * tz).
The function description gettimeofday () returns the current time with the structure indicated by tv, and the information of local time zone is placed in the structure indicated by tz.
Timeval structure is defined as:
Structure time interval {
Long tv _ sec/* sec */
Long tv _ usec/* microseconds */
};
The time zone structure is defined as follows:
Structure time zone {
Int tz _ minuteswest/* How many minutes is the difference between Greenwich Mean Time and Greenwich Mean Time */
Int tz _ dsttime/* state of daylight saving time */
};
Both of the above structures are defined in/usr/include/sys/time.h. The state represented by tz_dsttime is as follows
DST_NONE /* Do not use */
DST_USA /* USA */
Dst _ austAustralia/* Australia */
DST_WET /* Western Europe */
DST_MET /* Central Europe */
EET Daylight Saving Time/* Eastern Europe */
DST_CAN /* Canada */
DST_GB /* UK */
DST_RUM /* Romania */
DST_TUR /* Turkey */
DST_AUSTALT /* Australia (after 1986) */
If successful, the return value is 0; If it fails, the return value is-1, and the error code is stored in errno. Supplementary note that the memory space pointed by the default pointers tv and tz exceeds the access rights.
Example # includes
# include & ltunistd.h & gt
main(){
Structured TV;
Structural time zone tz;
gettimeofday(& amp; Television & tz);
printf(" TV _ sec; %d\n ",TV,. TV _ sec);
printf(" TV _ usec; %d\n ",TV . TV _ usec);
printf(" tz _ minutes west; %d\n ",tz . tz _ minutes west);
printf("tz_dsttime,%d\n ",tz . tz _ dst time);
}
Execute tv_sec: 974857339.
tv_usec: 136996
tz_minuteswest:-540
tz_dsttime:0
Gmtime (get the current time and date) related functions time, asctime, ctime, localtime.
Header file # include
Define the function structtm * gmtime (consttime _ t * timep);
Function description gmtime () converts the information in the time_t structure pointed by the parameter time po into the time and date representation method used in the real world, and then returns the result through the structure tm.
Structure tm is defined as
Structural trademark
{
int tm _ sec
int tm _ min
int tm _ hour
int tm _ mday
int tm _ mon
int tm _ year
int tm _ wday
int tm _ yday
int tm _ isdst
};
Int tm_sec represents the current second. The normal range is 0-59, but 6 1 second is allowed.
Int tm_min represents the current score, ranging from 0 to 59.
Int tm_hour hours from midnight, ranging from 0 to 23.
Int tm_mday Number of days in the current month, ranging from 0 1 to 3 1.
Int tm_mon indicates the current month, counting from 1 month, and the range is 0- 1 1.
Years since Int tm _ year years.
Int tm_wday The number of days in a week starting from Monday, ranging from 0 to 6.
Int tm_yday 65438+1 October1days this year, ranging from 0 to 365.
Int tm_isdst daylight saving time sign
The time and date returned by this function is UTC time without time zone conversion.
The return value return structure tm represents the current UTC time.
Example # includes
main(){
char *wday[]={"Sun "," Mon "," Tue "," Wed "," Thu "," Fri "," Sat " };
Time_t time;
struct TM * p;
Time (& amptimep);
p=gmtime。 timep);
printf("%d%d%d ",( 1900+p->; tm_year),( 1+p->; tm_mon),p-& gt; TM _ mday);
printf(" % s % d; % d; %d\n ",wday[p-& gt; tm_wday],p-& gt; tm_hour,p-& gt; tm_min,p-& gt; TM _ sec);
}
Execute 2000/128 Saturday at 8: 15: 38.
Localtime (get local current time and date) related functions time, asctime, ctime and gmtime.
Header file # include
Define the function structtm * localtime (consttime _ t * timep);
This function shows that localtime () converts the information in the time_t structure pointed by the parameter timep into the time and date representation method used in the real world, and then returns the result through the structure tm. Please refer to gmtime () for the definition of structure tm. The time and date returned by this function have been converted to the local time zone.
Return Value Returns the structure tm representing the current local time.
Example # includes
main(){
char *wday[]={"Sun "," Mon "," Tue "," Wed "," Thu "," Fri "," Sat " };
Time_t time;
struct TM * p;
Time (& amptimep);
p=localtime。 timep); /* Get local time */
printf ("%d%d%d ",( 1900+p->; tm_year),(l+p-& gt; tm_mon),p-& gt; TM _ mday);
printf("%s%d:%d:%d\n ",wday[p-& gt; tm_wday],p-& gt; tm_hour,p-& gt; tm_min,p-& gt; TM _ sec);
}
Execute 2000/1Saturday, October 28th 1 1: 12: 22.
Mktime (converting time structure data into elapsed seconds) related functions time, asctime, gmtime, localtime.
Header file # include
Define the function time _ tmktime (strkutm * timeptr);
Function description: mktime () is used to convert the tm structure data indicated by the parameter timeptr into the number of seconds from AD1970 65438+1October 1: 00 to UTC time.
Returns the number of seconds elapsed.
Example/* Use time () to get the time (seconds) and use localtime ().
Convert it into struct tm, and then use mktine () to convert it into the original second */
# include & lttime.h & gt
Master ()
{
Time_t time;
strcut TM * p;
Time (& amptimep);
printf("time() : %d \n ",timep);
p=localtime。 timep);
time p = mktime(p);
printf(" time()-& gt; local time()-& gt; mktime():%d\n ",time p);
}
Execution time (): 974943297
time()-& gt; local time()-& gt; mktime():974943297
Settimeofday related functions time, ctime, ftime, gettimeofday.
Header file # include
# include & ltunistd.h & gt
Define the function int settime of day (const struct timeval * TV, const struct timezone * tz);
Function description: settimeofday () sets the current time to the structure information indicated by tv, and sets the local time zone information to the structure indicated by tz. Please refer to gettimeofday () for a detailed description. Please note that only root privileges can use this function to modify the time.
If successful, the return value is 0; If it fails, the return value is-1, and the error code is stored in errno.
Error code EPERM is not called settimeofday () by root permission, and the permission is insufficient.
The EINVAL time zone or some data is incorrect, so the time cannot be set correctly.
Time (get the current time) related functions ctime, ftime, gettimeofday
Header file # include
Define the function time _ t time (time _ t * t);
Function Description: This function will return the number of seconds that have passed since 0: 00: 00 and 0: 00 UTC time AD 1970 1. If t is not a null pointer, this function will also save the return value to the memory pointed by the t pointer.
Returns the number of seconds on success, and the value of ((time_t)- 1) on failure. The cause of the error is stored in errno.
Example # includes
Encourage ()
{
int seconds = time((time _ t *)NULL);
Printf("%d\n ",seconds);
}
Execute 9.73E+08.
Thank you. The above functions are related to time under linux. I hope I can help friends in need. In fact, many problems are complicated by everyone themselves, which is really not difficult.