Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Time.hpp
Go to the documentation of this file.
1 // Copyright © 2020-2024 Alexandre Coderre-Chabot
2 //
3 // This file is part of Physical Quantities (PhQ), a C++ library of physical quantities, physical
4 // models, and units of measure for scientific computing.
5 //
6 // Physical Quantities is hosted at:
7 // https://github.com/acodcha/phq
8 //
9 // Physical Quantities is licensed under the MIT License:
10 // https://mit-license.org
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
13 // associated documentation files (the "Software"), to deal in the Software without restriction,
14 // including without limitation the rights to use, copy, modify, merge, publish, distribute,
15 // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 // - The above copyright notice and this permission notice shall be included in all copies or
18 // substantial portions of the Software.
19 // - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
20 // BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 #ifndef PHQ_UNIT_TIME_HPP
26 #define PHQ_UNIT_TIME_HPP
27 
28 #include <cstddef>
29 #include <cstdint>
30 #include <functional>
31 #include <map>
32 #include <ostream>
33 #include <string_view>
34 #include <unordered_map>
35 
36 #include "../Base.hpp"
37 #include "../Dimension/ElectricCurrent.hpp"
38 #include "../Dimension/Length.hpp"
39 #include "../Dimension/LuminousIntensity.hpp"
40 #include "../Dimension/Mass.hpp"
41 #include "../Dimension/SubstanceAmount.hpp"
42 #include "../Dimension/Temperature.hpp"
43 #include "../Dimension/Time.hpp"
44 #include "../Dimensions.hpp"
45 #include "../Unit.hpp"
46 #include "../UnitSystem.hpp"
47 
48 namespace PhQ {
49 
50 namespace Unit {
51 
52 /// \brief Time units.
53 enum class Time : int8_t {
54  /// \brief Nanosecond (ns) time unit.
55  Nanosecond,
56 
57  /// \brief Microsecond (μs) time unit.
59 
60  /// \brief Millisecond (ms) time unit.
62 
63  /// \brief Second (s) time unit.
64  Second,
65 
66  /// \brief Minute (min) time unit.
67  Minute,
68 
69  /// \brief Hour (hr) time unit.
70  Hour,
71 };
72 
73 } // namespace Unit
74 
75 /// \brief Standard time unit: second (s).
76 template <>
77 inline constexpr const Unit::Time Standard<Unit::Time>{Unit::Time::Second};
78 
79 /// \brief Physical dimension set of time units.
80 template <>
81 inline constexpr const Dimensions RelatedDimensions<Unit::Time>{
84  Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
85 };
86 
87 inline std::ostream& operator<<(std::ostream& stream, const Unit::Time unit) {
88  stream << Abbreviation(unit);
89  return stream;
90 }
91 
92 namespace Internal {
93 
94 template <>
95 inline const std::map<UnitSystem, Unit::Time> ConsistentUnits<Unit::Time>{
100 };
101 
102 template <>
103 inline const std::map<Unit::Time, UnitSystem> RelatedUnitSystems<Unit::Time>{};
104 
105 // clang-format off
106 
107 template <>
108 inline const std::map<Unit::Time, std::string_view> Abbreviations<Unit::Time>{
109  {Unit::Time::Nanosecond, "ns" },
110  {Unit::Time::Microsecond, "μs" },
111  {Unit::Time::Millisecond, "ms" },
112  {Unit::Time::Second, "s" },
113  {Unit::Time::Minute, "min"},
114  {Unit::Time::Hour, "hr" },
115 };
116 
117 template <>
118 inline const std::unordered_map<std::string_view, Unit::Time> Spellings<Unit::Time>{
119  {"ns", Unit::Time::Nanosecond },
120  {"nanosecond", Unit::Time::Nanosecond },
121  {"nanoseconds", Unit::Time::Nanosecond },
122  {"μs", Unit::Time::Microsecond},
123  {"us", Unit::Time::Microsecond},
124  {"microsecond", Unit::Time::Microsecond},
125  {"microseconds", Unit::Time::Microsecond},
126  {"ms", Unit::Time::Millisecond},
127  {"millisecond", Unit::Time::Millisecond},
128  {"milliseconds", Unit::Time::Millisecond},
129  {"s", Unit::Time::Second },
130  {"second", Unit::Time::Second },
131  {"seconds", Unit::Time::Second },
132  {"min", Unit::Time::Minute },
133  {"mins", Unit::Time::Minute },
134  {"minute", Unit::Time::Minute },
135  {"minutes", Unit::Time::Minute },
136  {"hr", Unit::Time::Hour },
137  {"hrs", Unit::Time::Hour },
138  {"hour", Unit::Time::Hour },
139  {"hours", Unit::Time::Hour },
140 };
141 
142 // clang-format on
143 
144 template <>
145 template <typename NumericType>
146 inline constexpr void Conversion<Unit::Time, Unit::Time::Nanosecond>::FromStandard(
147  NumericType& value) noexcept {
148  value *= static_cast<NumericType>(1.0E9L);
149 }
150 
151 template <>
152 template <typename NumericType>
153 inline constexpr void Conversion<Unit::Time, Unit::Time::Nanosecond>::ToStandard(
154  NumericType& value) noexcept {
155  value *= static_cast<NumericType>(1.0E-9L);
156 }
157 
158 template <>
159 template <typename NumericType>
160 inline constexpr void Conversion<Unit::Time, Unit::Time::Microsecond>::FromStandard(
161  NumericType& value) noexcept {
162  value *= static_cast<NumericType>(1.0E6L);
163 }
164 
165 template <>
166 template <typename NumericType>
167 inline constexpr void Conversion<Unit::Time, Unit::Time::Microsecond>::ToStandard(
168  NumericType& value) noexcept {
169  value *= static_cast<NumericType>(1.0E-6L);
170 }
171 
172 template <>
173 template <typename NumericType>
174 inline constexpr void Conversion<Unit::Time, Unit::Time::Millisecond>::FromStandard(
175  NumericType& value) noexcept {
176  value *= static_cast<NumericType>(1000.0L);
177 }
178 
179 template <>
180 template <typename NumericType>
181 inline constexpr void Conversion<Unit::Time, Unit::Time::Millisecond>::ToStandard(
182  NumericType& value) noexcept {
183  value *= static_cast<NumericType>(0.001L);
184 }
185 
186 template <>
187 template <typename NumericType>
188 inline constexpr void Conversion<Unit::Time, Unit::Time::Second>::FromStandard(
189  NumericType& /*value*/) noexcept {}
190 
191 template <>
192 template <typename NumericType>
193 inline constexpr void Conversion<Unit::Time, Unit::Time::Second>::ToStandard(
194  NumericType& /*value*/) noexcept {}
195 
196 template <>
197 template <typename NumericType>
198 inline constexpr void Conversion<Unit::Time, Unit::Time::Minute>::FromStandard(
199  NumericType& value) noexcept {
200  value /= static_cast<NumericType>(60.0L);
201 }
202 
203 template <>
204 template <typename NumericType>
205 inline constexpr void Conversion<Unit::Time, Unit::Time::Minute>::ToStandard(
206  NumericType& value) noexcept {
207  value *= static_cast<NumericType>(60.0L);
208 }
209 
210 template <>
211 template <typename NumericType>
212 inline constexpr void Conversion<Unit::Time, Unit::Time::Hour>::FromStandard(
213  NumericType& value) noexcept {
214  value /= static_cast<NumericType>(3600.0L);
215 }
216 
217 template <>
218 template <typename NumericType>
219 inline constexpr void Conversion<Unit::Time, Unit::Time::Hour>::ToStandard(
220  NumericType& value) noexcept {
221  value *= static_cast<NumericType>(3600.0L);
222 }
223 
224 template <typename NumericType>
225 inline const std::map<Unit::Time, std::function<void(NumericType* values, const std::size_t size)>>
226  MapOfConversionsFromStandard<Unit::Time, NumericType>{
228  Conversions<Unit::Time, Unit::Time::Nanosecond>::FromStandard<NumericType> },
230  Conversions<Unit::Time, Unit::Time::Microsecond>::FromStandard<NumericType>},
232  Conversions<Unit::Time, Unit::Time::Millisecond>::FromStandard<NumericType>},
234  Conversions<Unit::Time, Unit::Time::Second>::FromStandard<NumericType> },
236  Conversions<Unit::Time, Unit::Time::Minute>::FromStandard<NumericType> },
237  {Unit::Time::Hour, Conversions<Unit::Time, Unit::Time::Hour>::FromStandard<NumericType> },
238 };
239 
240 template <typename NumericType>
241 inline const std::map<Unit::Time,
242  std::function<void(NumericType* const values, const std::size_t size)>>
243  MapOfConversionsToStandard<Unit::Time, NumericType>{
245  Conversions<Unit::Time, Unit::Time::Nanosecond>::ToStandard<NumericType> },
247  Conversions<Unit::Time, Unit::Time::Microsecond>::ToStandard<NumericType>},
249  Conversions<Unit::Time, Unit::Time::Millisecond>::ToStandard<NumericType>},
250  {Unit::Time::Second, Conversions<Unit::Time, Unit::Time::Second>::ToStandard<NumericType> },
251  {Unit::Time::Minute, Conversions<Unit::Time, Unit::Time::Minute>::ToStandard<NumericType> },
252  {Unit::Time::Hour, Conversions<Unit::Time, Unit::Time::Hour>::ToStandard<NumericType> },
253 };
254 
255 } // namespace Internal
256 
257 } // namespace PhQ
258 
259 #endif // PHQ_UNIT_TIME_HPP
ElectricCurrent
Electric current units.
Mass
Mass units.
Definition: Mass.hpp:53
Length
Length units.
Definition: Length.hpp:53
SubstanceAmount
Amount of substance units.
Temperature
Temperature units. Not to be confused with temperature difference units. For example,...
Definition: Temperature.hpp:55
Time
Time units.
Definition: Time.hpp:53
@ Microsecond
Microsecond (μs) time unit.
@ Nanosecond
Nanosecond (ns) time unit.
@ Minute
Minute (min) time unit.
@ Millisecond
Millisecond (ms) time unit.
@ Hour
Hour (hr) time unit.
@ Second
Second (s) time unit.
Namespace that encompasses all of the Physical Quantities library's content.
@ FootPoundSecondRankine
Foot-pound-second-rankine (ft·lbf·s·°R) system.
@ MillimetreGramSecondKelvin
Millimetre-gram-second-kelvin (mm·g·s·K) system.
@ MetreKilogramSecondKelvin
Metre-kilogram-second-kelvin (m·kg·s·K) system.
@ InchPoundSecondRankine
Inch-pound-second-rankine (in·lbf·s·°R) system.
std::string_view Abbreviation(const Enumeration enumeration)
Returns the abbreviation of a given enumeration value. For example, PhQ::Abbreviation(PhQ::Unit::Time...
Definition: Base.hpp:89
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)