Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-chronos-utils.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
42#ifndef AH_CHRONOS_UTILS_H
43#define AH_CHRONOS_UTILS_H
44
45#include <string>
46#include <chrono>
47#include <format>
48
49namespace Aleph {
50// given a duration, return the number of seconds
51inline double duration_to_seconds(const std::chrono::duration<double> &d)
52{
53 return d.count();
54}
55
56// given a duration, return the number of milliseconds
57inline double duration_to_milliseconds(const std::chrono::duration<double> &d)
58{
59 return std::chrono::duration_cast<std::chrono::milliseconds>(d).count();
60}
61
62// given a duration, return the number of microseconds
63inline double duration_to_microseconds(const std::chrono::duration<double> &d)
64{
65 return std::chrono::duration_cast<std::chrono::microseconds>(d).count();
66}
67
68// given a duration, return the number of nanoseconds
69inline double duration_to_nanoseconds(const std::chrono::duration<double> &d)
70{
71 return std::chrono::duration_cast<std::chrono::nanoseconds>(d).count();
72}
73
74// given a duration, return the number of minutes
75inline double duration_to_minutes(const std::chrono::duration<double> &d)
76{
77 return std::chrono::duration_cast<std::chrono::minutes>(d).count();
78}
79
80// given a duration, return the number of hours
81inline double duration_to_hours(const std::chrono::duration<double> &d)
82{
83 return std::chrono::duration_cast<std::chrono::hours>(d).count();
84}
85
86// given a duration, return the number of days
87inline double duration_to_days(const std::chrono::duration<double> &d)
88{
89 return std::chrono::duration_cast<std::chrono::days>(d).count();
90}
91
92// given a duration, return a string with the proper units
93// according to the magnitude of the duration
94inline std::string duration_to_string(const auto &d)
95{
96 if (d < std::chrono::seconds(1))
97 return std::format("{} ms", duration_to_milliseconds(d));
98
99 if (d < std::chrono::minutes(1))
100 return std::format("{} s", duration_to_seconds(d));
101 if (d < std::chrono::hours(1))
102 return std::format("{} min", duration_to_minutes(d));
103 if (d < std::chrono::days(1))
104 return std::format("{} h", duration_to_hours(d));
105
106 return std::format("{} days", duration_to_days(d));
107}
108} // namespace Aleph
109
110#endif // AH_CHRONOS_UTILS_H
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
double duration_to_seconds(const std::chrono::duration< double > &d)
double duration_to_milliseconds(const std::chrono::duration< double > &d)
std::string duration_to_string(const auto &d)
double duration_to_nanoseconds(const std::chrono::duration< double > &d)
double duration_to_microseconds(const std::chrono::duration< double > &d)
double duration_to_minutes(const std::chrono::duration< double > &d)
double duration_to_days(const std::chrono::duration< double > &d)
double duration_to_hours(const std::chrono::duration< double > &d)