当前位置:网站首页>13. Time conversion function
13. Time conversion function
2022-07-04 01:58:00 【666QAQ】
time_t Convert to printable format
1.ctime()
#include <time.h>
char *ctime(const time_t *timep);
Returns pointer to statically allocated string terminated
by newline and \0 on success, or NULL on error
Will return a long 26 Byte string , Include date and time in standard format ,
Such as :Thu Feb 10 23:11:07 2022
The string contains a newline character and a terminating empty byte .ctime() Function when converting , The local time zone and DST Take into account , The returned string is statically allocated , Next time, yes ctime() Will be overridden by a call to .
SUSv3 The standard stipulates , call ctime()、gmtime()、localTime() or asctime() Any function in , May overwrite the return results of other functions ( Statically allocated data structures ). in other words , These functions can share the returned character array and tm Structure .
time_t And decomposition time
time_t Decomposition time
function gmtime() and localtime() Can be one time_t Value is converted to a so-called decomposition time (broken-down time). The decomposition time is placed in a statically allocated structure , Its address is returned as the result of the function .
#include <time.h>
struct tm *gmtime(const time_t *timep);
struct tm *localtime(const time_t *timep);
Both returns a pointer to a statically allocated broken-down time
structure on success, or NULL on error.
gmtime() Convert the calendar time to correspond to UTC Decomposition time of .( Letter GM Indicates Greenwich mean time ).
by comparison , function localtime() Time zone and daylight saving time settings need to be considered , Returns a decomposition time corresponding to the system local time .
tm structure
struct tm{
int tm_sec; /*Seconds (00-60)*/
int tm_min; /*Minutes (0-59)*/
int tm_hour; /*Hours (0-23)*/
int tm_mday; /*Day of the month (1-31)*/
int tm_mon; /*Month (0-11)*/
int tm_year; /*Year since 1900*/
int tm_wday; /*Day of the week (Sundat = 0)*/
int tm_yday; /*Day in the year (0-365; 1 Jan = 0)*/
int tm_isdst; /*Daylight saving time flag >0 :DST is effect =0 :DST is not effect <0 :DST infomation not available*/
};
Decomposition time turns time_t
function mktime() Translate the decomposition time of a local time zone into time_t value , And return it as the result of the function , The caller places the decomposition time in a tm structure , And then to timeptr The pointer points to the structure . This transformation ignores the input tm The structure of the tm_wday and tm_yday Field .
#include <time.h>
time_t mktime(struct tm *timeptr);
Returns seconds since the Epoch corresponding to timeptr
on success, or (time_t)-1 on error
function mktime() It may change timeptr The structure that it points to , At least Will ensure that tm_mday and tm_wday Setting of field value , It can correspond to other input fields .
Besides ,mktime() Does not require tm Other fields of the structure are limited by the scope , The value of any field is out of range ,mktime() all It will be adjusted within the effective range , And adjust other fields appropriately . All these adjustments , Both occur in mktime() to update tm_mday and tm_wday Field and calculate the return value time_t Before .
for example :
if tm_sec by 123, Then the value of this field will be 3, meanwhile tm_min Field value will +2( If the cause tm_min overflow , Will continue to adjust , Increasing tm_hour Value , And so on .) These adjustments even apply to negative field values , for example , Appoint tm_sec by -1 It means the... Of the previous minute 59 second .
This function allows you to calculate the date and time by decomposing the time , Therefore, it is very useful .
Conversion between decomposition time and printing time
Decomposition time to print format
asctime()
#include <time.h>
char *asctime(const struct tm *timeptr);
Returns pointer to statically allocated string terminated by newline
and \0 on success, or NULL on error
Compared with ctime(), The local time zone setting is correct asctime() No impact , Because what it converts is a decomposition time .asctime() The reentrant version is asctime_r()
strftime()
#include <time.h>
size_t strftime(char *outstr, size_t maxsize, const char *format,
const struct tm *timrptr);
Returns number of bytes placed in outstr (excluding terminating null
byte) on success, or 0 on error.
format Parameters are strings , See strftime(3) Manual page , These conversion specifiers all conform to SUSv3 standard .
%a The abbreviated name of the day of the week according to the current locale.
(Calculated from tm_wday.)
%A The full name of the day of the week according to the current locale. (Calcu‐
lated from tm_wday.)
%b The abbreviated month name according to the current locale. (Calculated from
tm_mon.)
%B The full month name according to the current locale. (Calculated from
tm_mon.)
%c The preferred date and time representation for the current locale.
%C The century number (year/100) as a 2-digit integer. (SU) (Calculated from
tm_year.)
%d The day of the month as a decimal number (range 01 to 31). (Calculated from
tm_mday.)
%D Equivalent to %m/%d/%y. (Yecch—for Americans only. Americans should note
that in other countries %d/%m/%y is rather common. This means that in inter‐
national context this format is ambiguous and should not be used.) (SU)
%e Like %d, the day of the month as a decimal number, but a leading zero is re‐
placed by a space. (SU) (Calculated from tm_mday.)
%E Modifier: use alternative format, see below. (SU)
%F Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
%G The ISO 8601 week-based year (see NOTES) with century as a decimal number.
The 4-digit year corresponding to the ISO week number (see %V). This has the
same format and value as %Y, except that if the ISO week number belongs to the
previous or next year, that year is used instead. (TZ) (Calculated from
tm_year, tm_yday, and tm_wday.)
%g Like %G, but without century, that is, with a 2-digit year (00–99). (TZ) (Cal‐
culated from tm_year, tm_yday, and tm_wday.)
%h Equivalent to %b. (SU)
%H The hour as a decimal number using a 24-hour clock (range 00 to 23). (Calcu‐
lated from tm_hour.)
%I The hour as a decimal number using a 12-hour clock (range 01 to 12). (Calcu‐
lated from tm_hour.)
%j The day of the year as a decimal number (range 001 to 366). (Calculated from
tm_yday.)
%k The hour (24-hour clock) as a decimal number (range 0 to 23); single digits
are preceded by a blank. (See also %H.) (Calculated from tm_hour.) (TZ)
%l The hour (12-hour clock) as a decimal number (range 1 to 12); single digits
are preceded by a blank. (See also %I.) (Calculated from tm_hour.) (TZ)
%m The month as a decimal number (range 01 to 12). (Calculated from tm_mon.)
%M The minute as a decimal number (range 00 to 59). (Calculated from tm_min.)
%n A newline character. (SU)
%O Modifier: use alternative format, see below. (SU)
%p Either "AM" or "PM" according to the given time value, or the corresponding
strings for the current locale. Noon is treated as "PM" and midnight as "AM".
(Calculated from tm_hour.)
%P Like %p but in lowercase: "am" or "pm" or a corresponding string for the cur‐
rent locale. (Calculated from tm_hour.) (GNU)
%r The time in a.m. or p.m. notation. In the POSIX locale this is equivalent to
%I:%M:%S %p. (SU)
%R The time in 24-hour notation (%H:%M). (SU) For a version including the sec‐
onds, see %T below.
%s The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
(Calculated from mktime(tm).)
%S The second as a decimal number (range 00 to 60). (The range is up to 60 to
allow for occasional leap seconds.) (Calculated from tm_sec.)
%t A tab character. (SU)
%T The time in 24-hour notation (%H:%M:%S). (SU)
%u The day of the week as a decimal, range 1 to 7, Monday being 1. See also %w.
(Calculated from tm_wday.) (SU)
%U The week number of the current year as a decimal number, range 00 to 53,
starting with the first Sunday as the first day of week 01. See also %V and
%W. (Calculated from tm_yday and tm_wday.)
%V The ISO 8601 week number (see NOTES) of the current year as a decimal number,
range 01 to 53, where week 1 is the first week that has at least 4 days in the
new year. See also %U and %W. (Calculated from tm_year, tm_yday, and
tm_wday.) (SU)
%w The day of the week as a decimal, range 0 to 6, Sunday being 0. See also %u.
(Calculated from tm_wday.)
%W The week number of the current year as a decimal number, range 00 to 53,
starting with the first Monday as the first day of week 01. (Calculated from
tm_yday and tm_wday.)
%x The preferred date representation for the current locale without the time.
%X The preferred time representation for the current locale without the date.
%y The year as a decimal number without a century (range 00 to 99). (Calculated
from tm_year)
%Y The year as a decimal number including the century. (Calculated from tm_year)
%z The +hhmm or -hhmm numeric timezone (that is, the hour and minute offset from
UTC). (SU)
%Z The timezone name or abbreviation.
%+ The date and time in date(1) format. (TZ) (Not supported in glibc2.)
%% A literal '%' character.
边栏推荐
- Introduction to graphics: graphic painting (I)
- Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them
- Bacteriostatic circle scanning correction template
- When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
- Jerry's update contact [article]
- TP5 automatic registration hook mechanism hook extension, with a complete case
- Valentine's Day - 9 jigsaw puzzles with deep love in wechat circle of friends
- Push technology practice | master these two tuning skills to speed up tidb performance a thousand times!
- Remember another interview trip to Ali, which ends on three sides
- Hbuilder link Xiaoyao simulator
猜你喜欢

SQL statement

Example 072 calculation of salary it is known that the base salary of an employee of a company is 500 yuan. The amount of software sold by the employee and the Commission method are as follows: Sales

Should enterprises start building progressive web applications?

Remember another interview trip to Ali, which ends on three sides

Conditional test, if, case conditional test statements of shell script

Audio resource settings for U3D resource management

The automatic control system of pump station has powerful functions and diverse application scenarios

Solution of cursor thickening

LeetCode 168. Detailed explanation of Excel list name

Huawei cloud micro certification Huawei cloud computing service practice has been stable
随机推荐
Notice on Soliciting Opinions on the draft of information security technology mobile Internet application (APP) life cycle security management guide
The difference between lambda expressions and anonymous inner classes
[leetcode daily question] a single element in an ordered array
After listening to the system clear message notification, Jerry informed the device side to delete the message [article]
Development of user-defined navigation bar in uniapp
How to view the computing power of GPU?
Small program graduation project based on wechat examination small program graduation project opening report function reference
Mongodb learning notes: command line tools
MySQL statement learning record
What are the main investment products of bond funds and what are they
Idea if a class cannot be found, it will be red
MySQL deadly serial question 2 -- are you familiar with MySQL index?
mysql使用視圖報錯,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]
Hunan University | robust Multi-Agent Reinforcement Learning in noisy environment
Small program graduation project based on wechat reservation small program graduation project opening report reference
Maximum entropy model
G3 boiler water treatment registration examination and G3 boiler water treatment theory examination in 2022
Chain ide -- the infrastructure of the metauniverse
Applet graduation design is based on wechat course appointment registration. Applet graduation design opening report function reference