May 11, 2021 C++
The so-called date type is not available in the standard library of C+ . T he structure and functions of the C language for date and time operations are inherited by C. In order to use date- and time-related functions and structures, you need to refer to the header file in the C++ program.
There are four time-related types: clock_t, time_t, size_t, and tm. Type clock_t, size_t, and time_t can represent system time and date as some kind of integer.
The structure type tm saves the date and time as a C structure, which 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; // 夏令时 }
Here are some important functions on dates and times in C/C. All of these functions are part of the C/C?standard library, and you can see the details of each function in the C?standard library.
Serial number | Function & Description |
---|---|
1 |
time_t time(time_t *time);
This function returns the current calendar time of the system, since the number of seconds that have passed 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 indicating local time, string form day month year hours:minutes:seconds year\n 。 |
3 |
struct tm *localtime(const time_t *time);
This function returns a point to representative local time. tm Structure pointer. |
4 |
clock_t clock(void);
This function returns the time (generally the beginning of the program), the time used by the processor clock.Returns .1 if time is not available. |
5 |
char * asctime ( const struct tm * time );
This function returns a pointer to the string, and the string contains information stored in the configuration of TIME, and the return form is: DAY MONTH DATE HOURS: Minutes: seconds year \ n \ 0. |
6 |
struct tm *gmtime(const time_t *time);
This function returns a pointer to the Time, TIME is a TM structure, and is also known as the GMT time (GMT) representation with coordination world (UTC). |
7 |
time_t mktime(struct tm *time);
This function returns to the calendar time, which is equivalent to the time stored in the structure of Time. |
8 |
double difftime ( time_t time2, time_t time1 );
This function returns a number of seconds between TIME1 and TIME2. |
9 |
size_t strftime();
This function can be used to format the date and time as the specified format. |
The following example gets the date and time of the current system, including local time and coordinated world time (UTC).
#include <iostream>
#include <ctime>
using namespace std;
int main( )
{
// 基于当前系统的当前日期/时间
time_t now = time(0);
// 把 now 转换为字符串形式
char* dt = ctime(&now);
cout << "本地日期和时间:" << dt << endl;
// 把 now 转换为 tm 结构
tm *gmtm = gmtime(&now);
dt = asctime(gmtm);
cout << "UTC 日期和时间:"<< dt << endl;
}
When the above code is compiled and executed, it produces the following results:
本地日期和时间:Sat Jan 8 20:07:41 2011 UTC 日期和时间:Sun Jan 9 03:07:41 2011
The tm structure is particularly important when dealing with date- and time-related operations in C/C. T he tm structure holds the date and time as a C structure. M ost time-related functions use the tm structure. The following example uses the tm structure and various functions related to date and time.
Before you practice using structures, you need to have a basic understanding of the C structure and how to use the arrow-gt; operator to access structure members.
#include <iostream>
#include <ctime>
using namespace std;
int main( )
{
// 基于当前系统的当前日期/时间
time_t now = time(0);
cout << "1970年1月1日到目前经过的秒数:" << 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 << "时间: "<< 1 + ltm->tm_hour << ":";
cout << 1 + ltm->tm_min << ":";
cout << 1 + ltm->tm_sec << endl;
}
When the above code is compiled and executed, it produces the following results:
1970年1月1日到目前经过的秒数:1524456057 年: 2018 月: 4 日: 23 时间: 5:1:58