C time function set

xiaoxiao2021-03-06  40

Asctime (expressed time and date in string format)

related functions

Time, CTIME, GMTIME, LOCALTIME

Header file

#include

Definition function

Char * asctime (const struct tm * timeptr);

Function description

Asctime () converts the information in the TM structure referred to the parameter TimePtr into the time dates represented by the real world, and then returns the result in a string form. This function has been converted into local time by the time zone, and the string format is: "WED JUN 30 21:49:08 1993 / N"

return value

This string may be destroyed if the relevant time date function is called again. This function is different from CTIME in the incoming parameters.

Additional information

Returns a string represents the current local time date.

example

#include main () {time_t timep; time (& timep); Printf ("% s", asctime (gmtime (& time)));}

carried out

Sat Oct 28 02:10:06 2000

CTIME (indicate time and date in string format)

related functions

Time, Asctime, GMTIME, LOCALTIME

Header file

#include

Definition function

Char * ctime (const time_t * Timep);

Function description

CTIME () converts the information in the Time_t structure referred to the parameter TIMEP into the time date representation method used by the real world, and then returns the result in a string form. This function has been converted into local time by the time zone, and the string format is "WED JUN 30 21: 49: 08 1993 / N". This string may be destroyed if the relevant time date function is called again.

return value

Returns a string represents the current local time date.

example

#include main () {time_t timep; time (& time); Printf ("% s", ctime (& time));}

carried out

Sat Oct 28 10: 12: 05 2000

GetTimeOfDay (get the current time)

related functions

Time, CTIME, FTIME, SETTIMEOFDAY

Header file

#include #include

Definition function

INT getTimeOfDay (Struct Timeval * TV, Struct Timezone * Tz)

Function description

GetTimeOfDay () will return the current time there is a structure referred to in TV. The information of the local time zone is placed in the structure referred to in Tz. The TimeVal structure is defined as: struct timeval {long TV_sec; / * second * / long TV_usec; ​​/ * microsecond * /}; TimeZone structure is defined as: Struct Timezone {int TZ_MINUTESWEST; / * and GREENWICH time difference * / int TZ_DSTTIME ; / * Status of daylight saving time * /}; the above two structures are defined in /usr/include/sys/time.h. The status represented by tz_dsttime is the following DST_NONE / * Do not use * / dst_usa / * United States * / DST_AUST / * AUTED * / DST_WET / * Western Europe * / DST_MET / * 中 欧 * / dst_eet / * Eastern Europe * / DST_CAN / * Canada * / DST_GB / * Great Britain * / DST_RUM / * Romania * / DST_TUR / * Turkey * / DST_Austalt / * Australia (after 1986) * /

return value

Success returns 0, the failure returns -1, the error code exists in Errno. Additional description EFAULT pointer TV and TZ are referred to by the memory space beyond access rights. example

#include #include main () {struct timevalval TV; struct timezone tz; gettimeofday (& TV, & tz); Printf ("TV_sec;% d / n", TV, .tv_sec ); Printf ("tv_usec;% d / n", tv.tv_usec); Printf ("tz_minuteswest;% d / n", tz.tz_minuteswest); Printf ("tz_dsttime,% d / n", tz.tz_dstime); }

carried out

TV_SEC: 974857339TV_USEC: 136996TZ_MINUTESWEST: -540TZ_DSTTIME: 0

GMTIME (achieved time and date)

related functions

Time, Asctime, CTIME, LOCALTIME

Header file

#include

Definition function

StructTM * GMTIME (const time_t * timep);

Function description

GMTIME () converts information in the Time_t structure referred to the parameter TIMEP into the time date representation method used in real world, and then returns the result by the structure Tm. The structural Tm definition is StructTM {INTTM_SEC; INTTM_MIN; int TM_HOUR; INTTM_MDAY; INTTM_MON; INTTM_YEAR; INTTM_WDAY; INTTM_YDAY; INTTM_ISDST;}; int TM_SEC represents the current number of seconds, normal range 0-59 However, it allows for 61 second INTTM_MIN to represent the current score, the number of hours counts from 0-59INT TM_HOUR, the range of 0-23int TM_MDAY is currently the number of days in the month, the range 01-31int TM_MON represents the current month, from January The number of days from 0-11int TM_Year from 1900, IntTm_wday's day, from Monday, the range is 0-6int TM_YDAY from January 1 this year, the number of days is 0- 365INT TM_ISDST Skylight Savings Time Flag This function returned to the time period without time zone conversion, but UTC time.

