Time Related library <ctime> analysis

Source: Internet
Author: User
Tags iso 8601 month name

Original works, reproduced please specify the source: http://www.cnblogs.com/shrimp-can/p/5649487.html

I. Types of definitions

1.clock_t: Clock type

2.size_t: unsigned int

3.time_t: Time Type

4.struct TM: Structure, members are as follows:

Member Type meaning Range
tm_sec int Seconds after the minute 0-60*
tm_min int Minutes after the hour 0-59
tm_hour int Hours since midnight 0-23
tm_mday int Day of the Month 1-31
tm_mon int Months since January 0-11
tm_year int years since 1900
tm_wday int Days since Sunday 0-6
tm_yday int Days since January 1 0-365
tm_isdst int Daylight Saving Time Flag

Second, the definition of the macro

1.null: null pointer

2.clocks_per_sec: Clock cycles per second

Three, time operation function

1.clock: clock_t clock (void);

Returns the current clock period, converted to s divided by clocks_per_sec

2.difftime: Double difftime (time_t end, time_t beginning);

Calculates the time unit seconds between geginning and end

3.mktime: time_t mktime (struct TM * timeptr);

Returns the time that timeptr points to, and timeptr updates the other.

  time_t rawtime;   struct  TM * TIMEINFO;   int  year, month, day;  Const   Char  * weekday[] = { "Sunday" ,  "Monday" ,  "Tuesday"   ,  "Wednesday" ,  "Thursday" ,  "Friday" ,  "Saturday" }; printf ( "Enter Year:" ); Fflush (stdout);  scanf ( "%d" , &year); printf ( Enter Month: ); Fflush (stdout);  scanf ( "%d" , &month); printf ( "Enter Day:" ); Fflush (stdout);  scanf ( "%d" , &day);  Time (&rawtime);  Timeinfo = LocalTime (&rawtime);  Timeinfo->tm_year = year-1900;  Timeinfo->tm_mon = month-1;  Timeinfo->tm_mday = day;  Mktime (Timeinfo); printf ( that's a%s.\n ", Weekday[timeinfo->tm_wday]); 
Output is:
Enter year: 2000Enter month: 5Enter day: 20That day is a Saturday.

4.Time: time_t time (time_t* timer);

Gets the current calendar time, stored in the timer, and returns a null pointer if the fetch fails, returning the current calendar time successfully

Iv. Time Conversion Correlation function

1.asctime: char* asctime (const struct TM * timeptr);

Converts the time of the TM type to the form of a string, returning a string such as www Mmm dd hh:mm:ss yyyy

2.ctime: char* ctime (const time_t * timer);

Converts the time of the time_t type to the form of a string, returning a string such as: Www Mmm dd HH:MM:SS yyyy

3.gmtime: struct TM * gmtime (const time_t * timer);

Converts the time type of the time_t to UTC time, and the corresponding time is stored in the Fabric TM

4.localtime: struct TM * localtime (const time_t * timer);

Use the time in the timer to populate localtime

5.strftime: size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr);

Copies the time of the structure TM description to the string ptr, the maximum maxsize character, as formatted in format. The formate format is as follows:

  time_t rawtime;  struct tm * timeinfo;  char buffer [80];  time (&rawtime);  timeinfo = localtime (&rawtime);  strftime (buffer,80,"Now it‘s %I:%M%p.",timeinfo);  puts (buffer);

 输出为:

Now it‘s 03:21PM.

by
specifier replacedExample
%a Abbreviated Weekday name * Thu
%A Full Weekday name * Thursday
%b Abbreviated Month name * Aug
%B Full month name * August
%c Date and Time representation * Thu Aug 23 14:55:02 2001
%C Year divided by + truncated to Integer ( 00-99 ) 20
%d Day of the month, zero-padded ( 01-31 ) 23
%D Short MM/DD/YY date, equivalent to%m/%d/%y 08/23/01
%e Day of the month, space-padded (  1-31 ) 23
%F Short YYYY-MM-DD date, equivalent to%Y-%m-%d 2001-08-23
%g Week-based year, last digits ( 00-99 ) 01
%G Week-based year 2001
%h Abbreviated Month name * (same as %b ) Aug
%H Hour in 24h format ( 00-23 ) 14
%I Hour in 12h format ( 01-12 ) 02
%j Day of the Year ( 001-366 ) 235
%m Month as a decimal number ( 01-12 ) 08
%M Minute ( 00-59 ) 55
%n New-line character ( ‘\n‘ )
%p AM or PM designation PM
%r 12-hour clock time * 02:55:02 pm
%R 24-hour time HH:MM , equivalent to%H:%M 14:55
%S Second ( 00-61 ) 02
%t Horizontal-tab character ( ‘\t‘ )
%T ISO 8601 time Format ( HH:MM:SS ), equivalent to%H:%M:%S 14:55:02
%u ISO 8601 Weekday as number with Monday as 1 ( 1-7 ) 4
%U Week number with the first Sunday as the first day of Week one ( 00-53 ) 33
%V ISO 8601 Week number ( 00-53 ) 34
%w Weekday as a decimal number with Sunday as 0 ( 0-6 ) 4
%W Week number with the first Monday as the first day of Week one ( 00-53 ) 34
%x Date representation * 08/23/01
%X Time representation * 14:55:02
%y year, last digits ( 00-99 ) 01
%Y Year 2001
%z ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100)
If timezone cannot be determined, no characters
+100
%Z Timezone name or abbreviation *
If timezone cannot be determined, no characters
CDT
%% A Sign % %

Reference: http://www.cplusplus.com/reference/ctime/

Time Related library <ctime> analysis

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.