Converting decimal date to unixtime

Hello,
I’m writing a program to analise data in C++ (using ROOT’s objects). It should load data from textfile, and then plot it on histogram. The problem is one of the file has time expressed in decimal date, like 2017.552, and the rest of them are in unixtime. That’s a piece of my code of loading data from .txt:

` std:ifstream inputData(filename.Data());
 double fractiontime(0.0);
 double sunspot(0.0);
 double deviation(0.0);

while (!inputData.eof()){
	fractiontime=0.0;
	sunspot=0.0;
	deviation=0.0;
			
   inputData>>fractiontime>>sunspot>>deviation>>;
		
	if (fractiontime>2017.500&&sunspot>-1){
		h1->Fill(fractiontime,sunspot);
		h2->Fill(fractiontime);
		h3->Fill(fractiontime,deviation);
	}
}
inputData.close();`

I need to change the format of time before filling histograms (h1,h2,h3) to unixtime and I’m not sure how to do that.

does it mean 2017 + (0.552 year) ? as a year does not have a fixed length in second that might be not the case.

Yes, it’s a format of day/month/year, so 2017.552 is 21.07.2017, 2017.555 is 22.07.2017 etc.

I understand how you get 2017 … but how do you get 21.07 from .552 ?

// Howard Hinnant's date.h
// see https://github.com/HowardHinnant/date/blob/master/include/date/date.h
#include <date.h> 
using namespace date;
double y_with_fraction = 2017.552;
int y = y_with_fraction; // cast to int here
auto yBegin = sys_days(year{y}/jan/1);
auto daysInYear = (sys_days(year{y + 1}/jan/1) - yBegin).count();
auto result = yBegin + chrono::seconds(static_cast<int>((y_with_fraction - y)*86400*daysInYear));

// output result (Variant 1):
time_t tt = chrono::system_clock::to_time_t(result);
std::tm t;
gmtime_r(&tt, &t);
cout << put_time(&t, "%c") << '\n';

// output result (Variant 2):
cout << year_month_day{floor<days>(result)} << '\n';

Outputs (1): Fri Jul 21 11:31:11 2017
Outputs (2): 2017-07-21

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.