Skip to content

Commit 61533e2

Browse files
Fix request's received_time (#459)
* Use std::chrono for the request received_time. * Update Development/nmos/api_utils.cpp * Update Development/nmos/tai.h * Update links --------- Co-authored-by: jonathan-r-thorpe <64410119+jonathan-r-thorpe@users.noreply.github.com>
1 parent 300e5e2 commit 61533e2

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

Development/nmos/api_utils.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,15 @@ namespace nmos
701701
add_api_finally_handler(api_, hsts, gate);
702702
auto api = [api_, &gate](web::http::http_request req) mutable
703703
{
704+
// hmm, in Windows, the boost version of the time_point::now sometimes returns the same value after a small time increment.
705+
// This issue does not seem to happen in the std::chrono implementation.
706+
#if defined(_WIN32) && defined (BST_THREAD_BOOST)
707+
// calculate received_time using std::chrono
708+
const auto now = tai_clock::time_point(tai_clock::duration(std::chrono::system_clock::now().time_since_epoch().count() + tai_clock::tai_offset().count()));
709+
req.headers().add(details::received_time, nmos::make_version(tai_from_time_point(now)));
710+
#else
704711
req.headers().add(details::received_time, nmos::make_version());
712+
#endif
705713
slog::log<slog::severities::too_much_info>(gate, SLOG_FLF)
706714
<< stash_remote_address(req.remote_address())
707715
<< stash_http_method(req.method())

Development/nmos/tai.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace nmos
2828
// tai_clock::time_point (or tai_clock::duration, since NMOS uses relative timestamps in a few places)
2929
};
3030

31-
// tai_clock is a std::chrono Clock for generating and manipulating tai timestamps
31+
// tai_clock is a bst::chrono Clock for generating and manipulating tai timestamps
3232
struct tai_clock
3333
{
3434
// "It is suggested (although not mandated) that these timestamps are stored with nanosecond resolution."
@@ -46,6 +46,9 @@ namespace nmos
4646
// time points; nmos::strictly_increasing_update is used to prevent duplicate values in nmos::resources
4747
static const bool is_steady = bst::chrono::system_clock::is_steady;
4848

49+
// UTC is 37 seconds behind TAI; see comments below for details
50+
static const duration tai_offset() { return bst::chrono::seconds(37); }
51+
4952
static time_point now()
5053
{
5154
// "NMOS specifications shall use the PTP/SMPTE Epoch, i.e. 1 January 1970 00:00:00 TAI."
@@ -69,11 +72,9 @@ namespace nmos
6972
// and https://en.wikipedia.org/wiki/International_Atomic_Time
7073
// and https://github.com/HowardHinnant/date/issues/129
7174
// and https://cr.yp.to/proto/utctai.html
72-
// and https://www.iers.org/SharedDocs/News/EN/BulletinC.html
73-
// and https://www.ietf.org/timezones/data/leap-seconds.list
74-
static const duration tai_offset = bst::chrono::seconds(37);
75-
76-
return time_point(tai_offset + bst::chrono::system_clock::now().time_since_epoch());
75+
// and https://datacenter.iers.org/data/latestVersion/bulletinC.txt
76+
// and https://data.iana.org/time-zones/data/leap-seconds.list
77+
return time_point(tai_offset() + bst::chrono::system_clock::now().time_since_epoch());
7778
}
7879
};
7980

0 commit comments

Comments
 (0)