当前位置:网站首页>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.
边栏推荐
- Infiltration learning diary day19
- The reasons why QT fails to connect to the database and common solutions
- Pyinstaller packaging py script warning:lib not found and other related issues
- Hamburg University of Technology (tuhh) | intelligent problem solving as integrated hierarchical reinforcement learning
- Setting function of Jerry's watch management device [chapter]
- G3 boiler water treatment registration examination and G3 boiler water treatment theory examination in 2022
- Méthode de calcul de la connexion MSSQL de la carte esp32c3
- Hbuilder link Xiaoyao simulator
- Valentine's Day - 9 jigsaw puzzles with deep love in wechat circle of friends
- Mongodb learning notes: command line tools
猜你喜欢

JVM performance tuning and practical basic theory - medium

What is the student party's Bluetooth headset recommendation? Student party easy to use Bluetooth headset recommended

Should enterprises start building progressive web applications?
![Jerry's synchronous weather information to equipment [chapter]](/img/a9/e09bbf62161ea8ba60b0fce3fb5f89.jpg)
Jerry's synchronous weather information to equipment [chapter]

Pyinstaller packaging py script warning:lib not found and other related issues
![Jerry's watch listens to the message notification of the target third-party software and pushes the message to the device [article]](/img/8b/ff062f34d36e1caa9909c8ab431daf.jpg)
Jerry's watch listens to the message notification of the target third-party software and pushes the message to the device [article]

How programmers find girlfriends through blind dates

Introduction to superresolution

Small program graduation project based on wechat video broadcast small program graduation project opening report function reference

Force buckle day32
随机推荐
Infiltration learning diary day19
PMP daily three questions (February 14, 2022)
Pyinstaller packaging py script warning:lib not found and other related issues
Maximum likelihood method, likelihood function and log likelihood function
Prose article appreciation - the rain in the warm country has never changed into cold, hard and brilliant flowers. Knowledgeable people think he is monotonous, and he thinks he is unlucky, doesn't he?
SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution
A. Min Max Swap
Take you to master the formatter of visual studio code
Introduction to graphics: graphic painting (I)
Logical operator, displacement operator
From the 18th line to the first line, the new story of the network security industry
Solution to the problem that jsp language cannot be recognized in idea
Huawei cloud micro certification Huawei cloud computing service practice has been stable
Solution of cursor thickening
MySQL statement learning record
Why is the operation unsuccessful (unresolved) uncaught syntaxerror: invalid or unexpected token (resolved)
Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
MySQL advanced (Advanced) SQL statement (I)
Winter vacation daily question -- a single element in an ordered array
TP5 automatic registration hook mechanism hook extension, with a complete case