MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
chrono.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mbgl/platform/time.hpp>
4 
5 #include <chrono>
6 #include <limits>
7 #include <string>
8 #include <type_traits>
9 
10 namespace mbgl {
11 
12 using Clock = std::chrono::steady_clock;
13 
14 using Seconds = std::chrono::seconds;
15 using Milliseconds = std::chrono::milliseconds;
16 
17 using TimePoint = Clock::time_point;
18 using Duration = Clock::duration;
19 
20 // Used to measure second-precision times, such as times gathered from HTTP responses.
21 using Timestamp = std::chrono::time_point<std::chrono::system_clock, Seconds>;
22 
23 namespace util {
24 
25 inline Timestamp now() {
26  return platform::now();
27 }
28 
29 // Returns the RFC1123 formatted date. E.g. "Tue, 04 Nov 2014 02:13:24 GMT"
31 
32 // YYYY-mm-dd HH:MM:SS e.g. "2015-11-26 16:11:23"
34 
35 Timestamp parseTimestamp(const char *);
36 
37 Timestamp parseTimestamp(int32_t timestamp);
38 
39 // C++17 polyfill
40 #if defined(_MSC_VER) && !defined(__clang__)
41 template <class _Rep, class _Period, std::enable_if_t<std::numeric_limits<_Rep>::is_signed, int> = 0>
42 _NODISCARD constexpr std::chrono::duration<_Rep, _Period> abs(const std::chrono::duration<_Rep, _Period> _Dur) noexcept(
43  std::is_arithmetic_v<_Rep>) /* strengthened */ {
44  // create a duration with count() the absolute value of _Dur.count()
45  return _Dur < std::chrono::duration<_Rep, _Period>::zero() ? std::chrono::duration<_Rep, _Period>::zero() - _Dur : _Dur;
46 }
47 #else
48 template <class Rep, class Period, class = std::enable_if_t<
49  std::chrono::duration<Rep, Period>::min() < std::chrono::duration<Rep, Period>::zero()>>
50 constexpr std::chrono::duration<Rep, Period> abs(std::chrono::duration<Rep, Period> d)
51 {
52  return d >= d.zero() ? d : -d;
53 }
54 #endif
55 
56 } // namespace util
57 
58 } // namespace mbgl
std::chrono::time_point< std::chrono::system_clock, std::chrono::seconds > now()
std::unique_ptr< Expression > string(std::unique_ptr< Expression >, std::unique_ptr< Expression > def=nullptr)
std::string rfc1123(Timestamp)
Timestamp now()
Definition: chrono.hpp:25
std::enable_if_t< std::is_integral_v< T >, T > min(T a, T b)
Definition: minmax.hpp:26
Timestamp parseTimestamp(const char *)
std::string iso8601(Timestamp)
Definition: actor.hpp:15
Clock::time_point TimePoint
Definition: chrono.hpp:17
std::chrono::steady_clock Clock
Definition: chrono.hpp:12
std::chrono::milliseconds Milliseconds
Definition: chrono.hpp:15
std::chrono::seconds Seconds
Definition: chrono.hpp:14
std::chrono::time_point< std::chrono::system_clock, Seconds > Timestamp
Definition: chrono.hpp:21
Clock::duration Duration
Definition: chrono.hpp:18