Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Common/Convert.cpp (1 of 28 files). Show all
Unified · Split
| @@ -40,7 +40,7 @@ using namespace boost::posix_time; · 39 unmodified lines | |||
| 40 | using namespace boost::gregorian; | 40 | using namespace boost::gregorian; |
| 41 | using namespace GSF; | 41 | using namespace GSF; |
| 42 | 42 | ||
| 43 | const DateTime DateTimeEpoch(date(1400, 1, 1), time_duration(0, 0, 0)); | 43 | const datetime_t DateTimeEpoch(date(1400, 1, 1), time_duration(0, 0, 0)); |
| 44 | const auto DateTimeTicksPerSecond = time_duration::ticks_per_second(); | 44 | const auto DateTimeTicksPerSecond = time_duration::ticks_per_second(); |
| 45 | 45 | ||
| 46 | string PreparseTimestamp(const string& timestamp, time_duration& utcOffset) | 46 | string PreparseTimestamp(const string& timestamp, time_duration& utcOffset) |
| @@ -161,24 +161,24 @@ void GSF::ToUnixTime(const int64_t ticks, time_t& unixSOC, uint16_t& millisecond · 114 unmodified lines | |||
| 161 | milliseconds = static_cast<uint16_t>(ticks / 10000 % 1000); | 161 | milliseconds = static_cast<uint16_t>(ticks / 10000 % 1000); |
| 162 | } | 162 | } |
| 163 | 163 | ||
| 164 | DateTime GSF::FromUnixTime(time_t unixSOC, uint16_t milliseconds) | 164 | datetime_t GSF::FromUnixTime(time_t unixSOC, uint16_t milliseconds) |
| 165 | { | 165 | { |
| 166 | return from_time_t(unixSOC) + boost::posix_time::milliseconds(milliseconds); | 166 | return from_time_t(unixSOC) + boost::posix_time::milliseconds(milliseconds); |
| 167 | } | 167 | } |
| 168 | 168 | ||
| 169 | DateTime GSF::FromTicks(const int64_t ticks) | 169 | datetime_t GSF::FromTicks(const int64_t ticks) |
| 170 | { | 170 | { |
| 171 | static float64_t tickInterval = float64_t(Ticks::PerSecond); | 171 | static float64_t tickInterval = float64_t(Ticks::PerSecond); |
| 172 | const DateTime time = from_time_t((ticks - Ticks::UnixBaseOffset) / Ticks::PerSecond); | 172 | const datetime_t time = from_time_t((ticks - Ticks::UnixBaseOffset) / Ticks::PerSecond); |
| 173 | const int64_t pticks = int64_t(ticks % Ticks::PerSecond / tickInterval * DateTimeTicksPerSecond); | 173 | const int64_t pticks = int64_t(ticks % Ticks::PerSecond / tickInterval * DateTimeTicksPerSecond); |
| 174 | return time + time_duration(0, 0, 0, pticks % DateTimeTicksPerSecond); | 174 | return time + time_duration(0, 0, 0, pticks % DateTimeTicksPerSecond); |
| 175 | } | 175 | } |
| 176 | 176 | ||
| 177 | int64_t GSF::ToTicks(const DateTime& time) | 177 | int64_t GSF::ToTicks(const datetime_t& time) |
| 178 | { | 178 | { |
| 179 | static float64_t tickInterval = pow(10.0, time_duration::num_fractional_digits()); | 179 | static float64_t tickInterval = pow(10.0, time_duration::num_fractional_digits()); |
| 180 | const time_duration offset = time - DateTimeEpoch; | 180 | const time_duration offset = time - DateTimeEpoch; |
| 181 | return Ticks::PtimeBaseOffset + offset.total_seconds() * Ticks::PerSecond + | 181 | return Ticks::PTimeBaseOffset + offset.total_seconds() * Ticks::PerSecond + |
| 182 | int64_t(offset.fractional_seconds() / tickInterval * Ticks::PerSecond); | 182 | int64_t(offset.fractional_seconds() / tickInterval * Ticks::PerSecond); |
| 183 | } | 183 | } |
| 184 | 184 | ||
| @@ -240,9 +240,9 @@ uint32_t GSF::TicksToString(char* ptr, uint32_t maxsize, string format, int64_t · 55 unmodified lines | |||
| 240 | return strftime(ptr, maxsize, formatStream.str().data(), &timeinfo); | 240 | return strftime(ptr, maxsize, formatStream.str().data(), &timeinfo); |
| 241 | } | 241 | } |
| 242 | 242 | ||
| 243 | DateTime GSF::LocalFromUtc(const DateTime& timestamp) | 243 | datetime_t GSF::LocalFromUtc(const datetime_t& timestamp) |
| 244 | { | 244 | { |
| 245 | return boost::date_time::c_local_adjustor<DateTime>::utc_to_local(timestamp); | 245 | return boost::date_time::c_local_adjustor<datetime_t>::utc_to_local(timestamp); |
| 246 | } | 246 | } |
| 247 | 247 | ||
| 248 | std::string GSF::ToString(const Guid& value) | 248 | std::string GSF::ToString(const Guid& value) |
| @@ -250,7 +250,7 @@ std::string GSF::ToString(const Guid& value) · 1 unmodified line | |||
| 250 | return boost::uuids::to_string(value); | 250 | return boost::uuids::to_string(value); |
| 251 | } | 251 | } |
| 252 | 252 | ||
| 253 | std::string GSF::ToString(const DateTime& value, const char* format) | 253 | std::string GSF::ToString(const datetime_t& value, const char* format) |
| 254 | { | 254 | { |
| 255 | stringstream stream; | 255 | stringstream stream; |
| 256 | time_facet* facet = new time_facet(format); | 256 | time_facet* facet = new time_facet(format); |
| @@ -309,7 +309,35 @@ bool GSF::ParseBoolean(const string& value) · 52 unmodified lines | |||
| 309 | return false; | 309 | return false; |
| 310 | } | 310 | } |
| 311 | 311 | ||
| 312 | bool GSF::TryParseDouble(const string& value, float64_t& result) | 312 | bool GSF::TryParseInt32(const std::string& value, int32_t& result, const int32_t defaultValue) |
| 313 | { | ||
| 314 | try | ||
| 315 | { | ||
| 316 | result = stoi(value); | ||
| 317 | return true; | ||
| 318 | } | ||
| 319 | catch (...) | ||
| 320 | { | ||
| 321 | result = defaultValue; | ||
| 322 | return false; | ||
| 323 | } | ||
| 324 | } | ||
| 325 | |||
| 326 | bool GSF::TryParseInt64(const std::string& value, int64_t& result, const int64_t defaultValue) | ||
| 327 | { | ||
| 328 | try | ||
| 329 | { | ||
| 330 | result = stoll(value); | ||
| 331 | return true; | ||
| 332 | } | ||
| 333 | catch (...) | ||
| 334 | { | ||
| 335 | result = defaultValue; | ||
| 336 | return false; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | bool GSF::TryParseDouble(const string& value, float64_t& result, const float64_t defaultValue) | ||
| 313 | { | 341 | { |
| 314 | try | 342 | try |
| 315 | { | 343 | { |
| @@ -318,6 +346,7 @@ bool GSF::TryParseDouble(const string& value, float64_t& result) · 2 unmodified lines | |||
| 318 | } | 346 | } |
| 319 | catch (...) | 347 | catch (...) |
| 320 | { | 348 | { |
| 349 | result = defaultValue; | ||
| 321 | return false; | 350 | return false; |
| 322 | } | 351 | } |
| 323 | } | 352 | } |
| @@ -428,8 +457,8 @@ const char* GSF::Coalesce(const char* data, const char* nonEmptyValue) · 104 unmodified lines | |||
| 428 | return data; | 457 | return data; |
| 429 | } | 458 | } |
| 430 | 459 | ||
| 431 | // Parse a timestamp string, e.g.: 2018-03-14T19:23:11.665-04:00 | 460 | // Attempt to parse a timestamp string, e.g.: 2018-03-14T19:23:11.665-04:00 |
| 432 | bool GSF::TryParseTimestamp(const char* time, DateTime& timestamp, bool parseAsUTC) | 461 | bool GSF::TryParseTimestamp(const char* time, datetime_t& timestamp, const datetime_t& defaultValue, bool parseAsUTC) |
| 433 | { | 462 | { |
| 434 | static const locale formats[] = { | 463 | static const locale formats[] = { |
| 435 | locale(locale::classic(), new time_input_facet("%Y-%m-%d %H:%M:%S%F")), | 464 | locale(locale::classic(), new time_input_facet("%Y-%m-%d %H:%M:%S%F")), |
| @@ -438,10 +467,12 @@ bool GSF::TryParseTimestamp(const char* time, DateTime& timestamp, bool parseAsU · 2 unmodified lines | |||
| 438 | 467 | ||
| 439 | static const int32_t formatsCount = sizeof(formats) / sizeof(formats[0]); | 468 | static const int32_t formatsCount = sizeof(formats) / sizeof(formats[0]); |
| 440 | 469 | ||
| 470 | time_duration utcOffset{}; | ||
| 471 | const string cleanTimestamp = PreparseTimestamp(time, utcOffset); | ||
| 472 | |||
| 441 | for (int32_t i = 0; i < formatsCount; i++) | 473 | for (int32_t i = 0; i < formatsCount; i++) |
| 442 | { | 474 | { |
| 443 | time_duration utcOffset{}; | 475 | istringstream stream(cleanTimestamp); |
| 444 | istringstream stream(PreparseTimestamp(time, utcOffset)); | ||
| 445 | 476 | ||
| 446 | stream.imbue(formats[i]); | 477 | stream.imbue(formats[i]); |
| 447 | stream >> timestamp; | 478 | stream >> timestamp; |
| @@ -455,14 +486,15 @@ bool GSF::TryParseTimestamp(const char* time, DateTime& timestamp, bool parseAsU · 7 unmodified lines | |||
| 455 | } | 486 | } |
| 456 | } | 487 | } |
| 457 | 488 | ||
| 489 | timestamp = defaultValue; | ||
| 458 | return false; | 490 | return false; |
| 459 | } | 491 | } |
| 460 | 492 | ||
| 461 | DateTime GSF::ParseTimestamp(const char* time, bool parseAsUTC) | 493 | datetime_t GSF::ParseTimestamp(const char* time, bool parseAsUTC) |
| 462 | { | 494 | { |
| 463 | DateTime timestamp; | 495 | datetime_t timestamp; |
| 464 | 496 | ||
| 465 | if (TryParseTimestamp(time, timestamp, parseAsUTC)) | 497 | if (TryParseTimestamp(time, timestamp, DateTime::MinValue, parseAsUTC)) |
| 466 | return timestamp; | 498 | return timestamp; |
| 467 | 499 | ||
| 468 | throw runtime_error("Failed to parse timestamp \"" + string(time) + "\""); | 500 | throw runtime_error("Failed to parse timestamp \"" + string(time) + "\""); |