C + + Date & Time

Source: Internet
Author: User
Tags local time month name time and date

The C + + standard library does not provide a so-called date type. C + + inherits the structure and functions used for the date and time operations of the language. In order to use date and time-related functions and structures, you need to reference the <ctime> header file in a C + + program.

There are four time-related types:clock_t, time_t, size_t , and TM. Types clock_t, size_t, and time_t can represent system time and date as some sort of integer.

The structure Type TM holds the date and time in the form of a C structure, the TM structure is defined as follows:

struct TM { int tm_sec; //sec, normal range from 0 to 59, but allowed to int tm_min; //min, range from 0 to < Span class= "Hl-code" > < Span class= "Hl-code" > int tm_hour; //hours, range from 0 to < Span class= "hl-types" > < Span class= "Hl-identifier" > < Span class= "hl-comment" > int tm_mday//January of the day, range from 1 to + < Span class= "hl-comment" > < Span class= "Hl-code" > int tm_mon //month, range from 0 to < Span class= "hl-comment" > < Span class= "Hl-code" > int tm_year//number of years since 1900 < Span class= "Hl-code" > < Span class= "Hl-code" > < Span class= "hl-comment" > int tm_wday; //the day of the week, ranging from 0 to 6, from Sunday < Span class= "Hl-code" > < Span class= "Hl-code" > < Span class= "hl-comment" > int tm_yday; //the day of the year, ranging from 0 to 365, from January 1 < Span class= "Hl-code" > < Span class= "hl-comment" > int tm_isdst //Daylight Saving time }

The following are important functions for dates and times in C/s + +. All of these functions are part of the C + + standard library, and you can see the details of each function in the C + + standard library.

Serial Number Functions & Descriptions
1 time_t time (time_t *time);
This function returns the current calendar time of the system, the number of seconds elapsed since January 1, 1970. If the system does not have time, it returns. 1.
2 Char *ctime (const time_t *time);
This returns a string pointer representing the local time, in string form, day month of Hours:minutes:seconds year\n\0.
3 struct TM *localtime (const time_t *time);
The function returns a pointer to the TM structure representing the local time.
4 clock_t clock (void);
This function returns the time that the processor clock was used by the program execution (typically the beginning of the program). Returns the. 1 if the time is not available.
5 char * asctime (CONST struct TM * time);
The function returns a pointer to a string containing information stored in the structure that time points to, returned in the form: Day month date hours:minutes:seconds year\n\0.
6 struct TM *gmtime (const time_t *time);
The function returns a pointer to time, which is the TM structure, expressed in Coordinated Universal Time (UTC), which is also known as GMT (GMT).
7 time_t mktime (struct TM *time);
The function returns the calendar time, which is the amount of time stored in the structure that is pointed to.
8 Double Difftime (time_t time2, time_t time1);
The function returns the number of seconds between time1 and Time2.
9 size_t strftime ();
The function can be used to format the date and time for the specified format.
C Library Function-strftime ()Describe

C Library Functions size_t strftime (char *str, size_t maxsize, const char *format, const struct TM *timeptr) according to format The formatting rules that are defined in the format structure timeptr The time represented by, and store it in str .

Statement

The following is a declaration of the Strftime () function.

size_t strftime(char*str,size_t maxsize,constChar*  Format,conststruct*timeptr)        
Parameters
    • Str -This is a pointer to the target array to copy the resulting C string.
    • MaxSize -This is the maximum number of characters that are copied to Str.
    • format -This is a C string that contains any combination of ordinary characters and special format specifiers. These format specifiers are replaced by functions with relative values that represent the time specified in the TM. The format specifier is:
specifier replaced by Example
%a Abbreviated day of the week is called Sun
%A Full day of the week is called Sunday
%b Abbreviated month name Mar
%B Full month name March
%c Date and time notation Sun 19 02:56:02 2012
%d The day of the January (01-31) 19
%H Hours in 24-hour format (00-23) 14
%I Hours in 12-hour format (01-12) 05
%j Day of the Year (001-366) 231
%m The Month in decimal number (01-12) 08
%M Points (00-59) 55
%p AM or PM name Pm
%s Seconds (00-61) 02
%u Week of the year, first Sunday as the first day of the first week (00-53) 33
%w The number of days of the week in decimal notation, expressed in Sunday as 0 (0-6) 4
%W The first week of the year, with the first Monday as the first day of the Week (00-53) 34
%x Date notation 08/19/12
%x Time notation 02:50:06
%y Year, last two digits (00-99) 01
%Y Year 2012
%Z Name or abbreviation of the time zone Cdt
%% A% symbol %
    • timeptr -This is a pointer to the TM structure that contains a calendar time that is decomposed into the following sections:

structTm{   IntTm_sec; /* seconds, range from 0 to 59*/ IntTm_min; /* min, range from 0 to 59*/ IntTm_hour; /* hours, range from 0 to 23*/ IntTm_mday; /* Day of January, range from 1 to 31 */ int Tm_mon;  / * month, range from 0 to 11*/ int tm_year;  / * Number of years since 1900 */ int tm_wday;  / * The day of the week, ranging from 0 to 6 */ int tm_yday;  / * The day of the year, ranging from 0 to 365 */ int tm_isdst;  / * Daylight Saving Time */};                
return value

If the resulting C string is less than a size character (including a null end character), the total number of characters copied to STR (excluding the null end character) is returned, otherwise zero is returned.

C + + Date & Time

Related Article

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.