#include "../../shared/MiscSupport.h" #include "ImportData.h" static time_t importFromDelphi(double delphiTime) { const int days = (int)delphiTime; // Note: Delphi time is precise to the millisecond. 4.999 seconds means // that we will hit 5 seconds in 1 millisecond, so we should still call it // 4 seconds. 4.999999997 means that there was roundoff error, and this // should be interpreted as 5.000 seconds. So we round to the neareast // millisecond, then truncate to the correct second. We have to follow this // algorithm to show the right time. Clocks that display things precise to // the minute still hit midnight at the same time as clocks that are precise // to the millisecond. If we rounded more, the clock that was precise to the // minute would chime 30 seconds too soon. const int second = (int)((delphiTime - days)*24*60*60+0.0005); struct tm brokenDown; brokenDown.tm_sec = second; brokenDown.tm_min = 0; brokenDown.tm_hour = 0; brokenDown.tm_mday = days - 29220; // 29221.0 in delphi -> 1/1/1980 brokenDown.tm_mon = 0; // 0 = January. brokenDown.tm_year = 80; // 0 = 1900. brokenDown.tm_isdst = -1; // Unknown. Linux figures it out correctly. return mktime(&brokenDown); } static time_t importFromDelphi(std::string const &rawTime) { return importFromDelphi(strtodDefault(rawTime, 0.0)); } time_t importTime(std::string const &rawTime) { if (rawTime.find('.') != rawTime.npos) return importFromDelphi(rawTime); int64_t asInt = strtollDefault(rawTime, 0); static const int64_t MIN_DELPHI_TIME = 3654; static const int64_t MAX_DELPHI_TIME = 76703; // MIN_DELPHI_TIME would be 1/1/1910 if we interpreted it as a Delphi time, // or 1/1/1970 1:00:54 AM Pacific if we interpreted it as UNIX time. // MAX_DELPHI_TIME would be 1/1/2110 if we interpreted it as a Delphi time, // or 1/1/1970 9:18:23 PM. If a value is between these two values, we // assume the input was a Delphi time. Otherwise we assume it's a UNIX time. // This seems like a reasonable guess. if ((asInt < MAX_DELPHI_TIME) && (asInt > MIN_DELPHI_TIME)) return importFromDelphi(asInt); return asInt; } #ifdef __UNIT_TEXT_IMPORT_TIME_ static TclList testTime(double delphi, std::string expected) { TclList result; result<<"delphi"<