C Standard Library source code anatomy (7): Date and Time Functions time. h

Source: Internet
Author: User
Tags posix

The date and time functions are in time. h, it mainly represents the clock_t type of the processor clock, time_t type of time, number of tick messages per second clocks_per_sec, struct TM structure describing the calendar time, function clock, time, asctime, ctime, gmtime, localtime, mktime, difftime, strftime, wcsftime (wide character version ), others are non-standard extensions.
1. Time. h: clock_t type, time_t type definition, macro clocks_per_sec, struct TM structure includes seconds, minutes, hours, days, months, years (from January 1, 1900) week, is the day of the year, the Daylight Saving Time sign a total of 9 members.

/* ISO c99 standard: 7.23 date and time <time. h> */<br/> # ifndef_time_h <br/> # If (! Defined _ need_time_t &&! Defined _ need_clock_t &/<br/>! Defined _ need_timespec) <br/> # DEFINE _ time_h1 <br/> # include <features. h> <br/>__ begin_decls <br/> # endif <br/> # ifdef_time_h <br/>/* from <stddef. obtain size_t and null */<br/> # DEFINE _ need_size_t <br/> # DEFINE _ need_null <br/> # include <stddef. h> <br/>/* the constant clocks_per_sec defined here is the number of clock ticking times per second on the processor */<br/> # include <bits/time. h> <br/>/* here is the macro name in the outdated POSIX.1-1988 for the same constant */<br/> # If! Defined _ strict_ansi __&&! Defined _ use_xopen2k <br/> # ifndef clk_tck <br/> # define clk_tckclocks_per_sec <br/> # endif/* <time. h> sorted ded */<br/> # If! Defined _ clock_t_defined & (defined _ time_h | defined _ need_clock_t) <br/> # DEFINE _ clock_t_defined1 <br/> # include <bits/types. h> <br/>__ begin_namespace_std <br/> typedef _ clock_t;/* defines the CPU clock type, the clock function returns this type */<br/>__ end_namespace_std <br/> # If defined _ use_xopen | defined _ use_posix | defined _ use_misc <br/> __ using_namespace_std (clock_t) <br/> # endif/* clock_t is not defined and <t IME. h> clock_t */<br/> # UNDEF _ need_clock_t <br/> # If! Defined _ time_t_defined & (defined _ time_h | defined _ need_time_t) <br/> # DEFINE _ time_t_defined1 <br/> # include <bits/types. h> <br/>__ begin_namespace_std <br/> typedef _ time_t;/* defines the time type, which is an integer type, the time function returns this type */<br/>__ end_namespace_std <br/> # If defined _ use_posix | defined _ use_misc | defined _ use_svid <br/> __ using_namespace_std (time_t) <br/> # endif/* time_t is not defined and <time. H> time_t */<br/> # UNDEF _ need_time_t is required. <br/> # If! Defined _ clockid_t_defined &/<br/> (defined _ time_h & defined _ use_posix199309) | defined _ need_clockid_t) <br/> # DEFINE _ clockid_t_defined1 <br/> # include <bits/types. h> <br/> typedef _ clockid_t;/* defines the type of the clock ID, clock and timer functions use it */<br/> # endif/* clockid_t is not defined, and <time. h> clockid_t */<br/> # UNDEF _ clockid_time_t is required. <br/> # If! Defined _ timer_t_defined &/<br/> (defined _ time_h & defined _ use_posix199309) | defined _ need_timer_t) <br/> # DEFINE _ timer_t_defined1 <br/> # include <bits/types. h> <br/> typedef _ timer_t;/* defines the type of time ID, which is used by the time_create function */<br/> # endif/* timer_t does not exist, and <time. h> timer_t */<br/> # UNDEF _ need_timer_t is required. <br/> # If! Defined _ timespec_defined &/<br/> (defined _ time_h &/<br/> (defined _ use_posix199309 | defined _ use_misc )) |/<br/> defined _ need_timespec) <br/> # DEFINE _ timespec_defined1 <br/> # include <bits/types. h>/* Here we define the _ time_t */<br/>/* structure of the time value in posix.1b, which is similar to struct timeval, however, use nanoseconds instead of microseconds */<br/> struct timespec <br/> {<br/> _ time_t TV _sec;/* seconds */<br/> long int TV _nsec; /* nanoseconds */<br/>}; <br/> # Endif/* timespec is not defined and <time. h> timespec */<br/> # UNDEF _ need_timespec <br/> # ifdef_time_h <br/>__ begin_namespace_std <br/>/* may be used by other time functions. the TM structure of */<br/> struct tm <br/> {<br/> int tm_sec; /* seconds [0-60] (a maximum of 1 leap seconds allowed) */<br/> int tm_min; /* minutes [0-59] */<br/> int tm_hour;/* hours [0-23] */<br/> int tm_mday; /* Date [1-31] */<br/> int tm_mon;/* month [0-11] */<br/> int tm_year; /* The number of annual copies starting from January 1, 1900-1900. */<br/> int t M_wday;/* day of the week [0-6] */<br/> int tm_yday;/* days counted from January 1, January 1 [0-365] */<br/> int tm_isdst; /* Daylight Saving Time sign, 1 Daylight Saving Time, 0 non-Daylight Saving Time,-1 not sure [-1/0/1] */<br/> # ifdef _ use_bsd <br/> long int tm_gmtoff; /* seconds east of UTC */<br/> _ const char * tm_zone; /* Time Zone abbreviation */<br/> # else <br/> long int _ tm_gmtoff; /* seconds east of UTC */<br/> _ const char * _ tm_zone;/* Time Zone abbreviation */<br/> # endif <br/> }; <br/>__ end_namespace_std <br/> # If defined _ use_xopen | defined __ Use_posix | defined _ use_misc <br/>__ using_namespace_std (TM) <br/> # endif <br/> # ifdef _ use_posix199309 <br/>/* structure of the time initial value and interval in posix.1b */<br/> struct itimerspec <BR/>{< br/> struct timespec it_interval; <br/> struct timespec it_value; <br/>}; <br/>/* We can use a simple Forward Declaration */<br/> struct sigevent; <br/> # endif/* posix.1b */<br/> # ifdef _ use_xopen2k <br/> # ifndef _ pid_t_defined <br/> typedef _ pid_t Pid_t; <br/> # DEFINE _ pid_t_defined <br/> # endif <br/>__ begin_namespace_std <br/>/* use time of the program so far (user time + system time) <br/> the returned result/clocks_per_sec is the running time (in seconds) */<br/> extern clock_t clock (void) _ Throw; <br/>/* return the current calendar time and save it to timer. If timer is not null */<br/> extern time_t time (time_t * _ timer) _ Throw; <br/>/* returns the difference between two time1 and time0 values (in seconds) */<br/> extern double difftime (time_t _ time1, time_t _ time0) <Br/> _ Throw _ attribute _ (_ const _); <br/>/* returns the time_t representation of TP, and normalize TP */<br/> extern time_t mktime (struct TM * _ TP) _ Throw; <br/>/* format the TP according to the control string format and save it to S. A maximum of maxsize characters can be written to S, returns the number of characters written in <br/>. If the string length exceeds maxsize, 0 */<br/> extern size_t strftime (char * _ restrict _ s, size_t _ maxsize, <br/>__ const char * _ restrict _ format, <br/>__ const struct TM * _ restrict _ TP) _ Throw; <br/>__ end_na Mespace_std <br/> # ifdef _ use_xopen <br/>/* parses s based on format and saves the binary time information to TP. The returned value is a pointer to the first character not parsed in S */<br/> extern char * strptime (_ const char * _ restrict _ s, <br/> _ const char * _ restrict _ FMT, struct TM * _ TP) <br/> _ Throw; <br/> # endif <br/> # ifdef _ use_gnu <br/>/* is similar to the above two functions, but obtains information from the provided region settings, instead of using global region settings */<br/> # include <xlocale. h> <br/> extern size_t strftime_l (char * _ restrict _ s, size_t _ maxsize, <br/> _ const char * _ restrict _ format, <br/> _ const struct TM * _ restrict _ TP, <br/> _ locale_t _ LOC) _ Throw; <br/> extern char * strptime_l (_ const char * _ restrict _ s, <br/> _ const char * _ restrict _ FMT, struct TM * _ TP, <br/> _ locale_t _ LOC) _ Throw; <br/> # endif <br/>__ begin_namespace_std <br/>/* returns the struct TM representation of timer (UTC for Greenwich Mean Time) */<br/> extern struct TM * gmtime (_ const time_t * _ timer) _ Throw; <br/>/* returns the struct TM representation of timer (local time) */<br/> extern struct TM * localtime (_ const time_t * _ timer) _ Throw; <br/>__ end_namespace_std <br/> # If defined _ use_posix | defined _ use_misc <br/>/* returns the struct TM representation of timer (UTC as the Greenwich Mean Time ), <br/> Use TP to store results */<br/> extern struct TM * gmtime_r (_ const time_t * _ restrict _ timer, <br/> struct TM * _ restrict _ TP) _ Throw; <br/>/* returns the struct TM representation of timer (local time ), use <br/> TP to store results */<br/> extern struct TM * localtime_r (_ const time_t * _ restrict _ timer, <br/> struct TM * _ restrict _ TP) _ Throw; <br/> # endif/* POSIX or MISC */<br/>__ begin_namespace_std <br/>/* returns the printable Date and Time string of TP, format: "day mon dd hh: mm: SS yyyy/N" */<br/> extern char * asctime (_ const struct TM * _ TP) _ Throw; <br/>/* is equivalent to asctime (localtime (timer) */<br/> extern char * ctime (_ const time_t * _ timer) _ Throw; <br/>__ end_namespace_std <br/> # If defined _ use_posix | defined _ use_misc <br/>/* reentrant version of the preceding function */<br/> /* returns the printable Date and Time string of TP, format: "day mon dd hh: mm: SS yyyy/N ", <br/> stored in Buf */<br/> extern char * asctime_r (_ const struct TM * _ restrict _ TP, <br/> char * _ restrict _ BUF) _ Throw; <br/>/* equivalent to asctime_r (localtime_r (timer, * TMP *), Buf) */<br/> extern char * ctime_r (_ const time_t * _ restrict _ timer, <br/> char * _ restrict _ BUF) _ Throw; <br/> # endif/* POSIX or MISC */<br/>/* at localtime. C has defined */<br/> extern char * _ tzname [2];/* Current Time Zone name */<br/> extern int _ daylight; /* If the Daylight Saving Time is used */<br/> extern long int _ timezone; /* number of seconds west of UTC */<br/>/* some other extended functions */<br/>__ end_decls <br/> # endif/* <time. h> sorted ded. */<br/> # endif/* <time. h> not already supported ded. */

