1
0
Fork 0

timetag values to/from system time

This commit is contained in:
Kevin Matz 2023-04-24 13:04:49 -04:00
parent 4023cdaa81
commit 23572904d2
2 changed files with 54 additions and 0 deletions

View File

@ -94,6 +94,56 @@ void blob::oStream(std::shared_ptr<bufferstream> stream) const
}
/**
* @brief timetag::time_point
* @return
*
* Convert \cite NTPv4 NTP timestamp to a system referanced time_point, with
* not greater than nanosecond resolution.
*
* \warning \cite Spec11 OSC Spec 1.1 leaves unchanged the reliance on the 64-bit NTP
* timestamp format. The NTP Epoch 0 ends on February 7, 2036, at which time OSC
* timetag values will rollover.
*/
std::chrono::time_point<std::chrono::system_clock> timetag::time() const
{
std::chrono::time_point<std::chrono::system_clock> time; // POSIX epoch
time -= std::chrono::seconds(2208988800); // NTP epoch offset
time += std::chrono::seconds(seconds);
time += std::chrono::nanoseconds(
(uint32_t)(((double)fractional
*(1.0/(double)std::numeric_limits<typeof(fractional)>::max()))
*1e9));
/// \test validate time conversion from fractional seconds.
return time;
}
/**
* @brief timetag::setTime
* @param time
*
* Convert system referanced time_point to \cite NTPv4 NTP timestamps, with
* not greater than nanosecond resolution.
*
* \warning \cite Spec11 OSC Spec 1.1 leaves unchanged the reliance on the 64-bit NTP
* timestamp format. The NTP Epoch 0 ends on February 7, 2036, at which time OSC
* timetag values will rollover.
*/
void timetag::setTime(std::chrono::time_point<std::chrono::system_clock> time)
{
auto t = time.time_since_epoch(); // POSIX epoch
auto s = std::chrono::duration_cast<std::chrono::seconds>(t);
auto f = std::chrono::duration_cast<std::chrono::nanoseconds>(t - s);
s += std::chrono::seconds(2208988800); // NTP epoch offset
seconds = s.count();
fractional = ((double)f.count()
*1e-9)
*(1.0/(double)std::numeric_limits<typeof(fractional)>::max());
/// \test validate time conversion to fractional seconds.
}
void character::iStream(std::shared_ptr<bufferstream> stream)
{
value = stream->readType<int32_t>();

View File

@ -24,6 +24,7 @@
#pragma once
#include <bufferstream.h>
#include <chrono>
#include <vector>
namespace OSC {
@ -136,6 +137,9 @@ struct timetag
{
timetag() : Argument('t') {};
std::chrono::time_point<std::chrono::system_clock> time() const;
void setTime(std::chrono::time_point<std::chrono::system_clock> time = std::chrono::system_clock::now());
virtual size_t streamSize() const override { return 8; }
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }