ARTS 2.5.4 (git: bcd8c674)
artstime.h
Go to the documentation of this file.
1/* Copyright (C) 2020
2 * Richard Larsson <ric.larsson@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2, or (at your option) any
7 * later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA. */
18
27#ifndef ARTSTIME_H
28#define ARTSTIME_H
29
30#include <chrono>
31#include <cmath>
32#include <string_view>
33
34#include "array.h"
35#include "debug.h"
36#include "matpackI.h"
37#include "mystring.h"
38
40using TimeStep = std::chrono::duration<Numeric>;
41
43struct Time {
44 std::chrono::system_clock::time_point time;
45 using InternalTimeStep = decltype(time)::duration;
46
47 // Version will be updated when C++20 datetime is available... Version 1 will still assume local time at that time
48 [[nodiscard]] Index Version() const noexcept { return 1; }
49
50 // Construction
51 Time() : time(std::chrono::system_clock::now()) {}
52 explicit Time(std::time_t t)
53 : time(std::chrono::system_clock::from_time_t(t)) {}
54 explicit Time(std::tm t) : Time(std::mktime(&t)) {}
55 explicit Time(const String& t);
56
57 // Conversions
58 [[nodiscard]] std::time_t toTimeT() const {
60 }
61 [[nodiscard]] std::tm toStruct() const {
62 std::time_t x = toTimeT();
63 std::tm y;
64 tm* z = localtime_r(&x, &y);
65 ARTS_USER_ERROR_IF(not z, "Cannot construct time struct")
66 return y;
67 }
68 [[nodiscard]] std::tm toGMTStruct() const {
69 std::time_t x = toTimeT();
70 std::tm y;
71 tm* z = gmtime_r(&x, &y);
72 ARTS_USER_ERROR_IF(not z, "Cannot construct time struct")
73 return y;
74 }
75 [[nodiscard]] TimeStep seconds_into_day() const {
76 std::tm x = toStruct();
77 return TimeStep(x.tm_hour * 3600 + x.tm_min * 60 + x.tm_sec +
78 PartOfSecond());
79 }
80 [[nodiscard]] InternalTimeStep EpochTime() const {
81 return time.time_since_epoch();
82 }
83
84 // Operations
85 InternalTimeStep operator-(const Time& t) const noexcept {
86 return time - t.time;
87 }
88 bool operator<(const Time& t) const noexcept { return time < t.time; }
89 bool operator==(const Time& t) const noexcept { return time == t.time; }
90 bool operator!=(const Time& t) const noexcept {
91 return not this->operator==(t);
92 }
93 bool operator<=(const Time& t) const noexcept {
94 return this->operator<(t) or this->operator==(t);
95 }
96 bool operator>(const Time& t) const noexcept {
97 return not this->operator<=(t);
98 }
99 bool operator>=(const Time& t) const noexcept {
100 return this->operator>(t) or this->operator==(t);
101 }
102 template <typename T, typename R>
103 Time& operator+=(const std::chrono::duration<T, R>& dt) {
104 time += std::chrono::duration_cast<InternalTimeStep>(dt);
105 return *this;
106 }
107 template <typename T, typename R>
108 Time& operator-=(const std::chrono::duration<T, R>& dt) {
109 time -= std::chrono::duration_cast<InternalTimeStep>(dt);
110 return *this;
111 }
112 template <typename T, typename R>
113 Time operator+(const std::chrono::duration<T, R>& dt) const {
114 return (Time(*this) += dt);
115 }
116 template <typename T, typename R>
117 Time operator-(const std::chrono::duration<T, R>& dt) const {
118 return (Time(*this) -= dt);
119 }
120
121 // helpers
122 [[nodiscard]] Numeric Seconds() const {
123 return std::chrono::duration_cast<TimeStep>(time.time_since_epoch())
124 .count();
125 }
127 [[nodiscard]] Numeric PartOfSecond() const {
128 return std::fmod(Seconds(), 1.0);
129 }
130
131 // Conversion
132 explicit operator Numeric() const { return Seconds(); }
133
134 friend std::ostream& operator<<(std::ostream& os, const Time& t);
135
136 friend std::istream& operator>>(std::istream& is, Time& t);
137}; // Time
138
141
144
147
149std::ostream& operator<<(std::ostream& os, const TimeStep& dt);
150
163TimeStep time_stepper_selection(const String& time_step);
164
171Time next_even(const Time& t, const TimeStep& dt);
172
185ArrayOfIndex time_steps(const ArrayOfTime& times, const TimeStep& dt);
186
193Time mean_time(const ArrayOfTime& ts, Index s = 0, Index e = -1);
194
200Vector time_vector(const ArrayOfTime& times);
201
207ArrayOfTime time_vector(const Vector& times);
208
217
224
226struct DebugTime {
228 std::string_view msg;
229 DebugTime(const std::string_view s="Time") : msg(s) {}
231 #pragma omp critical
232 std::cerr << msg << ':' << ' ' << Time{} - start << '\n';
233 }
234};
235
236#endif // ARTSTIME_H
This file contains the definition of Array.
TimeStep median(ArrayOfTimeStep)
Returns the median time step.
Definition: artstime.cc:179
ArrayOfIndex time_steps(const ArrayOfTime &times, const TimeStep &dt)
Finds the index matching demands in a list of times.
Definition: artstime.cc:77
std::chrono::duration< Numeric > TimeStep
A duration of time, 1 full tick should be 1 second.
Definition: artstime.h:40
TimeStep time_stepper_selection(const String &time_step)
Returns a time step from valid string.
Definition: artstime.cc:37
Vector time_vector(const ArrayOfTime &times)
Converts from each Time to seconds and returns as Vector.
Definition: artstime.cc:167
Time mean_time(const ArrayOfTime &ts, Index s=0, Index e=-1)
Computes the average time in a list.
Definition: artstime.cc:151
TimeStep mean(const ArrayOfTimeStep &)
Returns the mean time step.
Definition: artstime.cc:190
std::ostream & operator<<(std::ostream &os, const TimeStep &dt)
Debug output for duration.
Definition: artstime.cc:199
Time next_even(const Time &t, const TimeStep &dt)
Returns the next time after t with an even time-step.
Definition: artstime.cc:68
This can be used to make arrays out of anything.
Definition: array.h:48
The Vector class.
Definition: matpackI.h:922
Helper macros for debugging.
#define ARTS_USER_ERROR_IF(condition,...)
Definition: debug.h:134
std::time_t to_time_t(TimePoint time_point)
Definition: main.cc:582
Implementation of Matrix, Vector, and such stuff.
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
This file contains the definition of String, the ARTS string class.
VectorView std(VectorView std, const Vector &y, const ArrayOfVector &ys, const Index start, const Index end_tmp)
Compute the standard deviation of the ranged ys.
Definition: raw.cc:205
Used to debug execution time, prints msg+time on destruction to std::cerr.
Definition: artstime.h:226
DebugTime(const std::string_view s="Time")
Definition: artstime.h:229
std::string_view msg
Definition: artstime.h:228
~DebugTime()
Definition: artstime.h:230
Time start
Definition: artstime.h:227
Class to handle time in ARTS.
Definition: artstime.h:43
std::time_t toTimeT() const
Definition: artstime.h:58
Numeric Seconds() const
Definition: artstime.h:122
InternalTimeStep EpochTime() const
Definition: artstime.h:80
Numeric PartOfSecond() const
Definition: artstime.h:127
Time operator-(const std::chrono::duration< T, R > &dt) const
Definition: artstime.h:117
Time()
Definition: artstime.h:51
bool operator<(const Time &t) const noexcept
Definition: artstime.h:88
decltype(time)::duration InternalTimeStep
Definition: artstime.h:45
std::chrono::system_clock::time_point time
Definition: artstime.h:44
bool operator==(const Time &t) const noexcept
Definition: artstime.h:89
TimeStep seconds_into_day() const
Definition: artstime.h:75
Time & operator+=(const std::chrono::duration< T, R > &dt)
Definition: artstime.h:103
InternalTimeStep operator-(const Time &t) const noexcept
Definition: artstime.h:85
bool operator!=(const Time &t) const noexcept
Definition: artstime.h:90
Time(std::tm t)
Definition: artstime.h:54
bool operator>=(const Time &t) const noexcept
Definition: artstime.h:99
Index Version() const noexcept
Definition: artstime.h:48
std::tm toStruct() const
Definition: artstime.h:61
bool operator<=(const Time &t) const noexcept
Definition: artstime.h:93
friend std::istream & operator>>(std::istream &is, Time &t)
Definition: artstime.cc:122
friend std::ostream & operator<<(std::ostream &os, const Time &t)
Definition: artstime.cc:104
void Seconds(Numeric x)
Definition: artstime.h:126
Time & operator-=(const std::chrono::duration< T, R > &dt)
Definition: artstime.h:108
Time(std::time_t t)
Definition: artstime.h:52
Time operator+(const std::chrono::duration< T, R > &dt) const
Definition: artstime.h:113
bool operator>(const Time &t) const noexcept
Definition: artstime.h:96
std::tm toGMTStruct() const
Definition: artstime.h:68