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
31
43#ifndef AH_CHRONOS_UTILS_H
44#define AH_CHRONOS_UTILS_H
45
46# include <string>
47# include <chrono>
48
49namespace Aleph
50{
51// given a duration, return the number of seconds
52inline double duration_to_seconds(const std::chrono::duration<double> & d)
53{
54 return d.count();
55}
56
57// given a duration, return the number of milliseconds
58inline double duration_to_milliseconds(const std::chrono::duration<double> & d)
59{
60 return std::chrono::duration_cast<std::chrono::milliseconds>(d).count();
61}
62
63// given a duration, return the number of microseconds
64inline double duration_to_microseconds(const std::chrono::duration<double> & d)
65{
66 return std::chrono::duration_cast<std::chrono::microseconds>(d).count();
67}
68
69// given a duration, return the number of nanoseconds
70inline double duration_to_nanoseconds(const std::chrono::duration<double> & d)
71{
72 return std::chrono::duration_cast<std::chrono::nanoseconds>(d).count();
73}
74
75// given a duration, return the number of minutes
76inline double duration_to_minutes(const std::chrono::duration<double> & d)
77{
78 return std::chrono::duration_cast<std::chrono::minutes>(d).count();
79}
80
81// given a duration, return the number of hours
82inline double duration_to_hours(const std::chrono::duration<double> & d)
83{
84 return std::chrono::duration_cast<std::chrono::hours>(d).count();
85}
86
87// given a duration, return the number of days
88inline double duration_to_days(const std::chrono::duration<double> & d)
89{
90 return std::chrono::duration_cast<std::chrono::days>(d).count();
91}
92
93// given a duration, return a string with the proper units
94// acording to the magnitude of the duration
95inline std::string duration_to_string(const auto & d)
96{
97 using namespace std::chrono;
98 if (d < seconds(1))
99 return std::to_string(duration_to_milliseconds(d)) + " ms";
100
101 if (d < minutes(1))
102 return std::to_string(duration_to_seconds(d)) + " s";
103 if (d < hours(1))
104 return std::to_string(duration_to_minutes(d)) + " min";
105 if (d < days(1))
106 return std::to_string(duration_to_hours(d)) + " h";
107
108 return std::to_string(duration_to_days(d)) + " days";
109}
110}
111
112#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)