API Documentation
Loading...
Searching...
No Matches
NtpTimestamp.h
Go to the documentation of this file.
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)
10 /**
11 * 64-bit NTP timestamp.
12 */
13 struct NtpTimestamp {
14 /** Number of seconds passed since Jan 1 1900, in big-endian format. */
15 quint32 seconds;
16
17 /** Fractional time part, in <tt>1/0xFFFFFFFF</tt>s of a second. */
18 quint32 fraction;
19
21 /**
22 * @param dateTime The Time to convert to NTP structure.
23 */
24 NtpTimestamp(const Time& dateTime)
25 {
26 /* Convert given time to number of milliseconds passed since Jan 1 1900. */
27 qint64 ntpMSecs = dateTime.getMilliseconds() + 2208988800000ULL;
28
29 /* Note that NTP epoch information is lost here. Assume 1900-2036 NTP epoch. */
30 quint32 s = ntpMSecs / 1000;
31 quint32 f = 0x100000000ll * (ntpMSecs % 1000) / 1000;
32
33 /* Convert to big-endian. */
34 seconds = qToBigEndian(s);
35 fraction = qToBigEndian(f);
36 }
37
38 /**
39 * @returns Time object for the given NTP timestamp.
40 */
41 Time toTime() const
42 {
43 /* Convert to local-endian. */
44 quint32 s = qFromBigEndian(seconds);
45 quint32 f = qFromBigEndian(fraction);
46
47 /* Convert NTP timestamp to number of milliseconds passed since Jan 1 1900. */
48 qint64 ntpMSecs = s * 1000ll + f * 1000ll / 0x100000000ll;
49
50 /* Construct Qt date time. */
51 return Time(Time::MILLISECOND * (ntpMSecs - 2208988800000LL));
52 }
53 };
54 static_assert(sizeof(NtpTimestamp) == 8);
55#pragma pack(pop)
56}
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:54
static constexpr uint08 MILLISECOND
Definition Time.h:83
uint08 getMilliseconds() const
Gets the timestamp in milliseconds.
Definition Time.h:193
Definition ACIColor.h:37
Definition NtpTimestamp.h:13
quint32 seconds
Definition NtpTimestamp.h:15
NtpTimestamp(const Time &dateTime)
Definition NtpTimestamp.h:24
Time toTime() const
Definition NtpTimestamp.h:41
quint32 fraction
Definition NtpTimestamp.h:18
NtpTimestamp()
Definition NtpTimestamp.h:20