Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Common (5 of 28 files). Show all
Unified · Split
| @@ -45,9 +45,13 @@ const decimal_t Decimal::DotNetMaxValue = decimal_t("792281625142643375935439503 · 44 unmodified lines | ||
| 45 | 45 | |
| 46 | 46 | const decimal_t Decimal::DotNetMinValue = decimal_t("-79228162514264337593543950335"); |
| 47 | 47 | |
| 48 | +const datetime_t DateTime::MaxValue(max_date_time); | |
| 49 | + | |
| 50 | +const datetime_t DateTime::MinValue(min_date_time); | |
| 51 | + | |
| 48 | 52 | const string Empty::String {}; |
| 49 | 53 | |
| 50 | −const DateTime Empty::DateTime {}; | |
| 54 | +const datetime_t Empty::DateTime {}; | |
| 51 | 55 | |
| 52 | 56 | const Guid Empty::Guid = NilGuidGen(); |
| 53 | 57 | |
| @@ -280,7 +284,7 @@ string GSF::PadRight(const string& value, uint32_t count, char padChar) · 226 unmodified lines | ||
| 280 | 284 | return value; |
| 281 | 285 | } |
| 282 | 286 | |
| 283 | −DateTime GSF::DateAdd(const DateTime& value, int32_t addValue, TimeInterval interval) | |
| 287 | +datetime_t GSF::DateAdd(const datetime_t& value, int32_t addValue, TimeInterval interval) | |
| 284 | 288 | { |
| 285 | 289 | switch (interval) |
| 286 | 290 | { |
| @@ -307,7 +311,7 @@ DateTime GSF::DateAdd(const DateTime& value, int32_t addValue, TimeInterval inte · 20 unmodified lines | ||
| 307 | 311 | } |
| 308 | 312 | } |
| 309 | 313 | |
| 310 | −int32_t GSF::DateDiff(const DateTime& startTime, const DateTime& endTime, TimeInterval interval) | |
| 314 | +int32_t GSF::DateDiff(const datetime_t& startTime, const datetime_t& endTime, TimeInterval interval) | |
| 311 | 315 | { |
| 312 | 316 | if (interval < TimeInterval::Hour) |
| 313 | 317 | { |
| @@ -345,7 +349,7 @@ int32_t GSF::DateDiff(const DateTime& startTime, const DateTime& endTime, TimeIn · 31 unmodified lines | ||
| 345 | 349 | } |
| 346 | 350 | } |
| 347 | 351 | |
| 348 | −int32_t GSF::DatePart(const DateTime& value, TimeInterval interval) | |
| 352 | +int32_t GSF::DatePart(const datetime_t& value, TimeInterval interval) | |
| 349 | 353 | { |
| 350 | 354 | static float64_t tickInterval = pow(10.0, time_duration::num_fractional_digits()); |
| 351 | 355 | |
| @@ -376,12 +380,12 @@ int32_t GSF::DatePart(const DateTime& value, TimeInterval interval) · 24 unmodified lines | ||
| 376 | 380 | } |
| 377 | 381 | } |
| 378 | 382 | |
| 379 | −DateTime GSF::Now() | |
| 383 | +datetime_t GSF::Now() | |
| 380 | 384 | { |
| 381 | − return DateTime { microsec_clock::local_time() }; | |
| 385 | + return datetime_t { microsec_clock::local_time() }; | |
| 382 | 386 | } |
| 383 | 387 | |
| 384 | −DateTime GSF::UtcNow() | |
| 388 | +datetime_t GSF::UtcNow() | |
| 385 | 389 | { |
| 386 | − return DateTime { microsec_clock::universal_time() }; | |
| 390 | + return datetime_t { microsec_clock::universal_time() }; | |
| 387 | 391 | } |
| @@ -48,6 +48,9 @@ namespace GSF · 47 unmodified lines | ||
| 48 | 48 | typedef double_t float64_t; |
| 49 | 49 | typedef boost::multiprecision::cpp_dec_float_100 decimal_t; |
| 50 | 50 | |
| 51 | + // DateTime type | |
| 52 | + typedef boost::posix_time::ptime datetime_t; | |
| 53 | + | |
| 51 | 54 | struct Int8 |
| 52 | 55 | { |
| 53 | 56 | static const int8_t MaxValue = static_cast<int8_t>(127); |
| @@ -105,16 +108,26 @@ namespace GSF · 51 unmodified lines | ||
| 105 | 108 | static const decimal_t DotNetMinValue; |
| 106 | 109 | }; |
| 107 | 110 | |
| 111 | + struct DateTime | |
| 112 | + { | |
| 113 | + static const datetime_t MaxValue; | |
| 114 | + static const datetime_t MinValue; | |
| 115 | + }; | |
| 116 | + | |
| 108 | 117 | struct Ticks |
| 109 | 118 | { |
| 119 | + static const int64_t MaxValue = 3155378975999999999L; // 12/31/1999 11:59:59.999 | |
| 120 | + static const int64_t MinValue = 0L; // 01/01/0001 00:00:00.000 | |
| 121 | + | |
| 122 | + static const int64_t UnixBaseOffset = 621355968000000000L; // 01/01/1970 00:00:00.000 | |
| 123 | + static const int64_t PTimeBaseOffset = 441481536000000000L; // 01/01/1400 00:00:00.000 | |
| 124 | + | |
| 110 | 125 | static const int64_t PerSecond = 10000000L; |
| 111 | 126 | static const int64_t PerMillisecond = Ticks::PerSecond / 1000; |
| 112 | 127 | static const int64_t PerMicrosecond = Ticks::PerSecond / 1000000; |
| 113 | 128 | static const int64_t PerMinute = 60L * Ticks::PerSecond; |
| 114 | 129 | static const int64_t PerHour = 60L * Ticks::PerMinute; |
| 115 | 130 | static const int64_t PerDay = 24L * Ticks::PerHour; |
| 116 | − static const int64_t UnixBaseOffset = 621355968000000000L; // 01/01/1970 00:00:00 | |
| 117 | − static const int64_t PtimeBaseOffset = 441481536000000000L; // 01/01/1400 00:00:00 | |
| 118 | 131 | }; |
| 119 | 132 | |
| 120 | 133 | template<class T> |
| @@ -167,7 +180,6 @@ namespace GSF · 46 unmodified lines | ||
| 167 | 180 | |
| 168 | 181 | typedef boost::any Object; |
| 169 | 182 | typedef boost::uuids::uuid Guid; |
| 170 | − typedef boost::posix_time::ptime DateTime; | |
| 171 | 183 | typedef boost::posix_time::time_duration TimeSpan; |
| 172 | 184 | typedef boost::system::error_code ErrorCode; |
| 173 | 185 | typedef boost::system::system_error SystemError; |
| @@ -192,7 +204,7 @@ namespace GSF · 18 unmodified lines | ||
| 192 | 204 | struct Empty |
| 193 | 205 | { |
| 194 | 206 | static const std::string String; |
| 195 | − static const GSF::DateTime DateTime; | |
| 207 | + static const GSF::datetime_t DateTime; | |
| 196 | 208 | static const GSF::Guid Guid; |
| 197 | 209 | static const GSF::Object Object; |
| 198 | 210 | static const GSF::IPAddress IPAddress; |
| @@ -394,11 +406,11 @@ namespace GSF · 195 unmodified lines | ||
| 394 | 406 | Millisecond |
| 395 | 407 | }; |
| 396 | 408 | |
| 397 | − DateTime DateAdd(const DateTime& value, int32_t addValue, TimeInterval interval); | |
| 398 | − int32_t DateDiff(const DateTime& startTime, const DateTime& endTime, TimeInterval interval); | |
| 399 | − int32_t DatePart(const DateTime& value, TimeInterval interval); | |
| 400 | − DateTime Now(); | |
| 401 | − DateTime UtcNow(); | |
| 409 | + datetime_t DateAdd(const datetime_t& value, int32_t addValue, TimeInterval interval); | |
| 410 | + int32_t DateDiff(const datetime_t& startTime, const datetime_t& endTime, TimeInterval interval); | |
| 411 | + int32_t DatePart(const datetime_t& value, TimeInterval interval); | |
| 412 | + datetime_t Now(); | |
| 413 | + datetime_t UtcNow(); | |
| 402 | 414 | } |
| 403 | 415 | |
| 404 | 416 | // Setup standard hash code for Guid |
| @@ -40,7 +40,7 @@ using namespace boost::posix_time; · 39 unmodified lines | ||
| 40 | 40 | using namespace boost::gregorian; |
| 41 | 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 | 44 | const auto DateTimeTicksPerSecond = time_duration::ticks_per_second(); |
| 45 | 45 | |
| 46 | 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 | 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 | 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 | 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 | 173 | const int64_t pticks = int64_t(ticks % Ticks::PerSecond / tickInterval * DateTimeTicksPerSecond); |
| 174 | 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 | 179 | static float64_t tickInterval = pow(10.0, time_duration::num_fractional_digits()); |
| 180 | 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 | 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 | 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 | 248 | std::string GSF::ToString(const Guid& value) |
| @@ -250,7 +250,7 @@ std::string GSF::ToString(const Guid& value) · 1 unmodified line | ||
| 250 | 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 | 255 | stringstream stream; |
| 256 | 256 | time_facet* facet = new time_facet(format); |
| @@ -309,7 +309,35 @@ bool GSF::ParseBoolean(const string& value) · 52 unmodified lines | ||
| 309 | 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 | 342 | try |
| 315 | 343 | { |
| @@ -318,6 +346,7 @@ bool GSF::TryParseDouble(const string& value, float64_t& result) · 2 unmodified lines | ||
| 318 | 346 | } |
| 319 | 347 | catch (...) |
| 320 | 348 | { |
| 349 | + result = defaultValue; | |
| 321 | 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 | 457 | return data; |
| 429 | 458 | } |
| 430 | 459 | |
| 431 | −// 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) | |
| 460 | +// Attempt to parse a timestamp string, e.g.: 2018-03-14T19:23:11.665-04:00 | |
| 461 | +bool GSF::TryParseTimestamp(const char* time, datetime_t& timestamp, const datetime_t& defaultValue, bool parseAsUTC) | |
| 433 | 462 | { |
| 434 | 463 | static const locale formats[] = { |
| 435 | 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 | 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 | 473 | for (int32_t i = 0; i < formatsCount; i++) |
| 442 | 474 | { |
| 443 | − time_duration utcOffset{}; | |
| 444 | − istringstream stream(PreparseTimestamp(time, utcOffset)); | |
| 475 | + istringstream stream(cleanTimestamp); | |
| 445 | 476 | |
| 446 | 477 | stream.imbue(formats[i]); |
| 447 | 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 | 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 | 498 | return timestamp; |
| 467 | 499 | |
| 468 | 500 | throw runtime_error("Failed to parse timestamp \"" + string(time) + "\""); |
| @@ -32,18 +32,18 @@ namespace GSF · 31 unmodified lines | ||
| 32 | 32 | void ToUnixTime(int64_t ticks, time_t& unixSOC, uint16_t& milliseconds); |
| 33 | 33 | |
| 34 | 34 | // Convert Unix second of century and milliseconds to DateTime |
| 35 | − DateTime FromUnixTime(time_t unixSOC, uint16_t milliseconds); | |
| 35 | + datetime_t FromUnixTime(time_t unixSOC, uint16_t milliseconds); | |
| 36 | 36 | |
| 37 | 37 | // Converts a timestamp, in Ticks, to DateTime |
| 38 | − DateTime FromTicks(int64_t ticks); | |
| 38 | + datetime_t FromTicks(int64_t ticks); | |
| 39 | 39 | |
| 40 | 40 | // Converts a DateTime to Ticks |
| 41 | − int64_t ToTicks(const DateTime& time); | |
| 41 | + int64_t ToTicks(const datetime_t& time); | |
| 42 | 42 | |
| 43 | 43 | // Thin wrapper around strftime to provide formats for milliseconds (%f) and full-resolution ticks (%t) |
| 44 | 44 | uint32_t TicksToString(char* ptr, uint32_t maxsize, std::string format, int64_t ticks); |
| 45 | 45 | |
| 46 | − DateTime LocalFromUtc(const DateTime& timestamp); | |
| 46 | + datetime_t LocalFromUtc(const datetime_t& timestamp); | |
| 47 | 47 | |
| 48 | 48 | // Converts an object to a string |
| 49 | 49 | template<class T> |
| @@ -56,7 +56,7 @@ namespace GSF · 6 unmodified lines | ||
| 56 | 56 | |
| 57 | 57 | std::string ToString(const Guid& value); |
| 58 | 58 | |
| 59 | − std::string ToString(const DateTime& value, const char* format = "%Y-%m-%d %H:%M:%S%F"); | |
| 59 | + std::string ToString(const datetime_t& value, const char* format = "%Y-%m-%d %H:%M:%S%F"); | |
| 60 | 60 | |
| 61 | 61 | std::string ToString(const TimeSpan& value); |
| 62 | 62 | |
| @@ -74,7 +74,11 @@ namespace GSF · 11 unmodified lines | ||
| 74 | 74 | |
| 75 | 75 | bool ParseBoolean(const std::string& value); |
| 76 | 76 | |
| 77 | − bool TryParseDouble(const std::string& value, float64_t& result); | |
| 77 | + bool TryParseInt32(const std::string& value, int32_t& result, int32_t defaultValue = 0); | |
| 78 | + | |
| 79 | + bool TryParseInt64(const std::string& value, int64_t& result, int64_t defaultValue = 0L); | |
| 80 | + | |
| 81 | + bool TryParseDouble(const std::string& value, float64_t& result, float64_t defaultValue = 0.0); | |
| 78 | 82 | |
| 79 | 83 | // Encodes a character value into an escaped RegEx value |
| 80 | 84 | std::string RegExEncode(char value); |
| @@ -88,10 +92,10 @@ namespace GSF · 7 unmodified lines | ||
| 88 | 92 | const char* Coalesce(const char* data, const char* nonEmptyValue); |
| 89 | 93 | |
| 90 | 94 | // Attempts to parse an time string in several common formats |
| 91 | − bool TryParseTimestamp(const char* time, DateTime& timestamp, bool parseAsUTC = true); | |
| 95 | + bool TryParseTimestamp(const char* time, datetime_t& timestamp, const datetime_t& defaultValue = DateTime::MinValue, bool parseAsUTC = true); | |
| 92 | 96 | |
| 93 | 97 | // Converts a string to a date-time that may be in several common formats |
| 94 | − DateTime ParseTimestamp(const char* time, bool parseAsUTC = true); | |
| 98 | + datetime_t ParseTimestamp(const char* time, bool parseAsUTC = true); | |
| 95 | 99 | |
| 96 | 100 | // Parses a string of key/value pairs into a case-insensitive string dictionary |
| 97 | 101 | StringMap<std::string> ParseKeyValuePairs(const std::string& value, char parameterDelimiter = ';', char keyValueDelimiter = '=', char startValueDelimiter = '{', char endValueDelimiter = '}'); |
| @@ -324,7 +324,7 @@ namespace GSF · 323 unmodified lines | ||
| 324 | 324 | return {}; |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | − inline std::string ToString(const Nullable<DateTime>& value, const char* fmt = "%Y-%m-%d %H:%M:%S%F") | |
| 327 | + inline std::string ToString(const Nullable<datetime_t>& value, const char* fmt = "%Y-%m-%d %H:%M:%S%F") | |
| 328 | 328 | { |
| 329 | 329 | if (value.HasValue()) |
| 330 | 330 | return ToString(value.GetValueOrDefault(), fmt); |