Skyward boardcore
Loading...
Searching...
No Matches
Time.h
Go to the documentation of this file.
1/* Copyright (c) 2023 Skyward Experimental Rocketry
2 * Author: Davide Basso
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <chrono>
26#include <ratio>
27
28#include "Units.h"
29
30namespace Boardcore
31{
32namespace Units
33{
34namespace Time
35{
36
37template <class Ratio = std::ratio<1>>
38using Time = Unit<UnitKind::Time, Ratio>;
39
40template <class ToTime, class FromTime>
41ToTime time_cast(FromTime const& from)
42{
43 return ToTime(from);
44}
45
46template <class Ratio>
47std::chrono::duration<float> to_chrono(Time<Ratio> const& from)
48{
49 return std::chrono::duration<float>(from.value());
50}
51
52using Nanosecond = Time<std::ratio<1, 1000000000>>; // Time in nanoseconds
53using Microsecond = Time<std::ratio<1, 1000000>>; // Time in microseconds
54using Millisecond = Time<std::ratio<1, 1000>>; // Time in milliseconds
55using Second = Time<>; // Time in seconds
56using Minute = Time<std::ratio<60>>; // Time in minutes
57using Hour = Time<std::ratio<3600>>; // Time in hours
58
59// Floats
60constexpr auto operator""_ns(long double n)
61{
62 return Nanosecond(static_cast<float>(n));
63};
64constexpr auto operator""_us(long double n)
65{
66 return Microsecond(static_cast<float>(n));
67};
68constexpr auto operator""_ms(long double n)
69{
70 return Millisecond(static_cast<float>(n));
71};
72constexpr auto operator""_s(long double n)
73{
74 return Second(static_cast<float>(n));
75};
76constexpr auto operator""_min(long double n)
77{
78 return Minute(static_cast<float>(n));
79};
80constexpr auto operator""_h(long double n)
81{
82 return Hour(static_cast<float>(n));
83};
84// Integers
85constexpr auto operator""_ns(unsigned long long n)
86{
87 return Nanosecond(static_cast<float>(n));
88};
89constexpr auto operator""_us(unsigned long long n)
90{
91 return Microsecond(static_cast<float>(n));
92};
93constexpr auto operator""_ms(unsigned long long n)
94{
95 return Millisecond(static_cast<float>(n));
96};
97constexpr auto operator""_s(unsigned long long n)
98{
99 return Second(static_cast<float>(n));
100};
101constexpr auto operator""_min(unsigned long long n)
102{
103 return Minute(static_cast<float>(n));
104};
105constexpr auto operator""_h(unsigned long long n)
106{
107 return Hour(static_cast<float>(n));
108};
109
110} // namespace Time
111} // namespace Units
112} // namespace Boardcore
Time< std::ratio< 1, 1000000 > > Microsecond
Definition Time.h:53
ToTime time_cast(FromTime const &from)
Definition Time.h:41
Time< std::ratio< 1, 1000000000 > > Nanosecond
Definition Time.h:52
Unit< UnitKind::Time, Ratio > Time
Definition Time.h:38
Time< std::ratio< 1, 1000 > > Millisecond
Definition Time.h:54
std::chrono::duration< float > to_chrono(Time< Ratio > const &from)
Definition Time.h:47
Time< std::ratio< 3600 > > Hour
Definition Time.h:57
Time< std::ratio< 60 > > Minute
Definition Time.h:56
This file includes all the types the logdecoder script will decode.