return value

Return structure TM represents current UTC time

example

#include main () {char * wday [] = {"Sun", "MON", "TUE", "WED", "Thu", "Fri", "SAT"}; Time_t TimeP; StructTM * P; Time (& TimeP); P = GMTIME (& TimeP); Printf ("% D% D% D", (1900 p-> tm_year), (1 P-> TM_MON), P-> TM_MDAY ); Printf ("% S% D;% D;% D / N", WDAY [P-> TM_WDAY], P-> TM_HOUR, P-> TM_MIN, P-> TM_SEC);}

carried out

2000/10/28 sat 8:15:38

LocalTime (obtained local time and date)

related functions

Time, AscTime, CTIME, GMTIME

Header file

#include

Definition function

StructTM * localtime (const time_t * timep);

Function Description LocalTime () converts the information in the Time_t structure referred to the parameter TIMEP into the time date representation method used by the real world, and then returns the result by the structure Tm. For the definition of structure TM, please refer to GMTIME (). The time dates returned by this function have been converted into local time zones.

return value

Returns the structure Tm represents the current local time.

example

#include main () {char * wday [] = {"Sun", "MON", "TUE", "WED", "Thu", "Fri", "SAT"}; Time_t TimeP; StructTM * P; Time (& TimeP); P = localtime (& TimeP); / * Get local time * / printf ("% D% D% D", (1900 p-> tm_year), (l p-> TM_MON), P-> TM_MDAY); Printf ("% S% D:% D:% D / N", WDAY [P-> TM_WDAY], P-> TM_HOUR, P-> TM_MIN, P-> TM_SEC); }

carried out

2000/10/28 sat 11:12:22

Mktime (convert the time structure data into the number of seconds)

related functions

Time, Asctime, GMTIME, LOCALTIME

Header file

#include

Definition function

Time_t mktime (STRCUT TM * TIMEPTR);

Function description

MkTime () is used to convert the TM structure data referred to by parameter TimePtr into the second number of seconds that count the UTC time from 0:00 pm, January 1, 1970.

return value

Returns the number of seconds.

example

/ * Time (second) with time ()), use localtime () to convert to struct TM and then use mktine () to convert StructTM to its original second number * / # include main () {time_t Timep ; STRCUT TM * P; TIME (& TIMEP); Printf ("Time ():% D / N", TIMEP); P = LocalTime (& TimeP); TimeP = MKTIME (P); Printf ("Time () -> LocalTime () -> mktime ():% d / n ", timeP);

carried out

Time (): 974943297time () -> LocalTime () -> mktime (): 974943297

SetTimeOfDay (set up time)

related functions

Time, CTIME, FTIME, GETTIMEOFDAY

Header file

#include #include

Definition function

Int setTimeOfDay (const struct timeval * tv, const struct timezone * tz);

Function description

SetTimeOfDay () will set the current time into structure information referred to by TV, and local time zone information is set to Tz. Please refer to getTimeOfDay () for a detailed description. Note that only root privileges can use this function to modify the time.

return value

Success returns 0, the failure returns -1, the error code exists in Errno.

error code

Eperm is not called setTimeOfDay (), and permissions is not enough by root permission. EINVAL time zone or a data is incorrect and cannot set the time correctly.

Time (achieved current time)

related functions

CTIME, FTIME, GETTIMEOFDAY header file

#include

Definition function

Time_t Time (Time_t * T);

Function description

This function returns from the UTC time from January 1, 1970, 200 minutes 0:00, 0:0:0:04. If t is not empty pointer, this function will save the return value to the memory referred to by the T pointer.

return value

Success returns the number of seconds, returns ((TIME_T) -1) value, and the error is existed in Errno.

example

#include mian () {int seconds = time ((time_t *) null); Printf ("% d / n", seconds);}

carried out

9.73e 08

转载请注明原文地址:https://www.9cbs.com/read-85791.html

New Post(0)