Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Loading...
Searching...
No Matches
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
48namespace PhQ {
49
50namespace Unit {
51
52/// \brief Time units.
53enum class Time : int8_t {
54 /// \brief Second (s) time unit.
55 Second,
56
57 /// \brief Nanosecond (ns) time unit.
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).
76template <>
77inline constexpr const Unit::Time Standard<Unit::Time>{Unit::Time::Second};
78
79/// \brief Physical dimension set of time units.
80template <>
81inline constexpr const Dimensions RelatedDimensions<Unit::Time>{
82 Dimensions{Dimension::Time{1}, Dimension::Length{0}, Dimension::Mass{0},
83 Dimension::ElectricCurrent{0}, Dimension::Temperature{0},
84 Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
85};
86
87inline std::ostream& operator<<(std::ostream& stream, const Unit::Time unit) {
88 stream << Abbreviation(unit);
89 return stream;
90}
91
92namespace Internal {
93
94template <>
95inline const std::map<UnitSystem, Unit::Time> ConsistentUnits<Unit::Time>{
100};
101
102template <>
103inline const std::map<Unit::Time, UnitSystem> RelatedUnitSystems<Unit::Time>{};
104
105// clang-format off
106
107template <>
108inline 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" },
113 {Unit::Time::Minute, "min"},
114 {Unit::Time::Hour, "hr" },
115};
116
117template <>
118inline 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 },
127 {"microsecond", Unit::Time::Microsecond},
128 {"microseconds", Unit::Time::Microsecond},
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
144template <>
145template <typename NumericType>
146inline constexpr void Conversion<Unit::Time, Unit::Time::Second>::FromStandard(
147 NumericType& /*value*/) noexcept {}
148
149template <>
150template <typename NumericType>
151inline constexpr void Conversion<Unit::Time, Unit::Time::Second>::ToStandard(
152 NumericType& /*value*/) noexcept {}
153
154template <>
155template <typename NumericType>
156inline constexpr void Conversion<Unit::Time, Unit::Time::Nanosecond>::FromStandard(
157 NumericType& value) noexcept {
158 value *= static_cast<NumericType>(1.0E9L);
159}
160
161template <>
162template <typename NumericType>
163inline constexpr void Conversion<Unit::Time, Unit::Time::Nanosecond>::ToStandard(
164 NumericType& value) noexcept {
165 value *= static_cast<NumericType>(1.0E-9L);
166}
167
168template <>
169template <typename NumericType>
170inline constexpr void Conversion<Unit::Time, Unit::Time::Microsecond>::FromStandard(
171 NumericType& value) noexcept {
172 value *= static_cast<NumericType>(1.0E6L);
173}
174
175template <>
176template <typename NumericType>
177inline constexpr void Conversion<Unit::Time, Unit::Time::Microsecond>::ToStandard(
178 NumericType& value) noexcept {
179 value *= static_cast<NumericType>(1.0E-6L);
180}
181
182template <>
183template <typename NumericType>
184inline constexpr void Conversion<Unit::Time, Unit::Time::Millisecond>::FromStandard(
185 NumericType& value) noexcept {
186 value *= static_cast<NumericType>(1000.0L);
187}
188
189template <>
190template <typename NumericType>
191inline constexpr void Conversion<Unit::Time, Unit::Time::Millisecond>::ToStandard(
192 NumericType& value) noexcept {
193 value *= static_cast<NumericType>(0.001L);
194}
195
196template <>
197template <typename NumericType>
198inline constexpr void Conversion<Unit::Time, Unit::Time::Minute>::FromStandard(
199 NumericType& value) noexcept {
200 value /= static_cast<NumericType>(60.0L);
201}
202
203template <>
204template <typename NumericType>
205inline constexpr void Conversion<Unit::Time, Unit::Time::Minute>::ToStandard(
206 NumericType& value) noexcept {
207 value *= static_cast<NumericType>(60.0L);
208}
209
210template <>
211template <typename NumericType>
212inline constexpr void Conversion<Unit::Time, Unit::Time::Hour>::FromStandard(
213 NumericType& value) noexcept {
214 value /= static_cast<NumericType>(3600.0L);
215}
216
217template <>
218template <typename NumericType>
219inline constexpr void Conversion<Unit::Time, Unit::Time::Hour>::ToStandard(
220 NumericType& value) noexcept {
221 value *= static_cast<NumericType>(3600.0L);
222}
223
224template <typename NumericType>
225inline const std::map<Unit::Time, std::function<void(NumericType* values, const std::size_t size)>>
226 MapOfConversionsFromStandard<Unit::Time, NumericType>{
227 {Unit::Time::Second, Conversions<Unit::Time, Unit::Time::Second>::FromStandard<NumericType> },
229 Conversions<Unit::Time, Unit::Time::Nanosecond>::FromStandard<NumericType> },
231 Conversions<Unit::Time, Unit::Time::Microsecond>::FromStandard<NumericType>},
233 Conversions<Unit::Time, Unit::Time::Millisecond>::FromStandard<NumericType>},
234 {Unit::Time::Minute, Conversions<Unit::Time, Unit::Time::Minute>::FromStandard<NumericType> },
235 {Unit::Time::Hour, Conversions<Unit::Time, Unit::Time::Hour>::FromStandard<NumericType> },
236};
237
238template <typename NumericType>
239inline const std::
240 map<Unit::Time, std::function<void(NumericType* const values, const std::size_t size)>>
241 MapOfConversionsToStandard<Unit::Time, NumericType>{
243 Conversions<Unit::Time, Unit::Time::Second>::ToStandard<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>},
251 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
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.
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)
@ 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