The Battle for Wesnoth  1.19.8+dev
chrono.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
18 
19 #include <algorithm>
20 #include <chrono>
21 #include <iomanip>
22 #include <sstream>
23 #include <string_view>
24 
25 #if __cpp_lib_chrono >= 201907L
26 #define CPP20_CHRONO_SUPPORT
27 #endif
28 
29 namespace chrono
30 {
31 #ifdef CPP20_CHRONO_SUPPORT
32 
33 using std::chrono::days;
34 using std::chrono::weeks;
36 using std::chrono::years;
37 
38 #else
39 
40 using days = std::chrono::duration<int, std::ratio<86400>>;
41 using weeks = std::chrono::duration<int, std::ratio<604800>>;
42 using months = std::chrono::duration<int, std::ratio<2629746>>;
43 using years = std::chrono::duration<int, std::ratio<31556952>>;
44 
45 #endif
46 
47 inline auto parse_timestamp(long long val)
48 {
49  return std::chrono::system_clock::from_time_t(val);
50 }
51 
52 inline auto parse_timestamp(const config_attribute_value& val)
53 {
54  return std::chrono::system_clock::from_time_t(val.to_long_long());
55 }
56 
57 inline auto serialize_timestamp(const std::chrono::system_clock::time_point& time)
58 {
59  return std::chrono::system_clock::to_time_t(time);
60 }
61 
62 inline auto format_local_timestamp(const std::chrono::system_clock::time_point& time, std::string_view format = "%F %T")
63 {
64  std::ostringstream ss;
65  auto as_time_t = std::chrono::system_clock::to_time_t(time);
66  ss << std::put_time(std::localtime(&as_time_t), format.data());
67  return ss.str();
68 }
69 
70 template<typename Duration>
71 inline auto parse_duration(const config_attribute_value& val, const Duration& def = Duration{0})
72 {
73  return Duration{val.to_long_long(def.count())};
74 }
75 
76 template<typename RepE, typename PeriodE, typename RepD, typename PeriodD>
77 constexpr double normalize_progress(
78  const std::chrono::duration<RepE, PeriodE>& elapsed,
79  const std::chrono::duration<RepD, PeriodD>& duration)
80 {
81  return std::clamp(std::chrono::duration<double, PeriodE>{elapsed} / duration, 0.0, 1.0);
82 }
83 
84 template<typename... Ts, typename Rep, typename Period>
85 constexpr auto deconstruct_duration(const std::tuple<Ts...>&, const std::chrono::duration<Rep, Period>& span)
86 {
87  auto time_remaining = std::chrono::duration_cast<std::common_type_t<Ts...>>(span);
88  return std::tuple{[&time_remaining]() {
89  auto duration = std::chrono::duration_cast<Ts>(time_remaining);
90  time_remaining -= duration;
91  return duration;
92  }()...};
93 }
94 
95 /** Helper types to be used with @ref deconstruct_duration */
96 namespace format
97 {
98 constexpr auto days_hours_mins_secs = std::tuple<
100  std::chrono::hours,
101  std::chrono::minutes,
102  std::chrono::seconds
103 >{};
104 
105 } // namespace format
106 
107 } // namespace chrono
Variant for storing WML attributes.
long long to_long_long(long long def=0) const
Definitions for the interface to Wesnoth Markup Language (WML).
constexpr auto days_hours_mins_secs
Definition: chrono.hpp:98
std::chrono::duration< int, std::ratio< 2629746 > > months
Definition: chrono.hpp:42
auto serialize_timestamp(const std::chrono::system_clock::time_point &time)
Definition: chrono.hpp:57
auto parse_timestamp(long long val)
Definition: chrono.hpp:47
auto parse_duration(const config_attribute_value &val, const Duration &def=Duration{0})
Definition: chrono.hpp:71
auto format_local_timestamp(const std::chrono::system_clock::time_point &time, std::string_view format="%F %T")
Definition: chrono.hpp:62
std::chrono::duration< int, std::ratio< 31556952 > > years
Definition: chrono.hpp:43
std::chrono::duration< int, std::ratio< 604800 > > weeks
Definition: chrono.hpp:41
constexpr auto deconstruct_duration(const std::tuple< Ts... > &, const std::chrono::duration< Rep, Period > &span)
Definition: chrono.hpp:85
std::chrono::duration< int, std::ratio< 86400 > > days
Definition: chrono.hpp:40
constexpr double normalize_progress(const std::chrono::duration< RepE, PeriodE > &elapsed, const std::chrono::duration< RepD, PeriodD > &duration)
Definition: chrono.hpp:77