Linux time programming related functions

Source: Internet
Author: User
Tags local time

Type description of the time

TimeTimeTime :日历时间。1970年1月1日0点到此时的秒数。

Time acquisition function
time_t time (time_t *t);
Function Description: This function returns the number of seconds that have elapsed since January 1, 1970 00:00 00 seconds. If time_t *timer a non-null pointer, the function also saves the return value to the memory pointed to by the timer pointer.
Return value: Success returns the number of seconds, Failure returns ((time_t)-1) value, and the reason for the error is stored in errno.

Time conversion function
struct TM *gmtime (const time_t *TIMEP);
Function Description: gmtime () converts the calendar time information pointed to by the parameter Timep to GMT time as represented by the TM structure, and is saved to the struct TM structure.

struct TM *localtime (const time_t *TIMEP);
Function Description: localtime () Converts the time_t time information pointed to by the parameter TIMEP to the local time zone as represented by the TM struct.

time_t mktime (struct TM *tm);
Function Description: The TM structure data that the parameter tm points to is converted to the number of seconds elapsed GMT time from January 1, 1970 00:00 00 seconds to the present.

TM Structure prototype

structTM {intTm_sec;/ * seconds * /    intTm_min;/ * minutes * /    intTm_hour;/ * hours * /    intTm_mday;/* Day of the month * /    intTm_mon;/ * Month * /    intTm_year;/ * Year * /    intTm_wday;/* Day of the week * *    intTm_yday;/* Day in the year * /    intTM_ISDST;/ * Daylight saving time Daylight Saving time * /};

Time display function
Char *asctime (const struct TM *TM);
Function Description: Converts the time of the TM format into a string form

Char *ctime (const time_t *TIMEP);
Function Description: Converts the calendar time to a string form of the cost time.

Get Time function
int gettimeofday (struct timeval *tv, struct timezone *tz);
Function Description: Calendar time deposit Timeval structure. local time Zone week deposit timezone structure.该函数常用于计算时间损耗

Prototype of structural body

structtimeval{time_t tv_sec;/ * Number of seconds * /suseconds_t tv_usec;/ * microseconds * /};structtimezone{intTz_minuteswest;/* Minutes of difference from GMT */    intTz_dsttime;/ * Daylight Saving Time status * /The status represented by};z_dsttime is as follows Dst_none/ * Do not use * /Dst_usa/ * USA * /Dst_aust/ * Australia * /Dst_wet/ * Western Europe *Dst_met/ * Central Europe * /Dst_eet/ * Eastern Europe *Dst_can/ * Canada * /Dst_gb/ * Britannia * /Dst_rum/ * Romania * /Dst_tur/ * Turkey * *Dst_austalt/* Australia (after 1986) */

Deferred execution function
unsigned int sleep (unsigned int seconds);
Function Description: Causes the program to sleep seconds seconds.

int Usleep (useconds_t usec);
Function Description: Make the program sleep USEC microseconds.

Linux time programming related functions

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.