A good way of storing and working with dates and time in C++.  A structure containing a calendar date and time broken down into its components.

Creating a tm


#include <ctime>
	
	tm my_date_time;

	my_date_time.tm_year = (2015 - 1900);	//years since 1900 (we must be >= 1970 for time_t based calcs)
	my_date_time.tm_mon = 1;		//months since January (0-11)
	my_date_time.tm_mday = 18;
	my_date_time.tm_hour = 12;
	my_date_time.tm_min = 03;
	my_date_time.tm_sec = 48;

Note:

tm_year is years since 1900!!
tm_month is months since January (0-11)!!
Note when converting to time_t your year must be >= 1970 as time_t is usually based from 00:00, Jan 1 1970 UTC!

Loading with current time


#include <time.h>
#include <sys/time.h>

	timeval curTime;
	tm *my_date_time;
	gettimeofday(&curTime, NULL);
	my_date_time = localtime(&curTime.tv_sec);
	 = my_date_time->tm_year - 100;
	 = my_date_time->tm_mon + 1;
	 = my_date_time->tm_mday;
	 = my_date_time->tm_hour;
	 = my_date_time->tm_min;
	 = my_date_time->tm_sec;

Converting tm to Universal Time String


	char my_date_time_string[22];
	strftime(my_date_time_string, sizeof(my_date_time_string), "%Y-%m-%dT%H:%M:%S", &my_date_time);

Convert tm to StringStream


	//Create as yyyymmddhh
	stringstream ss_archive_file_timestamp;
	ss_archive_file_timestamp << (tmFromDate.tm_year + 1900);							//2000 - 2099
	ss_archive_file_timestamp << setfill('0') << setw(2) << (tmFromDate.tm_mon + 1);	//1-12
	ss_archive_file_timestamp << setfill('0') << setw(2) << tmFromDate.tm_mday;			//1-31
	ss_archive_file_timestamp << setfill('0') << setw(2) << tmFromDate.tm_hour;			//0-23

	//Create in UTC format
	stringstream ss_archive_file_timestamp_utc;
	ss_archive_file_timestamp_utc << (tmFromDate.tm_year + 1900) << "-";							//2000 - 2099
	ss_archive_file_timestamp_utc << setfill('0') << setw(2) << (tmFromDate.tm_mon + 1) << "-";		//1-12
	ss_archive_file_timestamp_utc << setfill('0') << setw(2) << tmFromDate.tm_mday << " ";			//1-31
	ss_archive_file_timestamp_utc << setfill('0') << setw(2) << tmFromDate.tm_hour << ":00:00";		//0-23

Convert String To DateTime


	std::tm tm1 = {};
	if (strptime(MyDateCharString, "%Y-%m-%d", &tm1) == NULL)    //Can include time characters too if wanted "%Y-%m-%d %H:%M:%S"
	{
		//Failed

	}

Compare tm’s


	if (mktime(&tm1) > mktime(&tm2))		//Larger values of mktime() are more recent in time
	{
	}

Add, Subtract with tm’s

https://raspberry-projects.com/pi/programming-in-c/datetime/datetime-calculations

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *