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 Second (s) time unit.
55  Second,
56 
57  /// \brief Nanosecond (ns) time unit.
58  Nanosecond,
59 
60  /// \brief Microsecond (μs) time unit.
62 
63  /// \brief Millisecond (ms) time unit.
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::Second, "s" },
110  {Unit::Time::Nanosecond, "ns" },
111  {Unit::Time::Microsecond, "μs" },
112  {Unit::Time::Millisecond, "ms" },
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  {"s", Unit::Time::Second },
120  {"second", Unit::Time::Second },
121  {"seconds", Unit::Time::Second },
122  {"ns", Unit::Time::Nanosecond },
123  {"nanosecond", Unit::Time::Nanosecond },
124  {"nanoseconds", Unit::Time::Nanosecond },
125  {"μs", Unit::Time::Microsecond},
126  {"us", Unit::Time::Microsecond},
127  {"microsecond", Unit::Time::Microsecond},
128  {"microseconds", Unit::Time::Microsecond},
129  {"ms", Unit::Time::Millisecond},
130  {"millisecond", Unit::Time::Millisecond},
131  {"milliseconds", Unit::Time::Millisecond},
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::Second>::FromStandard(
147  NumericType& /*value*/) noexcept {}
148 
149 template <>
150 template <typename NumericType>
151 inline constexpr void Conversion<Unit::Time, Unit::Time::Second>::ToStandard(
152  NumericType& /*value*/) noexcept {}
153 
154 template <>
155 template <typename NumericType>
156 inline constexpr void Conversion<Unit::Time, Unit::Time::Nanosecond>::FromStandard(
157  NumericType& value) noexcept {
158  value *= static_cast<NumericType>(1.0E9L);
159 }
160 
161 template <>
162 template <typename NumericType>
163 inline constexpr void Conversion<Unit::Time, Unit::Time::Nanosecond>::ToStandard(
164  NumericType& value) noexcept {
165  value *= static_cast<NumericType>(1.0E-9L);
166 }
167 
168 template <>
169 template <typename NumericType>
170 inline constexpr void Conversion<Unit::Time, Unit::Time::Microsecond>::FromStandard(
171  NumericType& value) noexcept {
172  value *= static_cast<NumericType>(1.0E6L);
173 }
174 
175 template <>
176 template <typename NumericType>
177 inline constexpr void Conversion<Unit::Time, Unit::Time::Microsecond>::ToStandard(
178  NumericType& value) noexcept {
179  value *= static_cast<NumericType>(1.0E-6L);
180 }
181 
182 template <>
183 template <typename NumericType>
184 inline constexpr void Conversion<Unit::Time, Unit::Time::Millisecond>::FromStandard(
185  NumericType& value) noexcept {
186  value *= static_cast<NumericType>(1000.0L);
187 }
188 
189 template <>
190 template <typename NumericType>
191 inline constexpr void Conversion<Unit::Time, Unit::Time::Millisecond>::ToStandard(
192  NumericType& value) noexcept {
193  value *= static_cast<NumericType>(0.001L);
194 }
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::Second>::FromStandard<NumericType> },
230  Conversions<Unit::Time, Unit::Time::Nanosecond>::FromStandard<NumericType> },
232  Conversions<Unit::Time, Unit::Time::Microsecond>::FromStandard<NumericType>},
234  Conversions<Unit::Time, Unit::Time::Millisecond>::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>{
244  {Unit::Time::Second, Conversions<Unit::Time, Unit::Time::Second>::ToStandard<NumericType> },
246  Conversions<Unit::Time, Unit::Time::Nanosecond>::ToStandard<NumericType> },
248  Conversions<Unit::Time, Unit::Time::Microsecond>::ToStandard<NumericType>},
250  Conversions<Unit::Time, Unit::Time::Millisecond>::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)