2. clock function: returns an approximate value of the processor time, which is represented in clock_t type, usually in microseconds. The standard C clock function only handles the call error (returns-1 and converts it to the clock_t type ). The real implementation uses the Linux clock function. The standard C clock function is directly mapped to the Linux clock function.

/* Clock. c: clock function implementation */<br/> # include <sys/times. h> <br/> # include <time. h> <br/> # include <errno. h> <br/>/* return the time used by the program so far (user time + system time) */<br/> clock_t <br/> clock () <br/>{< br/> _ set_errno (enosys); <br/> return (clock_t)-1; <br/>}< br/> stub_warning (clock) <br/> # include <stub-tag.h>

3. Time Function: returns the current calendar time, expressed in the time_t type. Similar to the preceding clock function, the time function is implemented directly using the Linux time function (when an error occurs in the call is handled independently, that is, the-1 is returned and converted to the time_t type ).

/* Time. c: Implementation of the time function */<br/> # include <errno. h> <br/> # include <time. h> <br/>/* return the current calendar time and save it to timer. If timer is not null */<br/> time_t <br/> time (timer) <br/> time_t * timer; <br/>{< br/> _ set_errno (enosys); <br/> If (timer! = NULL) <br/> * timer = (time_t)-1; <br/> return (time_t)-1; <br/>}< br/> libc_hidden_def (time) <br/> stub_warning (time) <br/> # include <stub-tag.h>

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.