ARTS 2.5.11 (git: 6827797f)
artstime.h
Go to the documentation of this file.
1
9#ifndef ARTSTIME_H
10#define ARTSTIME_H
11
12#include <chrono>
13#include <cmath>
14#include <string_view>
15
16#include "array.h"
17#include "debug.h"
18#include "matpack_data.h"
19#include "mystring.h"
20
22using TimeStep = std::chrono::duration<Numeric>;
23
25struct Time {
26 std::chrono::system_clock::time_point time;
27 using InternalTimeStep = decltype(time)::duration;
28
29 // Version will be updated when C++20 datetime is available... Version 1 will still assume local time at that time
30 [[nodiscard]] Index Version() const noexcept { return 1; }
31
32 // Construction
33 Time() : time(std::chrono::system_clock::now()) {}
34 explicit Time(std::time_t t)
35 : time(std::chrono::system_clock::from_time_t(t)) {}
36 explicit Time(std::tm t) : Time(std::mktime(&t)) {}
37 explicit Time(const String& t);
38
39 // Conversions
40 [[nodiscard]] std::time_t toTimeT() const {
41 return std::chrono::system_clock::to_time_t(time);
42 }
43 [[nodiscard]] std::tm toStruct() const {
44 std::time_t x = toTimeT();
45 std::tm y;
46 tm* z = localtime_r(&x, &y);
47 ARTS_USER_ERROR_IF(not z, "Cannot construct time struct")
48 return y;
49 }
50 [[nodiscard]] std::tm toGMTStruct() const {
51 std::time_t x = toTimeT();
52 std::tm y;
53 tm* z = gmtime_r(&x, &y);
54 ARTS_USER_ERROR_IF(not z, "Cannot construct time struct")
55 return y;
56 }
57 [[nodiscard]] TimeStep seconds_into_day() const {
58 std::tm x = toStruct();
59 return TimeStep(x.tm_hour * 3600 + x.tm_min * 60 + x.tm_sec +
60 PartOfSecond());
61 }
62 [[nodiscard]] InternalTimeStep EpochTime() const {
63 return time.time_since_epoch();
64 }
65
66 // Operations
67 InternalTimeStep operator-(const Time& t) const noexcept {
68 return time - t.time;
69 }
70 bool operator<(const Time& t) const noexcept { return time < t.time; }
71 bool operator==(const Time& t) const noexcept { return time == t.time; }
72 bool operator!=(const Time& t) const noexcept {
73 return not this->operator==(t);
74 }
75 bool operator<=(const Time& t) const noexcept {
76 return this->operator<(t) or this->operator==(t);
77 }
78 bool operator>(const Time& t) const noexcept {
79 return not this->operator<=(t);
80 }
81 bool operator>=(const Time& t) const noexcept {
82 return this->operator>(t) or this->operator==(t);
83 }
84 template <typename T, typename R>
85 Time& operator+=(const std::chrono::duration<T, R>& dt) {
86 time += std::chrono::duration_cast<InternalTimeStep>(dt);
87 return *this;
88 }
89 template <typename T, typename R>
90 Time& operator-=(const std::chrono::duration<T, R>& dt) {
91 time -= std::chrono::duration_cast<InternalTimeStep>(dt);
92 return *this;
93 }
94 template <typename T, typename R>
95 Time operator+(const std::chrono::duration<T, R>& dt) const {
96 return (Time(*this) += dt);
97 }
98 template <typename T, typename R>
99 Time operator-(const std::chrono::duration<T, R>& dt) const {
100 return (Time(*this) -= dt);
101 }
102
103 // helpers
104 [[nodiscard]] Numeric Seconds() const {
105 return std::chrono::duration_cast<TimeStep>(time.time_since_epoch())
106 .count();
107 }
108 void Seconds(Numeric x) { operator+=(TimeStep(x - Seconds())); }
109 [[nodiscard]] Numeric PartOfSecond() const {
110 return std::fmod(Seconds(), 1.0);
111 }
112
113 // Conversion
114 explicit operator Numeric() const { return Seconds(); }
115
116 friend std::ostream& operator<<(std::ostream& os, const Time& t);
117
118 friend std::istream& operator>>(std::istream& is, Time& t);
119}; // Time
120
123
126
129
131std::ostream& operator<<(std::ostream& os, const TimeStep& dt);
132
145TimeStep time_stepper_selection(const String& time_step);
146
153Time next_even(const Time& t, const TimeStep& dt);
154
167ArrayOfIndex time_steps(const ArrayOfTime& times, const TimeStep& dt);
168
175Time mean_time(const ArrayOfTime& ts, Index s = 0, Index e = -1);
176
182Vector time_vector(const ArrayOfTime& times);
183
189ArrayOfTime time_vector(const Vector& times);
190
199
206
208struct DebugTime {
210 std::string_view msg;
211 DebugTime(const std::string_view s="Time") : msg(s) {}
213 #pragma omp critical
214 std::cerr << msg << ':' << ' ' << Time{} - start << '\n';
215 }
216};
217
218#endif // ARTSTIME_H
This file contains the definition of Array.
TimeStep median(ArrayOfTimeStep)
Returns the median time step.
Definition: artstime.cc:161
ArrayOfIndex time_steps(const ArrayOfTime &times, const TimeStep &dt)
Finds the index matching demands in a list of times.
Definition: artstime.cc:59
std::chrono::duration< Numeric > TimeStep
A duration of time, 1 full tick should be 1 second.
Definition: artstime.h:22
TimeStep time_stepper_selection(const String &time_step)
Returns a time step from valid string.
Definition: artstime.cc:19
Vector time_vector(const ArrayOfTime &times)
Converts from each Time to seconds and returns as Vector.
Definition: artstime.cc:149
Time mean_time(const ArrayOfTime &ts, Index s=0, Index e=-1)
Computes the average time in a list.
Definition: artstime.cc:133
TimeStep mean(const ArrayOfTimeStep &)
Returns the mean time step.
Definition: artstime.cc:172
std::ostream & operator<<(std::ostream &os, const TimeStep &dt)
Debug output for duration.
Definition: artstime.cc:181
Time next_even(const Time &t, const TimeStep &dt)
Returns the next time after t with an even time-step.
Definition: artstime.cc:50
This can be used to make arrays out of anything.
Definition: array.h:31
Helper macros for debugging.
#define ARTS_USER_ERROR_IF(condition,...)
Definition: debug.h:135
This file contains the definition of String, the ARTS string class.
Definition: mystring.h:246
Used to debug execution time, prints msg+time on destruction to std::cerr.
Definition: artstime.h:208
DebugTime(const std::string_view s="Time")
Definition: artstime.h:211
std::string_view msg
Definition: artstime.h:210
~DebugTime()
Definition: artstime.h:212
Time start
Definition: artstime.h:209
Class to handle time in ARTS.
Definition: artstime.h:25
std::time_t toTimeT() const
Definition: artstime.h:40
Numeric Seconds() const
Definition: artstime.h:104
InternalTimeStep EpochTime() const
Definition: artstime.h:62
Numeric PartOfSecond() const
Definition: artstime.h:109
Time operator-(const std::chrono::duration< T, R > &dt) const
Definition: artstime.h:99
Time()
Definition: artstime.h:33
bool operator<(const Time &t) const noexcept
Definition: artstime.h:70
decltype(time)::duration InternalTimeStep
Definition: artstime.h:27
std::chrono::system_clock::time_point time
Definition: artstime.h:26
bool operator==(const Time &t) const noexcept
Definition: artstime.h:71
TimeStep seconds_into_day() const
Definition: artstime.h:57
Time & operator+=(const std::chrono::duration< T, R > &dt)
Definition: artstime.h:85
InternalTimeStep operator-(const Time &t) const noexcept
Definition: artstime.h:67
bool operator!=(const Time &t) const noexcept
Definition: artstime.h:72
Time(std::tm t)
Definition: artstime.h:36
bool operator>=(const Time &t) const noexcept
Definition: artstime.h:81
Index Version() const noexcept
Definition: artstime.h:30
std::tm toStruct() const
Definition: artstime.h:43
bool operator<=(const Time &t) const noexcept
Definition: artstime.h:75
friend std::istream & operator>>(std::istream &is, Time &t)
Definition: artstime.cc:104
friend std::ostream & operator<<(std::ostream &os, const Time &t)
Definition: artstime.cc:86
void Seconds(Numeric x)
Definition: artstime.h:108
Time & operator-=(const std::chrono::duration< T, R > &dt)
Definition: artstime.h:90
Time(std::time_t t)
Definition: artstime.h:34
Time operator+(const std::chrono::duration< T, R > &dt) const
Definition: artstime.h:95
bool operator>(const Time &t) const noexcept
Definition: artstime.h:78
std::tm toGMTStruct() const
Definition: artstime.h:50