C + + Date & Time

Source: Internet
Author: User

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 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;   // 秒,正常范围从 0 到 59,但允许至 61  int tm_min;   // 分,范围从 0 到 59  int tm_hour;  // 小时,范围从 0 到 23  int tm_mday;  // 一月中的第几天,范围从 1 到 31  int tm_mon;   // 月,范围从 0 到 11  int tm_year;  // 自 1900 年起的年数  int tm_wday;  // 一周中的第几天,范围从 0 到 6,从星期日算起  int tm_yday;  // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起  int tm_isdst; // 夏令时}
Current date and time

The following instance gets the date and time of the current system.

#include <iostream>#include <ctime> using namespace std; int main( ){    /* 基于当前系统的当前日期/时间 */    time_t now = time(0);        /* 把 now 转换为字符串形式 */    char* dt = ctime(&now);        cout << "本地日期和时间:" << dt << endl;        return 0;}
Format time using Structure TM

The TM structure is especially important when dealing with date and time-related operations in 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.

#include <iostream>#include <ctime> using namespace std; int main(){   /* 基于当前系统的当前日期/时间 */   time_t now = time(0);    cout << "1970 到目前经过秒数:" << now << endl;    tm *ltm = localtime(&now);    /* 输出 tm 结构的各个组成部分 */   cout << "年: "<< 1900 + ltm->tm_year << endl;   cout << "月: "<< 1 + ltm->tm_mon<< endl;   cout << "日: "<<  ltm->tm_mday << endl;   cout << "时间: "<< ltm->tm_hour << ":";   cout << ltm->tm_min << ":";   cout << ltm->tm_sec << endl;      return 0;}
Complements the important functions of date and time in C + +:
Serial Number Functions & Descriptions
1 time_t time (time_t *time) This function returns the current calendar times 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), which returns a string pointer representing the local times, as a string of day month, Hours:minutes:seconds Year\n\0.
3 struct TM localtime (const time_t time); The function returns a pointer to the TM structure representing local times.
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:secon DS year\n\0.
6 struct TM gmtime (const time_t time); The function returns a pointer to the hour, 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); This function returns the calendar time, which is the amount of time that is 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 (); This function can be used to format the date and time for the specified format.

Reference from "Rookie Tutorial"

C + + Date & Time

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.