C++ Date & Time


The C++ standard library does not provide a so-called date type. C++ inherits the C language's structures and functions for date and time operations. In order to use date and time related functions and structures, the <ctime> header file needs to be referenced in the C++ program.

There are four time-related types: clock_t, time_t, size_t and tm. The types clock_t, size_t, and time_t represent the system time and date as certain integers.

Structure typetm Save date and time in the form of C structure. The definition of tm structure is 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 about date and time in C/C++ important functions. All these functions are part of the C/C++ standard library. You can check the details of each function in the C++ standard library.

456789

Current date and time

The following example obtains the current system date and time, 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

Formatting time using structure tm

tm structure It is particularly important when dealing with date and time related operations in C/C++. The tm structure holds the date and time as a C structure. Most time-related functions use the tm structure. The following example uses the tm structure and various date and time related functions.

Before practicing using structures, you need to have a basic understanding of C structures 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
Serial numberFunction & Description
1time_t time( time_t *time);
This function returns the current calendar time of the system, the number of seconds that have passed since January 1, 1970. If the system has no time, .1 is returned.
2char *ctime(const time_t *time);
This returns a string pointer representing the local time, character String format day month year hours:minutes:seconds year\n\0.
3struct tm *localtime(const time_t *time);
This function returns a pointer to ## representing the local time #tm Pointer to the structure.
clock_t clock(void);This function returns the processor when the program is executed (usually the beginning of the program) The time used by the clock. If the time is not available, .1 is returned.
char * asctime ( const struct tm * time );This function returns a pointer to a string, character The string contains the information stored in the structure pointed to by time, and the return format is: day month date hours:minutes:seconds year\n\0.
struct tm *gmtime(const time_t *time);This function returns a pointer to time, time is tm structure, expressed in Coordinated Universal Time (UTC) also known as Greenwich Mean Time (GMT).
time_t mktime(struct tm *time);This function returns the calendar time, which is equivalent to the structure pointed to by time Storage time.
double difftime ( time_t time2, time_t time1 );This function returns the number of seconds difference between time1 and time2 .
size_t strftime();This function can be used to format the date and time into the specified format.