Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Foundation/Time/Common.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Time_Common_h_
5#define _Stroika_Foundation_Time_Common_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <chrono>
10#include <ctime>
11
12/**
13 * \file
14 *
15 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
16 */
17
18namespace Stroika::Foundation::Time {
19
20 /**
21 * \note Design Note
22 * About time_t
23 *
24 * This is generally a 64-bit (but sometimes, like for 32-bit AIX, its 32-bit) signed (but not guaranteed signed) quantity. It generally
25 * represents a number of seconds since midnight Jan 1, 1970 in UTC.
26 *
27 * From http://en.cppreference.com/w/cpp/chrono/c/time_t
28 * Arithmetic type capable of representing times.
29 *
30 * Although not defined, this is almost always an integral value holding the number of seconds
31 * (not counting leap seconds) since 00:00, Jan 1 1970 UTC, corresponding to POSIX time.
32 *
33 * Because of this, basically ALL the Stroika APIs (unless an obvious or stated exception) treat time_t as
34 * in UTC.
35 *
36 * The Stroika APIs do NOT assume 64-bit, nor signedness, but when we require signedness, we use make_signed_t<time_t>
37 */
38
39 /**
40 * Several APIs internally (or explicitly) use duration_cast, and frequently those APIs do duration_cast to
41 * chrono::seconds.
42 *
43 * https://en.cppreference.com/w/cpp/chrono/duration/duration_cast
44 * Casting from a floating-point duration to an integer duration is subject to undefined behavior when the
45 * floating-point value is NaN, infinity, or too large to be representable by the target's integer type.
46 * Otherwise, casting to an integer duration is subject to truncation as with any static_cast to an integer type.
47 *
48 * In particular, Stroika makes use of INF values (Time::kInfinity) for durations because it so easily works with
49 * TimePoints and adding etc.
50 *
51 * But - we must be careful when calling std c++ APIs (like timed_mutex::wait_until()) to provide conforming timepoint values
52 * (even though https://en.cppreference.com/w/cpp/thread/condition_variable/wait_until says nothing about passing in inf floating point values).
53 *
54 * With Ubuntu 22.04, ubsan on clang++15 and libc++ seem to require this safety check.
55 */
56 template <typename CLOCK_T, typename DURATION_T = typename CLOCK_T::duration>
57 auto Pin2SafeSeconds (const chrono::time_point<CLOCK_T, DURATION_T>& tp) -> chrono::time_point<CLOCK_T, DURATION_T>;
58
59}
60
61/*
62 ********************************************************************************
63 ***************************** Implementation Details ***************************
64 ********************************************************************************
65 */
66#include "Common.inl"
67
68#endif /*_Stroika_Foundation_Time_Common_h_*/
auto Pin2SafeSeconds(const chrono::time_point< CLOCK_T, DURATION_T > &tp) -> chrono::time_point< CLOCK_T, DURATION_T >