Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Common/Convert.cpp (1 of 24 files). Show all
Unified · Split
| @@ -37,143 +37,143 @@ using namespace GSF::TimeSeries; · 36 unmodified lines | |||
| 37 | 37 | ||
| 38 | void GSF::TimeSeries::GetUnixTime(const int64_t ticks, time_t& unixSOC, int16_t& milliseconds) | 38 | void GSF::TimeSeries::GetUnixTime(const int64_t ticks, time_t& unixSOC, int16_t& milliseconds) |
| 39 | { | 39 | { |
| 40 | // Unix dates are measured as the number of seconds since 1/1/1970 | 40 | // Unix dates are measured as the number of seconds since 1/1/1970 |
| 41 | const int64_t BaseTimeOffset = 621355968000000000L; | 41 | const int64_t BaseTimeOffset = 621355968000000000L; |
| 42 | 42 | ||
| 43 | unixSOC = static_cast<time_t>((ticks - BaseTimeOffset) / 10000000); | 43 | unixSOC = static_cast<time_t>((ticks - BaseTimeOffset) / 10000000); |
| 44 | milliseconds = static_cast<int16_t>(ticks / 10000 % 1000); | 44 | milliseconds = static_cast<int16_t>(ticks / 10000 % 1000); |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | size_t GSF::TimeSeries::TicksToString(char* ptr, size_t maxsize, string format, int64_t ticks) | 47 | size_t GSF::TimeSeries::TicksToString(char* ptr, size_t maxsize, string format, int64_t ticks) |
| 48 | { | 48 | { |
| 49 | time_t fromSeconds; | 49 | time_t fromSeconds; |
| 50 | int16_t milliseconds; | 50 | int16_t milliseconds; |
| 51 | 51 | ||
| 52 | GetUnixTime(ticks, fromSeconds, milliseconds); | 52 | GetUnixTime(ticks, fromSeconds, milliseconds); |
| 53 | 53 | ||
| 54 | stringstream formatStream; | 54 | stringstream formatStream; |
| 55 | size_t formatIndex = 0; | 55 | size_t formatIndex = 0; |
| 56 | 56 | ||
| 57 | while (formatIndex < format.size()) | 57 | while (formatIndex < format.size()) |
| 58 | { | 58 | { |
| 59 | char c = format[formatIndex]; | 59 | char c = format[formatIndex]; |
| 60 | ++formatIndex; | 60 | ++formatIndex; |
| 61 | 61 | ||
| 62 | if (c != '%') | 62 | if (c != '%') |
| 63 | { | 63 | { |
| 64 | // Not a format specifier | 64 | // Not a format specifier |
| 65 | formatStream << c; | 65 | formatStream << c; |
| 66 | continue; | 66 | continue; |
| 67 | } | 67 | } |
| 68 | 68 | ||
| 69 | // Check for %f and %t format specifiers and handle them | 69 | // Check for %f and %t format specifiers and handle them |
| 70 | // accordingly. All other specifiers get forwarded to strftime | 70 | // accordingly. All other specifiers get forwarded to strftime |
| 71 | c = format[formatIndex]; | 71 | c = format[formatIndex]; |
| 72 | ++formatIndex; | 72 | ++formatIndex; |
| 73 | 73 | ||
| 74 | switch (c) | 74 | switch (c) |
| 75 | { | 75 | { |
| 76 | case 'f': | 76 | case 'f': |
| 77 | { | 77 | { |
| 78 | stringstream temp; | 78 | stringstream temp; |
| 79 | temp << setw(3) << setfill('0') << milliseconds; | 79 | temp << setw(3) << setfill('0') << milliseconds; |
| 80 | formatStream << temp.str(); | 80 | formatStream << temp.str(); |
| 81 | break; | 81 | break; |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | case 't': | 84 | case 't': |
| 85 | formatStream << ticks; | 85 | formatStream << ticks; |
| 86 | break; | 86 | break; |
| 87 | 87 | ||
| 88 | default: | 88 | default: |
| 89 | formatStream << '%' << c; | 89 | formatStream << '%' << c; |
| 90 | break; | 90 | break; |
| 91 | } | 91 | } |
| 92 | } | 92 | } |
| 93 | 93 | ||
| 94 | struct tm timeinfo; | 94 | struct tm timeinfo; |
| 95 | gmtime_s(&timeinfo, &fromSeconds); | 95 | gmtime_s(&timeinfo, &fromSeconds); |
| 96 | 96 | ||
| 97 | return strftime(ptr, maxsize, formatStream.str().data(), &timeinfo); | 97 | return strftime(ptr, maxsize, formatStream.str().data(), &timeinfo); |
| 98 | } | 98 | } |
| 99 | 99 | ||
| 100 | template <class T> | 100 | template <class T> |
| 101 | string GSF::TimeSeries::ToString(const T& obj) | 101 | string GSF::TimeSeries::ToString(const T& obj) |
| 102 | { | 102 | { |
| 103 | stringstream stream; | 103 | stringstream stream; |
| 104 | stream << obj; | 104 | stream << obj; |
| 105 | return stream.str(); | 105 | return stream.str(); |
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | Guid GSF::TimeSeries::ToGuid(const uint8_t* data, bool swapBytes) | 108 | Guid GSF::TimeSeries::ToGuid(const uint8_t* data, bool swapBytes) |
| 109 | { | 109 | { |
| 110 | Guid id; | 110 | Guid id; |
| 111 | uint8_t swappedBytes[16]; | 111 | uint8_t swappedBytes[16]; |
| 112 | uint8_t* encodedBytes; | 112 | uint8_t* encodedBytes; |
| 113 | 113 | ||
| 114 | // Check if bytes need to be decoded in reverse order | 114 | // Check if bytes need to be decoded in reverse order |
| 115 | if (swapBytes) | 115 | if (swapBytes) |
| 116 | { | 116 | { |
| 117 | uint8_t copy[8]; | 117 | uint8_t copy[8]; |
| 118 | 118 | ||
| 119 | for (size_t i = 0; i < 16; i++) | 119 | for (size_t i = 0; i < 16; i++) |
| 120 | { | 120 | { |
| 121 | swappedBytes[i] = data[15 - i]; | 121 | swappedBytes[i] = data[15 - i]; |
| 122 | 122 | ||
| 123 | if (i < 8) | 123 | if (i < 8) |
| 124 | copy[i] = swappedBytes[i]; | 124 | copy[i] = swappedBytes[i]; |
| 125 | } | 125 | } |
| 126 | 126 | ||
| 127 | // Convert Microsoft encoding to RFC | 127 | // Convert Microsoft encoding to RFC |
| 128 | swappedBytes[3] = copy[0]; | 128 | swappedBytes[3] = copy[0]; |
| 129 | swappedBytes[2] = copy[1]; | 129 | swappedBytes[2] = copy[1]; |
| 130 | swappedBytes[1] = copy[2]; | 130 | swappedBytes[1] = copy[2]; |
| 131 | swappedBytes[0] = copy[3]; | 131 | swappedBytes[0] = copy[3]; |
| 132 | 132 | ||
| 133 | swappedBytes[4] = copy[5]; | 133 | swappedBytes[4] = copy[5]; |
| 134 | swappedBytes[5] = copy[4]; | 134 | swappedBytes[5] = copy[4]; |
| 135 | 135 | ||
| 136 | swappedBytes[6] = copy[7]; | 136 | swappedBytes[6] = copy[7]; |
| 137 | swappedBytes[7] = copy[6]; | 137 | swappedBytes[7] = copy[6]; |
| 138 | 138 | ||
| 139 | encodedBytes = swappedBytes; | 139 | encodedBytes = swappedBytes; |
| 140 | } | 140 | } |
| 141 | else | 141 | else |
| 142 | { | 142 | { |
| 143 | encodedBytes = const_cast<uint8_t*>(data); | 143 | encodedBytes = const_cast<uint8_t*>(data); |
| 144 | } | 144 | } |
| 145 | 145 | ||
| 146 | for (Guid::iterator iter = id.begin(); iter != id.end(); ++iter, ++encodedBytes) | 146 | for (Guid::iterator iter = id.begin(); iter != id.end(); ++iter, ++encodedBytes) |
| 147 | *iter = static_cast<Guid::value_type>(*encodedBytes); | 147 | *iter = static_cast<Guid::value_type>(*encodedBytes); |
| 148 | 148 | ||
| 149 | return id; | 149 | return id; |
| 150 | } | 150 | } |
| 151 | 151 | ||
| 152 | Guid GSF::TimeSeries::ToGuid(const char* data) | 152 | Guid GSF::TimeSeries::ToGuid(const char* data) |
| 153 | { | 153 | { |
| 154 | const string_generator generator; | 154 | const string_generator generator; |
| 155 | return generator(data); | 155 | return generator(data); |
| 156 | } | 156 | } |
| 157 | 157 | ||
| 158 | const char* GSF::TimeSeries::Coalesce(const char* data, const char* nonEmptyValue) | 158 | const char* GSF::TimeSeries::Coalesce(const char* data, const char* nonEmptyValue) |
| 159 | { | 159 | { |
| 160 | if (data == nullptr) | 160 | if (data == nullptr) |
| 161 | return nonEmptyValue; | 161 | return nonEmptyValue; |
| 162 | 162 | ||
| 163 | if (strlen(data) == 0) | 163 | if (strlen(data) == 0) |
| 164 | return nonEmptyValue; | 164 | return nonEmptyValue; |
| 165 | 165 | ||
| 166 | return data; | 166 | return data; |
| 167 | } | 167 | } |
| 168 | 168 | ||
| 169 | time_t GSF::TimeSeries::ParseXMLTimestamp(const char* time) | 169 | time_t GSF::TimeSeries::ParseXMLTimestamp(const char* time) |
| 170 | { | 170 | { |
| 171 | istringstream in { time }; | 171 | istringstream in { time }; |
| 172 | sys_seconds timestamp; | 172 | sys_seconds timestamp; |
| 173 | 173 | ||
| 174 | // Parse an XML formatted timestamp string, e.g.: 2018-03-14T19:23:11.665-04:00, | 174 | // Parse an XML formatted timestamp string, e.g.: 2018-03-14T19:23:11.665-04:00, |
| 175 | // using the Hinnant date library: https://github.com/HowardHinnant/date | 175 | // using the Hinnant date library: https://github.com/HowardHinnant/date |
| 176 | in >> parse("%Y-%m-%dT%T%z", timestamp); | 176 | in >> parse("%Y-%m-%dT%T%z", timestamp); |
| 177 | 177 | ||
| 178 | return system_clock::to_time_t(timestamp); | 178 | return system_clock::to_time_t(timestamp); |
| 179 | } | 179 | } |