Linux C functions: Time Functions

Source: Internet
Author: User

Function category:
1. Set the time: settimeofday, tzset
2. Get time: time, ftime, gettimeofday
3. Time Format Conversion: mktime, strftime; gmtime, localtime; asctime, ctime
4. Others: Clock, difftime

Asctime: represents the time and date in string format.
Header file: Time. h
Function Syntax: char * asctime (const struct TM * timeptr );
Note: asctime () converts the information in the TM structure referred to by the timeptr function 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 returned string format is: "wed Jun 30 21:49:08 1993/N"
Ctime: represents the time and date in string format.
Header file: Time. h
Function Syntax: char * ctime (const time_t * timep );
Note: ctime () is the same as asctime () function, but the input parameter is time_t.
Example:
# Include <stdio. h>
# Include <time. h>
Int main (void)
{
Time_t timep;
Time (& timep );
Printf ("% s", ctime (& timep ));
Printf ("% s", asctime (gmtime (& timep )));
Return 0;
}
Running result:
Sun Dec 14 15:30:11 2008
Sun Dec 14 15:30:11 2008

Clock: Obtain the CPU usage time of the process.
Header file: Time. h
Function Definition: clock_t clock (void );
Description: Clock () is used to return the approximate CPU time occupied by the process.

Difftime: computing time gap
Header file: Time. h
Function Syntax: Double difftime (time_t time1, time_t time0 );
(Difftime () is used to calculate the parameter time1-time0, the result is returned with the exact value of double type. The time of the two parameters is January 1, 1970 00:00:00 UTC time.

Ftime: Get the current time and date
Header file: SYS/timeb. h
Function Syntax: int ftime (struct timeb * TP );
Note: ftime () outputs the structure specified by the parameter TP in the date range. The TP structure is defined as follows:
Struct timeb {
/* The number of seconds since January 1, January 1, 1970 */
Time_t time;
/* It is 1‰ seconds */
Unsigned short millitm;
/* The time difference between the current time zone and Greenwich, in the unit */
Short timezone;
/* Indicates the corrected state of daylight saving time. If the value is not 0, the corrected state of Daylight Saving Time is enabled */
Short dstflag;
};
0 is returned for both success and failure.
Example:
# Include <stdio. h>
# Include <sys/timeb. h>
Int main (void)
{
Struct timeb TP;
Ftime (& TP );
Printf ("Time: % d/N", TP. time );
Printf ("millitm: % d/N", TP. millitm );
Printf ("timezone: % d/N", TP. timezone );
Printf ("dstflag: % d/N", TP. dstflag );
Return 0;
}
Running result:
Time: 1229271908
Millitm: 716
Timezone:-480.
Dstflag: 0

Gettimeofday: Get the current time
Header file: SYS/time. h unist. d
Function Syntax: int gettimeofday (struct timeval * TV, struct timezone * tz );
Note: gettimeofday () will return the current time in the structure indicated by TV, and put the information of the local time zone in the structure indicated by tz. 0 is returned for success,-1 is returned for failure, and the error code is stored in errno. efault indicates that the memory space indicated by the pointer TV and TZ exceeds the access permission.
The timeval structure is defined:
Struct timeval {
/* The number of seconds since January 1, January 1, 1970 */
Long TV _sec;
/* Microseconds */
Long TV _usec;
};
The timezone structure is defined:
Struct timezone {
/* The time difference between Greenwich and */
Int tz_minuteswest;
/* State of daylight saving time */
Int tz_dsttime;
};
The above two structures are defined in/usr/include/sys/time. h. The status represented by tz_dsttime is as follows:
Dst_none/* not used */
Dst_usa/* United States */
Dst_aust/* Australia */
Dst_wet/* Western Europe */
Dst_met/* Central Europe */
Dst_eet/* Eastern Europe */
Dst_can/* Canada */
Dst_gb/* Great Britain */
Dst_rum/* Romania */
Dst_tur/* Turkey */
Dst_austalt/* Australia (after January 1, 1986 )*/

Gmtime: converts the number of seconds to the current time and date.
Header file: Time. h
Function Syntax: struct TM * gmtime (const time_t * timep );
Note: gmtime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result from the structure TM. The structure TM is defined:
Struct TM {
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 indicates the current number of seconds. The normal range is 0-59, but the value can be 61 seconds.
Int tm_min indicates the current score, range: 0-59
Int tm_hour, which is counted from midnight, ranges from 0 to 23.
The number of days in the current month of int tm_mday, ranging from 01 to 31.
Int tm_mon indicates the current month. The value ranges from 0 to 11 from January 1, January.
Int tm_year: The number of years since January 1, 1900
The number of days in a week for int tm_wday. The value ranges from 0 to 6 from Monday.
Int tm_yday indicates the number of days since January 1, January 1 this year. The value range is 0-365.
Int tm_isdst time saving flag
The time and date returned by this function are not converted by the time zone, which is the UTC time.

Localtime: converts the number of seconds to the current local time and date.
Header file: Time. h
Function Syntax: struct * localtime (const time_t * timep );
Note: localtime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result from the TM structure. for the definition of the structure TM, see gmtime (). the time date returned by this function has been converted to the local time zone.

Mktime: the number of seconds after which the time structure data is converted.
Header file: Time. h
Function Syntax: time_t mktime (struct TM * timeptr );
Note: mktime () is used to convert the TM structure data referred to by the timeptr parameter to the number of seconds passed by the UTC time since 00:00:00, January 1, January 1, 1970. The number of seconds elapsed is returned.

Settimeofday: set the current time
Header file: SYS/time. h unistd. h
Function Syntax: settimeofday () sets the current time to the structure information indicated by TV, and the local time zone information to the structure indicated by tz. for more information, see gettimeofday (). note: Only the root permission can be used to modify the time of this function. 0 is returned for success,-1 is returned for failure, and the error code is stored in errno.
Error code:
Eperm does not call settimeofday () with the root permission.
The einval time zone or data is incorrect and the time cannot be set correctly.

Strftime: format the date and time
Header file: Time. h
Function Syntax: size_t strftime (char * s, size_t Max, const char * format, const struct TM * TM );
Note: strftime () converts the Time Structure of the TM parameter by referring to the string format specified by the format parameter. The converted string content will be copied to the string array referred to by parameter S, the maximum length of this string is controlled by the parameter Max. the format command of the format parameter is as follows:
% A abbreviation of the local day of the week, for example, sun
% A abbreviation of the local day of the week, for example, Sunday
% B abbreviation of the local month
% B full name of the local month
% C local appropriate Date and Time Representation
% C year/100 indicates the year
% D number of days in the month, expressed as 01-31
% D is equivalent to "% m % d % Y" Format
% E number of days in the month, expressed as 1-31
% H abbreviation of the local month
% H represents the hour in the 24-hour format. The notation is 00-23.
% I represents the hour in 12-hour format, and the notation is 01-12.
% J days in a year (001-366)
% K represents the hour in the 24-hour format. The notation is 0-23.
% L represents the hour in 12-hour format. The notation is 1-12.
% M month (01-12)
% M minutes (00-59)
% N same as/n
% P displays the corresponding am or PM
% P displays the corresponding am or PM
% R is equivalent to "% I: % m: % S % P" Format
% R is equivalent to "% H: % m" Format
% S the number of seconds that have elapsed since January 1, 1970 00:00:00 UTC
% S (00-59)
% T same as/T
% T 24 hours, equivalent to "% H: % m: % s" Format
% U day of the week in one week, range: 1-7, Monday starts from 1
% U the number of weeks in a year (00-53). The first Sunday of January 1, January is 01.
% W indicates the day of the week in the one-star period. The value ranges from 0 to 6. Sunday starts from 0.
% W the number of weeks in a year (00-53). The first Monday of January 1, January is 01.
% X local date Representation
% X Local Time Representation
Year in % Y Century
% Y indicates the complete year in AD.
The Time Zone name used by % Z
%'
Returns the total number of characters in the string array to be copied to parameter S, excluding the string Terminator. If 0 is returned, it indicates that the string is not copied to parameter S, but not necessarily an error.
Note: The environment variables TZ and tc_time will affect the result of this function.
Example:
# Include <stdio. h>
# Include <time. h>
Int main (void)
{
Char * Format [] = {"% I: % m: % S % P % m/% d % A", "% x % Y", null };
Char Buf [30];
Int I;
Time_t clock;
Struct TM * TM;
Time (& clock );
TM = localtime (& clock );
For (I = 0; format [I]! = NULL; I ++)
{
Strftime (BUF, sizeof (BUF), format [I], Tm );
Printf ("% s => % s/n", format [I], Buf );
}
Return 0;
}
Running result:
% I: % m: % S % P % m/% d % A => 01: 46: 44 am 12/15 mon
% X % Y => 12/15/08 01:46:44 2008

Time: Get the current time
Header file: Time. h
Function Syntax: time_t time (time_t * t );
Note: Time () returns the number of seconds that have elapsed since 00:00:00, January 1, January 1, 1970, UTC. if t is not a null pointer, this function also saves the returned value to the memory indicated by T pointer. success returns the number of seconds, Failure Returns the (time_t-1) value, the cause of the error is stored in errno.

Tzset: sets the time zone for time conversion.
Header file: Time. h
Function Syntax: void tzset (void); extern char * tzname [2];
Note: tzset () is used to set the environment variable TZ to the global variable tzname, that is, to obtain the current local time zone from the environment variable. the time conversion function automatically calls this function. if tz Is set, tzname will find the closest Local Time Zone according to/etc/localtime. if tz Is null or cannot be recognized, the UTC time zone is used. this function is always successful and the tzname is initialized.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.