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
20 /**
21 * @param dateTime Qt date time.
22 * @returns Ntp time for the given qt
23 */
25 NtpTimestamp(const Time& dateTime)
26 {
27 /* Convert given time to number of milliseconds passed since Jan 1 1900. */
28 qint64 ntpMSecs = dateTime.getMilliseconds() + 2208988800000ULL;
29
30 /* Note that NTP epoch information is lost here. Assume 1900-2036 NTP epoch. */
31 quint32 s = ntpMSecs / 1000;
32 quint32 f = 0x100000000ll * (ntpMSecs % 1000) / 1000;
33
34 /* Convert to big-endian. */
35 seconds = qToBigEndian(s);
36 fraction = qToBigEndian(f);
37 }
38
39 /**
40 * @param ntpTime NTP timestamp.
41 * @returns Qt date time for the given NTP timestamp.
42 */
43 Time toTime() const
44 {
45 /* Convert to local-endian. */
46 quint32 s = qFromBigEndian(seconds);
47 quint32 f = qFromBigEndian(fraction);
48
49 /* Convert NTP timestamp to number of milliseconds passed since Jan 1 1900. */
50 qint64 ntpMSecs = s * 1000ll + f * 1000ll / 0x100000000ll;
51
52 /* Construct Qt date time. */
53 return Time(Time::MILLISECOND * (ntpMSecs - 2208988800000LL));
54 }
55 };
56 static_assert(sizeof(NtpTimestamp) == 8);
57#pragma pack(pop)
58}
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:25
Time toTime() const
Definition NtpTimestamp.h:43
quint32 fraction
Definition NtpTimestamp.h:18
NtpTimestamp()
Definition NtpTimestamp.h:24