Common Linux time programming functions

Source: Internet
Author: User

First, the time type is divided into two types:

  • Coordinated Universal Time (UTC): The world's standard time, that is, Greenwich Mean Time (GMT)
  • Calendar time: the calendar time, represented by a standard time point (for example, on January 1, January 1, 1970) to the number of seconds that have elapsed.
Time Acquisition
# Include <time. h> time_t time <time_t * tloc> // function: Obtain the calendar time, from on January 1, January 1, 1970 to the number of seconds that have elapsed since now. // typedef long time_t

Time Conversion

Struct TM * gmtime (const time_t * timep) // function: converts the calendar time to Greenwich Mean Time and saves it to the TM structure. Struct TM * localtime (const time_t * timep) // function converts the calendar time to the local time and saves it to the TM structure struct TM {int tm_sec; // The second value int tm_min; // minute value int tm_hour; // Hour value int tm_mday; // the nth day of the month int tm_mon; // the nth month of the year int tm_year; // which year int tm_wday; // The day of the week int tm_yday; // The day of the year int tm_lsdst; // Daylight Saving Time}

Time Display

Char * asctime (const struct TM * TM) // function: converts the time in the TM format to a string. For example, // sat Jul 30 08:43:03 2010 char * ctime (const time_t * timep) // function: converts the calendar time to a string of local time

Get time

Int gettimeofday (struct timeval * TV, struct timezone * tz) // function: gets the time difference from the early morning of the current day, which is often used to calculate the event time. Struct timeval {int TV _sec; // number of seconds int TV _usec; // Number of microseconds}

Latency

Unsigned int sleep (unsigned int seconds) // function: Enable the program to sleep seconds void usleep (unsigned long USEC) // function: Enable the program to sleep USEC microseconds

The following code provides a simple time programming to show the current time of the system.

# Include <stdio. h> # include <time. h> int main (void) {time_t seconds; char * timestr; seconds = Time (null); // obtain the calendar time timestr = ctime (& seconds ); // convert the calendar time to the string printf ("% s \ n", timestr); // print the returned 0 ;}

Output:

Wed Dec 12 14:14:57 2012

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.