Skip to content

Commit f04b80f

Browse files
Updated date/time conversions from ticks to apply value mask so that any leap second bits are not included in converted timestamps.
1 parent 74b9708 commit f04b80f

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/lib/CommonTypes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <cstddef>
3030
#include <map>
3131
#include <unordered_map>
32+
#include <string>
3233

3334
// Reduce boost code analysis warnings
3435
#pragma warning(push)
@@ -196,6 +197,8 @@ namespace sttp
196197

197198
// Flag (63rd bit) that indicates if leap second is positive or negative; 0 for add, 1 for delete.
198199
static constexpr int64_t LeapSecondDirection = 1LL << 62;
200+
201+
static constexpr int64_t ValueMask = ~LeapSecondFlag & ~LeapSecondDirection;
199202
};
200203

201204
inline int32_t ConvertInt32(const size_t value)

src/lib/Convert.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ string PreparseTimestamp(const string& timestamp, TimeSpan& utcOffset)
162162
return updatedTimestamp;
163163
}
164164

165-
void sttp::ToUnixTime(const int64_t ticks, time_t& unixSOC, uint16_t& milliseconds)
165+
void sttp::ToUnixTime(int64_t ticks, time_t& unixSOC, uint16_t& milliseconds)
166166
{
167+
ticks = ticks & Ticks::ValueMask;
168+
167169
// Unix dates are measured as the number of seconds since 1/1/1970
168170
unixSOC = (ticks - Ticks::UnixBaseOffset) / Ticks::PerSecond;
169171

@@ -178,10 +180,13 @@ datetime_t sttp::FromUnixTime(const time_t unixSOC, const uint16_t milliseconds)
178180
return from_time_t(unixSOC) + Milliseconds(milliseconds);
179181
}
180182

181-
datetime_t sttp::FromTicks(const int64_t ticks)
183+
datetime_t sttp::FromTicks(int64_t ticks)
182184
{
185+
ticks = ticks & Ticks::ValueMask;
186+
183187
const datetime_t time = from_time_t((ticks - Ticks::UnixBaseOffset) / Ticks::PerSecond);
184188
const int64_t pticks = ticks % Ticks::PerSecond * DateTimeTicksPerSecond / Ticks::PerSecond;
189+
185190
return time + TimeSpan(0, 0, 0, pticks % DateTimeTicksPerSecond);
186191
}
187192

0 commit comments

Comments
 (0)