The Battle for Wesnoth  1.19.24+dev
chrono.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2024 - 2025
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 get_local_timestamp(const std::chrono::system_clock::time_point& time)
63 {
64  auto as_time_t = std::chrono::system_clock::to_time_t(time);
65  return mktime(std::localtime(&as_time_t));
66 }
67 
68 // CAUTION: This does NOT return a language-localized string. To achieve that,
69 // use translation::translate_timestamp with chrono::get_local_timestamp.
70 inline auto format_local_timestamp(const std::chrono::system_clock::time_point& time, std::string_view format = "%F %T")
71 {
72  std::ostringstream ss;
73  auto as_time_t = std::chrono::system_clock::to_time_t(time);
74  ss << std::put_time(std::localtime(&as_time_t), format.data());
75  return ss.str();
76 }
77 
78 template<typename Duration>
79 inline auto parse_duration(const config_attribute_value& val, const Duration& def = Duration{0})
80 {
81  return Duration{val.to_long_long(def.count())};
82 }
83 
84 template<typename RepE, typename PeriodE, typename RepD, typename PeriodD>
85 constexpr double normalize_progress(
86  const std::chrono::duration<RepE, PeriodE>& elapsed,
87  const std::chrono::duration<RepD, PeriodD>& duration)
88 {
89  return std::clamp(std::chrono::duration<double, PeriodE>{elapsed} / duration, 0.0, 1.0);
90 }
91 
92 template<typename... Ts, typename Rep, typename Period>
93 constexpr auto deconstruct_duration(const std::tuple<Ts...>&, const std::chrono::duration<Rep, Period>& span)
94 {
95  auto time_remaining = std::chrono::duration_cast<std::common_type_t<Ts...>>(span);
96  return std::tuple{[&time_remaining]() {
97  auto duration = std::chrono::duration_cast<Ts>(time_remaining);
98  time_remaining -= duration;
99  return duration;
100  }()...};
101 }
102 
103 /** Helper types to be used with @ref deconstruct_duration */
104 namespace format
105 {
106 constexpr auto days_hours_mins_secs = std::tuple<
107  chrono::days,
108  std::chrono::hours,
109  std::chrono::minutes,
110  std::chrono::seconds
111 >{};
112 
113 } // namespace format
114 
115 } // 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:106
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:79
auto format_local_timestamp(const std::chrono::system_clock::time_point &time, std::string_view format="%F %T")
Definition: chrono.hpp:70
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:93
auto get_local_timestamp(const std::chrono::system_clock::time_point &time)
Definition: chrono.hpp:62
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:85