The Battle for Wesnoth  1.19.5+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 <chrono>
20 #include <iomanip>
21 #include <sstream>
22 #include <string_view>
23 
24 #if __cpp_lib_chrono >= 201907L
25 #define CPP20_CHRONO_SUPPORT
26 #endif
27 
28 namespace chrono
29 {
30 #ifdef CPP20_CHRONO_SUPPORT
31 
32 using std::chrono::days;
33 using std::chrono::weeks;
35 using std::chrono::years;
36 
37 #else
38 
39 using days = std::chrono::duration<int, std::ratio<86400>>;
40 using weeks = std::chrono::duration<int, std::ratio<604800>>;
41 using months = std::chrono::duration<int, std::ratio<2629746>>;
42 using years = std::chrono::duration<int, std::ratio<31556952>>;
43 
44 #endif
45 
46 inline auto parse_timestamp(long long val)
47 {
48  return std::chrono::system_clock::from_time_t(val);
49 }
50 
51 inline auto parse_timestamp(const config_attribute_value& val)
52 {
53  return std::chrono::system_clock::from_time_t(val.to_long_long());
54 }
55 
56 inline auto serialize_timestamp(const std::chrono::system_clock::time_point& time)
57 {
58  return std::chrono::system_clock::to_time_t(time);
59 }
60 
61 inline auto format_local_timestamp(const std::chrono::system_clock::time_point& time, std::string_view format = "%F %T")
62 {
63  std::ostringstream ss;
64  auto as_time_t = std::chrono::system_clock::to_time_t(time);
65  ss << std::put_time(std::localtime(&as_time_t), format.data());
66  return ss.str();
67 }
68 
69 template<typename Duration>
70 inline auto parse_duration(const config_attribute_value& val, const Duration& def = Duration{0})
71 {
72  return Duration{val.to_long_long(def.count())};
73 }
74 
75 template<typename Rep, typename Period>
76 constexpr auto deconstruct_duration(const std::chrono::duration<Rep, Period>& span)
77 {
78  auto days = std::chrono::duration_cast<chrono::days>(span);
79  auto hours = std::chrono::duration_cast<std::chrono::hours>(span - days);
80  auto minutes = std::chrono::duration_cast<std::chrono::minutes>(span - days - hours);
81  auto seconds = std::chrono::duration_cast<std::chrono::seconds>(span - days - hours - minutes);
82 
83  return std::tuple{ days, hours, minutes, seconds };
84 }
85 
86 } // 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).
std::chrono::duration< int, std::ratio< 2629746 > > months
Definition: chrono.hpp:41
auto serialize_timestamp(const std::chrono::system_clock::time_point &time)
Definition: chrono.hpp:56
auto parse_timestamp(long long val)
Definition: chrono.hpp:46
auto parse_duration(const config_attribute_value &val, const Duration &def=Duration{0})
Definition: chrono.hpp:70
auto format_local_timestamp(const std::chrono::system_clock::time_point &time, std::string_view format="%F %T")
Definition: chrono.hpp:61
std::chrono::duration< int, std::ratio< 31556952 > > years
Definition: chrono.hpp:42
std::chrono::duration< int, std::ratio< 604800 > > weeks
Definition: chrono.hpp:40
std::chrono::duration< int, std::ratio< 86400 > > days
Definition: chrono.hpp:39
constexpr auto deconstruct_duration(const std::chrono::duration< Rep, Period > &span)
Definition: chrono.hpp:76