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