Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The conversion method between liunx time function and time format and string

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

We can think of Greenwich mean time (GMT=UTC).

GMT: Greenwich mean time

UTC: time coordination time

1 、 time_t

Time_t time (time_t * t)

Get the number of seconds since January 1, 1970.

The time_t type, which is essentially a long integer (long) that represents the number of seconds from 1970-01-01 00:00:00 to the current timing, while timeval is accurate to milliseconds

2 、 timeval

Timeval type, which is a structural type, and the struct timeval header file is time.h

Struct timeval {time_t tv_sec; / * Seconds. * / second suseconds_t tv_usec; / * Microseconds. * / microseconds}

Use:

Struct timeval tv;gettimeofday (& tv, NULL); printf ("% d\ t% d\ n", tv.tv_usec, tv.tv_sec)

3 、 timezone

Struct timezone {int tz_minuteswest; / * minuteswest of Greenwich * / int tz_dsttime; / * type of DST correction * /}

4 、 struct tm

Tm structure, which is essentially a structure, which contains each time field

Struct tm {int tm_sec; / * seconds after the minute-[0jue 59] * / int tm_min; / * minutes after the hour-[0Magol 59] * / int tm_hour; / * hours since midnight-[0Magol 23] * / int tm_mday; / * day of the month-[1MIT 31] * / int tm_mon; / * months since January-[0J11] * / int tm_year / * years since 1900 * / int tm_wday; / * days since Sunday-[0Yue6] * / int tm_yday; / * days since January 1-[0365] * / int tm_isdst; / * daylight savings time flag * /}

Where tm_year indicates the number of years between 1900 and the present time. If the value is set manually, tm_isdst usually takes a value of-1.

Features:

The tm structure consists of year, month, day, hour, minute and second

Use:

Time_t timeout; struct tm * tmp_ptr = NULL; time (& t_time); printf ("t_time =% d.\ n", t_time); tmp_ptr = gmtime (& t_time) Printf ("after gmtime, the time is:\ nyday =% d\\ nwday =% d\\ nyear =% d\\ nmon =% d\\ nmday =% d\\ nhour =% d\\ nmin =% d\\ n sec =% d\\ n isdst =% d.\ n", tmp_ptr- > tm_yday, tmp_ptr- > tm_wday Tmp_ptr- > tm_year, tmp_ptr- > tm_mon, tmp_ptr- > tm_mday, tmp_ptr- > tm_hour, tmp_ptr- > tm_min, tmp_ptr- > tm_sec, tmp_ptr- > tm_isdst)

Print:

T_time = 1513841065.after gmtime, the time is: yday = 354 wday = 4 year = 117 mon = 11 mday = 21 hour = 7 min = 24 sec = 25 isdst = 0.

5 、 ctime/asctime

Char * ctime (const time_t * timep)

The time to convert timep to the real world, shown as a string, differs from asctime in the form of the parameters passed in.

Char * asctime (const struct tm* timeptr)

Converts the information in the structure into real-world time, displayed in the form of a string.

Char * ctime (const time_t * timer) returns a string representing the local time, which is based on the parameter timer.

The returned string format: Thu Dec 21 13:59:57 2017

Use:

Time_t curtime;struct tm * tm_ptr = NULL;time (& curtime); tm_ptr = localtime (& curtime); printf ("current time of ctime conversion =% s", ctime (& curtime)); printf ("current time of asctime conversion =% s", asctime (tm_ptr))

Print:

Current time of ctime conversion = Thu Dec 21 13:59:57 2017

Current time of asctime conversion = Thu Dec 21 13:59:57 2017

6 、 gmtime/localtime

Struct tm* gmtime (const time_t * timep)

Converts the time represented by time_t to UTC time without time zone conversion, which is a pointer to the struct tm structure.

Stuct tm* localtime (const time_t * timep)

Similar to gmtime, but it is the time of the time zone conversion.

Time_t curtime

The gmtime function converts curtime to Greenwich mean time of struct tm structure, which basically means that gmtime turns out the standard time of 0 time zone.

The localtime function converts curtime to the local time of the struct tm structure, and localtime is the time of the current time zone transferred out, taking into account the time zone.

For example:

Time_t timeout; struct tm * tmp_ptr = NULL; time (& t_time); printf ("t_time =% d.\ n", t_time); tmp_ptr = gmtime (& t_time) Printf ("after gmtime, the time is:\ nyday =% d\\ nwday =% d\\ nyear =% d\\ nmon =% d\\ nmday =% d\\ nhour =% d\\ nmin =% d\\ n sec =% d\\ n isdst =% d.\ n", tmp_ptr- > tm_yday, tmp_ptr- > tm_wday Tmp_ptr- > tm_year, tmp_ptr- > tm_mon, tmp_ptr- > tm_mday, tmp_ptr- > tm_hour, tmp_ptr- > tm_min, tmp_ptr- > tm_sec, tmp_ptr- > tm_isdst) Tmp_ptr = localtime (& t_time) Printf ("after localtime, the time is:\ nyday =% d\\ nwday =% d\\ nyear =% d\\ nmon =% d\\ nmday =% d\\ nhour =% d\\ nmin =% d\\ n sec =% d\\ n isdst =% d.\ n", tmp_ptr- > tm_yday, tmp_ptr- > tm_wday Tmp_ptr- > tm_year, tmp_ptr- > tm_mon, tmp_ptr- > tm_mday, tmp_ptr- > tm_hour, tmp_ptr- > tm_min, tmp_ptr- > tm_sec, tmp_ptr- > tm_isdst) Return 0

Print:

T_time = 1513841065.after gmtime, the time is: yday = 354 wday = 4 year = 117 mon = 11 mday = 21 hour = 21 min = 24 sec = 25 isdst = 0.after localtime, the time is: yday = 354 wday = 4 year = 117 mon = 11 mon = 21 hour = 15 min = 24 sec = 25 isdst = 0.

7 、 mktime

Time_t mktime (struct tm* timeptr)

Convert the time of the struct tm structure to the number of seconds since 1970.

Mktime functions in contrast to gmtime/localtime, gmtime/localtime converts time_t to struct tm structural data, and mktime converts struct tm back to UTC time of time_t type

Examples of use:

Time_t timeout; struct tm * tm_ptr = NULL; time (& t_time); printf ("time_t first time value =% d.\ n", t_time); tm_ptr = gmtime (& t_time) Printf ("time_t switch gmtime type, the time is:\ nyday =% d\\ nwday =% d\\ nyear =% d\\ nmon =% d\\ nmday =% d\\ nhour =% d\\ nmin =% d\\ n sec =% d\\ n isdst =% d.\ n", tm_ptr- > tm_yday Tm_ptr- > tm_wday, tm_ptr- > tm_year, tm_ptr- > tm_mon, tm_ptr- > tm_mday, tm_ptr- > tm_hour, tm_ptr- > tm_min, tm_ptr- > tm_sec, tm_ptr- > tm_isdst) T_time = mktime (tm_ptr); / * re-converted to the UTC time of time_t type. There is a time zone conversion, the time of zone 0 is the time of zone 0, so the time used at once is the time of zone 0 * / printf ("gmtime type switch time_t second time =% d.\ n", t_time)

Print:

Time_t first time value = 1513842674.time_t switch gmtime type, the time is: yday = 354 wday = 4 year = 117 mon = 11 mday = 21 hour = 7 min = 51 sec = 14 isdst = 0.gmtime type switch time_t second time = 1513813874.

8 、 gettimeofday

Int gettimeofday (struct timeval * tv, struct timezone * tz)

Returns the seconds and subtleties of the current distance from 1970, followed by tz, which is generally not used.

Examples of use:

Struct timeval time_val;gettimeofday (& time_val, NULL); / / gettimeofday (& start,&tz); same result printf ("time seconds. Tv_sec:% d.\ n", time_val.tv_sec); printf ("time microseconds. Tv_usec:% d.\ n", time_val.tv_usec)

Print:

Time seconds. Tv_sec:1513843633 time microseconds. Tv_usec:689374

9 、 difftime

Double difftime (time_t time1, time_t time2)

Returns the number of seconds between the two times

Examples of use:

/ / get the time difference time_t tasking start; time_t tasking start; time (& t_start); sleep (5); time (& t_end); printf ("interval time =% f.\ n", difftime (t_end, t_start))

Print:

Interval = 5.000000.

10 、 strftime

Size_t strftime (char * strDest, size_t maxsize, const char * format, const struct tm * timeptr)

Use the strftime () function to format the time into the format we want.

(abbreviation of% a day of week% A full name of day of the week% b full name of month% c time string of standard date% C year last two digits% d decimal representation of the day of the month% D month / day / year% e in the two character field, the last two digits of the% F year-month-day% g year expressed in the decimal system, using the week-based year% G year. Number of minutes in decimal representation of month% m decimal system, number of minutes in% n new line% p local AM or PM display% r 12 hours time% R display hours and minutes: hh:mm%S decimal seconds% t horizontal system Table% T displays minutes and seconds: the day ordinal of the week of hh:mm:ss%u Monday is the week ordinal of the first day (values from 0 to 6, Monday is 0)% U, Sunday is the week ordinal of the first day (values from 0 to 53)% V, using the week-based annual% w decimal system (values from 0 to 6, Sunday is 0) the week ordinal of% W Take Monday as the first day (values from 0 to 53)% x standard date string% X standard time string% y without decimal year of the century (values from 0 to 99)% Y decimal year of the century part of the Z time zone name, if the time zone name cannot be obtained, an empty character is returned. % sign)

Examples of use:

Time_t timekeeping; char buf; struct tm* tm_ptr = NULL; time (& t_time); tm_ptr = localtime (& t_time); / / 2017-12-21 18:53:58 strftime (buf, 64, "% Y-%m-%d% H:%M:%S", tm_ptr); strftime (buf, 64, "% Y-%m-%d--% H:%M:%S", tm_ptr) Printf ("formatTimeString =% s.\ n", buf)

Print:

FormatTimeString = 2017-12-21 18:53:58.formatTimeString = 2017-12-21-- 18:54:46.

11 、 strptime

The function, in contrast to the strftime function, formats the string into a tm structure.

Size_t strftime (char * s format,const struct tm * maxsize,char * timeptr)

Examples of use:

Char buf [] = "2017-12-21-- 18:54:46"; struct tm tm_ptr; / / 2017-12-21 18:53:58 strptime (buf, "% Y-%m-%d--% H:%M:%S", & tm_ptr) Printf ("- strptime-, the time is:\\ nyday =% d\\ nwday =% d\\ nyear =% d\\ nmon =% d\\ nmday =% d\\ n hour =% d\\ nmin =% d\\ n sec =% d.\ n", tm_ptr.tm_yday, tm_ptr.tm_wday Tm_ptr.tm_year, tm_ptr.tm_mon, tm_ptr.tm_mday, tm_ptr.tm_hour, tm_ptr.tm_min, tm_ptr.tm_sec)

Print:

-strptime-, the time is: yday = 354 wday = 4 year = 117 mon = 11 mday = 21 hour = 18 min = 54 sec = 46.

The above liunx time function and time format and string conversion method is the editor to share with you all the content, I hope to give you a reference, but also hope that you support more.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report