Skip to content
Snippets Groups Projects
Commit 33e388ae authored by Sergey Yakubov's avatar Sergey Yakubov
Browse files

do not use get_time

parent 57943b0a
No related branches found
No related tags found
No related merge requests found
......@@ -107,26 +107,44 @@ std::string IsoDateFromEpochNanosecs(uint64_t time_from_epoch_nanosec) {
}
uint64_t NanosecsEpochFromISODate(std::string date_time) {
std::istringstream iss{date_time};
double frac = 0;
int pos = date_time.find_first_of('.');
if (pos!=std::string::npos) {
std::string frac_str = date_time.substr(pos);
if (sscanf(frac_str.c_str(), "%lf", &frac)!=1) {
return 0;
}
date_time = date_time.substr(0, pos);
}
std::tm tm{};
if (!(iss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S"))) {
int year, month, day, hour, minute, second;
hour = 0;
minute = 0;
second = 0;
auto n = sscanf(date_time.c_str(), "%d-%d-%dT%d:%d:%d", &year, &month, &day, &hour, &minute, &second);
if (!(year >= 1970 && month >= 1 && month <= 12 && day >= 1 && day <= 31) || (n != 3 && n != 6)) {
return 0;
}
if ((n==3 && date_time.size()!=10) || (n==6 && date_time.size()!=19)) {
return 0;
}
tm.tm_sec = second;
tm.tm_min = minute;
tm.tm_hour = hour;
tm.tm_mday = day;
tm.tm_mon = month-1;
tm.tm_year = year-1900;
system_clock::time_point tp = system_clock::from_time_t (timegm(&tm));
uint64_t ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(tp).
time_since_epoch().count();
if (iss.eof()) {
return ns > 0 ? ns : 1;
}
double zz = 0;
if (iss.peek() != '.' || !(iss >> zz)) {
return 0;
}
ns = ns + uint64_t(frac * 1000000000) ;
ns = ns + uint64_t(zz * 1000000000);
return ns > 0 ? ns : 1;
}
......
......@@ -136,8 +136,7 @@ auto tests = std::vector<TestEpochFromISODate> {
TestEpochFromISODate{"2019-07-25T15:38:11.100010002", 1564069091100010002},
//errors
TestEpochFromISODate{"1970-13-01T00:00:00.000000002", 0},
TestEpochFromISODate{"1970-12-01T00:00:00,0", 0},
TestEpochFromISODate{"1970-12-01T00:00:00.", 0},
};
TEST(FileInFo, NanosecsEpochFromISODate) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment