Latest web development tutorials

C ++ Date & Time

C ++ standard library does not provide the so-called date type. C ++ inherited the structure and function of the C language for the date and time of the operation. In order to use the date and time related functions and structures need to reference the <ctime> header file in C ++ programs.

There are four time-relatedtypes: clock_t, time_t, size_t and tm.Type clock_t, size_t and time_t the system time and date can be represented as some sort of integer.

Structure typetm to the date and time in the form of C structures stored 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; // 夏令时
}

The following is a C / C ++ function on important dates and times. All of these functions are part of the C / C ++ standard library, you can look at the details of each function in the C ++ standard library.

序号函数 & 描述
1 time_t time(time_t *time);
该函数返回系统的当前日历时间,自 1970 年 1 月 1 日以来经过的秒数。如果系统没有时间,则返回 .1。
2 char *ctime(const time_t *time);
该返回一个表示当地时间的字符串指针,字符串形式day month year hours:minutes:seconds year\n\0
3 struct tm *localtime(const time_t *time);
该函数返回一个指向表示本地时间的tm结构的指针。
4 clock_t clock(void);
该函数返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。如果时间不可用,则返回 .1。
5 char * asctime ( const struct tm * time );
该函数返回一个指向字符串的指针,字符串包含了 time 所指向结构中存储的信息,返回形式为:day month date hours:minutes:seconds year\n\0。
6 struct tm *gmtime(const time_t *time);
该函数返回一个指向 time 的指针,time 为 tm 结构,用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。
7 time_t mktime(struct tm *time);
该函数返回日历时间,相当于 time 所指向结构中存储的时间。
8 double difftime ( time_t time2, time_t time1 );
该函数返回 time1 和 time2 之间相差的秒数。
9 size_t strftime();
该函数可用于格式化日期和时间为指定的格式。

The current date and time

The following examples get the date and time of the current system, including local time and Coordinated Universal 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

Use formatting time tm structure

Whentm structure with dates and time-related operations in C / C ++, and is particularly important.tm structure in the form of C structures to save the date and time. Most of the time-related functions use the tm structure. The following example uses the tm structure and various related with the date and time functions.

Before practice using the structure, you need to have a basic understanding of the C structure, and know how to use the arrow -> operator to access structure members.

#include <iostream>
#include <ctime>

using namespace std;

int main( )
{
   // 基于当前系统的当前日期/时间
   time_t now = time(0);

   cout << "Number of sec since January 1,1970:" << now << endl;

   tm *ltm = localtime(&now);

   // 输出 tm 结构的各个组成部分
   cout << "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 << ":";
   cout << 1 + ltm->tm_min << ":";
   cout << 1 + ltm->tm_sec << endl;
}

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

Number of sec since January 1, 1970:1294548238
Year: 2011
Month: 1
Day: 8
Time: 22: 44:59