❤️ c + + get the current time, there is a small detail! ❤️

c + + get the current time

1. Basic concepts

  • time stamp

Timestamp is a time representation. It is defined as the total number of seconds from 00:00:00 on January 1, 1970 Greenwich mean time to the present.

typedef __int64 __time64_t;

typedef __time64_t time_t;

In c + +, we define the data type time_t is the timestamp. At present, a considerable number of operating systems use 32-bit binary numbers to represent time. The timestamp of such systems can be used up to 03:14:07 GMT on January 19, 2038 (binary: 01111111 11111111 11111111). In the next second, the binary number will change to 10000000 00000000 00000000 00000000, and an overflow error occurs, causing the system to misunderstand the time as 20:45:52 on December 13, 1901. This is likely to cause software failure or even system paralysis. Systems that use 64 bit binary numbers to represent time (up to 15:30:08 GMT 2922770265, December 4, 1996) will basically not encounter such overflow problems.

Therefore, time is in the source code of c + +_ T is defined as a 64 bit int, and interesting knowledge is added! This wave is really thin!

  • storage structure

c + + uses a structure named tm to store time, in which the year is the number of years since 1900, rather than directly storing. For example, in 2011, the month starts from 0, 0 indicates January, and the week also starts from 0, 0 indicates Sunday and 1 indicates Monday.

struct tm
{
    int tm_sec;  /*Seconds, normal range 0-59, but allowed to 61*/
    int tm_min;  /*Minutes, 0-59*/
    int tm_hour; /*Hours, 0-23*/
    int tm_mday; /*Day, i.e. the day of the month, 1-31*/
    int tm_mon;  /*Month, from January, 0-11*/  1+p->tm_mon;
    int tm_year;  /*How many years has it been since 1900*/  1900+ p->tm_year;
    int tm_wday; /*Week, the day of the week, from Sunday, 0-6*/
    int tm_yday; /*The number of days from January 1 this year to the present, ranging from 0-365*/
    int tm_isdst; /*Daylight saving time flag*/
};

2. Application examples

  • Header file

#include <time.h>

  • Output time

Output the current time in yy/mm/dd hh:mm:ss format

#include <iostream>
#include <string>
#include <time.h>

std::string GetNowTime() {
	time_t setTime;
	//Get timestamp
	time(&setTime);
	tm ptm;
	//The current time is calculated by timestamp
	localtime_s(&ptm, &setTime);
	std::string time = std::to_string(ptm.tm_year + 1900)
		+ "/"
		+ std::to_string(ptm.tm_mon + 1)
		+ "/"
		+ std::to_string(ptm.tm_mday)
		+ " "
		+ std::to_string(ptm.tm_hour) + ":"
		+ std::to_string(ptm.tm_min) + ":"
		+ std::to_string(ptm.tm_sec);
	return time;
}

//test
int main(int argc, char *argv[]) {
	std::string strTime = GetNowTime();
	std::cout << "now time: " << strTime << std::endl;
	system("pause");
	return 0;
}
  • localtime,localtime_s and Localtime_ Difference and use of R

localtime,localtime_s,localtime_r is used to obtain the system time, where localtime_r is used to obtain system time under Linux platform, localtime_s is used to obtain the system time on the Windows platform, while the system time obtained by Localtime does not distinguish the system.

When multithreading is not involved and there is only one system time, for example, to obtain the current system time, use localtime; However, when multiple times need to be converted into the format of system time or used in multithreading, you should choose to use localtime according to the system type_ S or localtime_r.

Note: as for why the three functions that look almost the same are specifically mentioned, in fact, the localtime compilation directly tells me that it is unsafe. I think it is unsafe to change one. Can I write the requirements? Du Niang told me localtime_s works, so it works when windows writes code, but it's cool when linux is deployed, and it's changed to localtime_r. So I decided to give it three direct Internet access b waves, brothers, am I right?

❤️ Come here, don't you like it, don't you pay attention, don't you nag? Thank you very much!!! ❤️

Keywords: C++ Linux

Added by BittenApple on Mon, 10 Jan 2022 21:18:27 +0200