C + + time and date

Source: Internet
Author: User

C++ Date & Time

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 &NBSP; 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:

structTm{  IntTm_sec; seconds, normal range from 0 to 59, but allowed to 61 IntTm_min; range from 0 to 59 IntTm_hour; Hours, ranging from 0 to 23 IntTm_mday; The day of the January, ranging 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, from Sunday  int Tm_yday //the day ordinal of the year, ranging from 0 to 365, from January 1  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.
Current date and time

The following instance gets the date and time of the current system, including local time and Coordinated Universal Time (UTC).

#include <iostream>#include <ctime>Using NamespaceStd;IntMain( ){ Current date/time based on current system time_tNow=Time(0); Convert now to String form Char*Dt=CTime(&Now);cout<< "Local Date and Time:" << DT <<; //convert now to TM structure  TM *gmtm  = Gmtime (&now DT = Asctime (gmtm cout <<  "UTC Date and Time:"  DT << Endl;               /span>                

When the above code is compiled and executed, it produces the following result:

Local date and time:SatJan  8: £º date and time:SunJan9 From: £ º             
Format time using Structure TM

TM structures are especially important when dealing with date and time-related operations in C/C + +. The TM structure holds the date and time in the form of a C structure. Most time-related functions use the TM structure. The following example uses a TM structure and various date and time-related functions.

Before practicing using structs, you need to have a basic understanding of C structures and know how to access struct members using the arrow-and operator.

#include <iostream>#include <ctime>Using NamespaceStd;IntMain( ){ Current date/time based on current system time_tNow=Time(0);cout<< "Number of SEC since January 1, 1970:" <<Now<<Endl;Tm*Ltm=LocalTime(&Now); Output the various components of the TM structurecout<< "Year:"<< 1900 +Ltm-Tm_year<<Endl;cout<< "Month:"<< 1 +Ltm-Tm_mon<<Endl;cout<< "Day:"<<Ltm-Tm_mday<<Endl;cout<< "Time:"<< 1 + Ltm->tm_hour <<  ;<< 1 + Ltm->tm_min << span class= "str" ":" ;<< 1 + Ltm->tm_sec << Endl }             /span>                

When the above code is compiled and executed, it produces the following results:

Numberjanuary 1,< Span class= "PLN" > 1970:1294548238year: 2011month: 1day: 8time: 22:  44:59 
Source Link: http://i.cnblogs.com/ Editposts.aspx?opt=1

C + + time and date

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.