ARTS 2.5.0 (git: 9ee3ac6c)
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
33#include "array.h"
34#include "matpackI.h"
35#include "mystring.h"
36
38using TimeStep = std::chrono::duration<Numeric>;
39
41class Time {
42private:
43 std::chrono::system_clock::time_point mtime;
44
45public:
46 using InternalTimeStep = decltype(mtime)::duration;
47
48 // Version will be updated when C++20 datetime is available... Version 1 will still assume local time at that time
49 [[nodiscard]] Index Version() const noexcept {return 1;}
50
51 // Construction
52 Time() : mtime(std::chrono::system_clock::now()) {}
53 explicit Time(std::time_t t) : mtime(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 // Data
58 [[nodiscard]] const std::chrono::system_clock::time_point& Data() const {return mtime;}
59
60 // Conversions
61 [[nodiscard]] std::time_t toTimeT() const {return std::chrono::system_clock::to_time_t(mtime);}
62 [[nodiscard]] std::tm toStruct() const {std::time_t x=toTimeT(); std::tm* y = std::localtime(&x); return *y;}
63 [[nodiscard]] std::tm toGMTStruct() const {std::time_t x=toTimeT(); std::tm* y = std::gmtime(&x); return *y;}
64 [[nodiscard]] TimeStep seconds_into_day() const {std::tm x = toStruct(); return TimeStep(x.tm_hour*3600 + x.tm_min*60 + x.tm_sec + PartOfSecond());}
65 [[nodiscard]] InternalTimeStep EpochTime() const {return mtime.time_since_epoch();}
66
67 // Operations
68 InternalTimeStep operator-(const Time& t) const noexcept {return mtime - t.mtime;}
69 bool operator<(const Time& t) const noexcept {return mtime < t.mtime;}
70 bool operator==(const Time& t) const noexcept {return mtime == t.mtime;}
71 bool operator!=(const Time& t) const noexcept {return not this -> operator==(t);}
72 bool operator<=(const Time& t) const noexcept {return this -> operator<(t) or this -> operator==(t);}
73 bool operator>(const Time& t) const noexcept {return not this -> operator<=(t);}
74 bool operator>=(const Time& t) const noexcept {return this -> operator>(t) or this -> operator==(t);}
75 template <typename T, typename R> Time& operator+=(const std::chrono::duration<T, R>& dt) {mtime += std::chrono::duration_cast<InternalTimeStep>(dt); return *this;}
76 template <typename T, typename R> Time& operator-=(const std::chrono::duration<T, R>& dt) {mtime -= std::chrono::duration_cast<InternalTimeStep>(dt); return *this;}
77 template <typename T, typename R> Time operator+(const std::chrono::duration<T, R>& dt) const {return (Time(*this) += dt);}
78 template <typename T, typename R> Time operator-(const std::chrono::duration<T, R>& dt) const {return (Time(*this) -= dt);}
79
80 // helpers
81 [[nodiscard]] Numeric Seconds() const {return std::chrono::duration_cast<TimeStep>(mtime.time_since_epoch()).count();}
83 [[nodiscard]] Numeric PartOfSecond() const {return std::fmod(Seconds(), 1.0);}
84
85 // Conversion
86 explicit operator Numeric() const { return Seconds(); }
87}; // Time
88
91
94
97
99std::ostream& operator<<(std::ostream& os, const Time& t);
100
102std::istream& operator>>(std::istream& is, Time& t);
103
105inline std::ostream& operator<<(std::ostream& os, const TimeStep& dt) {return os << dt.count() << " seconds";}
106
119TimeStep time_stepper_selection(const String& time_step);
120
127Time next_even(const Time& t, const TimeStep& dt);
128
141ArrayOfIndex time_steps(const ArrayOfTime& times, const TimeStep& dt);
142
149Time mean_time(const ArrayOfTime& ts, Index s=0, Index e=-1);
150
156Vector time_vector(const ArrayOfTime& times);
157
163ArrayOfTime time_vector(const Vector& times);
164
173
180
181#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:38
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::istream & operator>>(std::istream &is, Time &t)
Input for Time.
Definition: artstime.cc:122
std::ostream & operator<<(std::ostream &os, const Time &t)
Output for Time.
Definition: artstime.cc:104
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:107
Class to handle time in ARTS.
Definition: artstime.h:41
std::time_t toTimeT() const
Definition: artstime.h:61
Numeric Seconds() const
Definition: artstime.h:81
InternalTimeStep EpochTime() const
Definition: artstime.h:65
Numeric PartOfSecond() const
Definition: artstime.h:83
const std::chrono::system_clock::time_point & Data() const
Definition: artstime.h:58
Time operator-(const std::chrono::duration< T, R > &dt) const
Definition: artstime.h:78
Time()
Definition: artstime.h:52
bool operator<(const Time &t) const noexcept
Definition: artstime.h:69
bool operator==(const Time &t) const noexcept
Definition: artstime.h:70
decltype(mtime)::duration InternalTimeStep
Definition: artstime.h:46
TimeStep seconds_into_day() const
Definition: artstime.h:64
Time & operator+=(const std::chrono::duration< T, R > &dt)
Definition: artstime.h:75
InternalTimeStep operator-(const Time &t) const noexcept
Definition: artstime.h:68
bool operator!=(const Time &t) const noexcept
Definition: artstime.h:71
Time(std::tm t)
Definition: artstime.h:54
bool operator>=(const Time &t) const noexcept
Definition: artstime.h:74
Index Version() const noexcept
Definition: artstime.h:49
std::tm toStruct() const
Definition: artstime.h:62
bool operator<=(const Time &t) const noexcept
Definition: artstime.h:72
void Seconds(Numeric x)
Definition: artstime.h:82
Time & operator-=(const std::chrono::duration< T, R > &dt)
Definition: artstime.h:76
Time(std::time_t t)
Definition: artstime.h:53
std::chrono::system_clock::time_point mtime
Definition: artstime.h:43
Time operator+(const std::chrono::duration< T, R > &dt) const
Definition: artstime.h:77
bool operator>(const Time &t) const noexcept
Definition: artstime.h:73
std::tm toGMTStruct() const
Definition: artstime.h:63
The Vector class.
Definition: matpackI.h:876
Implementation of Matrix, Vector, and such stuff.
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
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