NDEVR
API Documentation
NtpTimestamp.h
1#pragma once
2#include <NDEVR/Time.h>
3#include <QtCore/QtGlobal>
4#include <QtCore/QtEndian>
5#include <QtCore/QDateTime>
6
7namespace NDEVR
8{
9#pragma pack(push, 1)
13 struct NtpTimestamp {
15 quint32 seconds;
16
18 quint32 fraction;
19
27 NtpTimestamp(const Time& dateTime)
28 {
29 /* Convert given time to number of milliseconds passed since Jan 1 1900. */
30 qint64 ntpMSecs = dateTime.getMilliseconds() + 2208988800000ULL;
31
32 /* Note that NTP epoch information is lost here. Assume 1900-2036 NTP epoch. */
33 quint32 s = ntpMSecs / 1000;
34 quint32 f = 0x100000000ll * (ntpMSecs % 1000) / 1000;
35
36 /* Convert to big-endian. */
37 seconds = qToBigEndian(s);
38 fraction = qToBigEndian(f);
39 }
40
44 Time toTime() const
45 {
46 /* Convert to local-endian. */
47 quint32 s = qFromBigEndian(seconds);
48 quint32 f = qFromBigEndian(fraction);
49
50 /* Convert NTP timestamp to number of milliseconds passed since Jan 1 1900. */
51 qint64 ntpMSecs = s * 1000ll + f * 1000ll / 0x100000000ll;
52
53 /* Construct Qt date time. */
54 return Time(Time::MILLISECOND * (ntpMSecs - 2208988800000LL));
55 }
56 };
57 static_assert(sizeof(NtpTimestamp) == 8);
58#pragma pack(pop)
59}
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:62
uint08 getMilliseconds() const
Gets the timestamp in milliseconds since 1970.
Definition Time.h:275
The primary namespace for the NDEVR SDK.
Time toTime() const
NtpTimestamp(const Time &dateTime)
quint32 seconds
Number of seconds passed since Jan 1 1900, in big-endian format.
NtpTimestamp()
Default constructor.
quint32 fraction
Fractional time part, in 1/0xFFFFFFFFs of a second.