ARTS  2.3.1285(git:92a29ea9-dirty)
artstime.h
Go to the documentation of this file.
1 /* Copyright (C) 2020
2  * Richard Larsson <larsson@mps.mpg.de>
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 "mystring.h"
35 
37 using TimeStep = std::chrono::duration<Numeric>;
38 
40 class Time {
41 private:
42  std::chrono::system_clock::time_point mtime;
43 
44 public:
45  using InternalTimeStep = decltype(mtime)::duration;
46 
47  // Version will be updated when C++20 datetime is available... Version 1 will still assume local time at that time
48  Index Version() const noexcept {return 1;}
49 
50  // Construction
51  Time() : mtime(std::chrono::system_clock::now()) {}
52  explicit Time(std::time_t t) : mtime(std::chrono::system_clock::from_time_t(t)) {}
53  explicit Time(std::tm t) : Time(std::mktime(&t)) {}
54 
55  // Data
56  const std::chrono::system_clock::time_point& Data() const {return mtime;}
57 
58  // Conversions
59  std::time_t toTimeT() const {return std::chrono::system_clock::to_time_t(mtime);}
60  std::tm toStruct() const {std::time_t x=toTimeT(); std::tm* y = std::localtime(&x); return *y;}
61  std::tm toGMTStruct() const {std::time_t x=toTimeT(); std::tm* y = std::gmtime(&x); return *y;}
62  TimeStep seconds_into_day() const {std::tm x = toStruct(); return TimeStep(x.tm_hour*3600 + x.tm_min*60 + x.tm_sec + PartOfSecond());}
63  InternalTimeStep EpochTime() const {return mtime.time_since_epoch();}
64 
65  // Operations
66  InternalTimeStep operator-(const Time& t) const {return mtime - t.mtime;}
67  bool operator<(const Time& t) const {return mtime < t.mtime;}
68  bool operator==(const Time& t) const {return mtime == t.mtime;}
69  template <typename T, typename R> Time& operator+=(const std::chrono::duration<T, R>& dt) {mtime += std::chrono::duration_cast<InternalTimeStep>(dt); return *this;}
70  template <typename T, typename R> Time& operator-=(const std::chrono::duration<T, R>& dt) {mtime -= std::chrono::duration_cast<InternalTimeStep>(dt); return *this;}
71  template <typename T, typename R> Time operator+(const std::chrono::duration<T, R>& dt) const {return (Time(*this) += dt);}
72  template <typename T, typename R> Time operator-(const std::chrono::duration<T, R>& dt) const {return (Time(*this) -= dt);}
73 
74  // helpers
75  Numeric Seconds() const {return std::chrono::duration_cast<TimeStep>(mtime.time_since_epoch()).count();}
77  Numeric PartOfSecond() const {return std::fmod(Seconds(), 1.0);}
78 }; // Time
79 
82 
85 
87 std::ostream& operator<<(std::ostream& os, const Time& t);
88 
90 std::istream& operator>>(std::istream& is, Time& t);
91 
93 inline std::ostream& operator<<(std::ostream& os, const TimeStep& dt) {return os << dt.count() << " seconds";}
94 
101 Time next_even(const Time& t, const TimeStep& dt);
102 
115 ArrayOfIndex time_steps(const ArrayOfTime& times, const String& step);
116 
123 Time mean_time(const ArrayOfTime& ts, Index s=0, Index e=-1);
124 
125 #endif // ARTSTIME_H
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
std::tm toStruct() const
Definition: artstime.h:60
std::time_t toTimeT() const
Definition: artstime.h:59
Time(std::time_t t)
Definition: artstime.h:52
std::ostream & operator<<(std::ostream &os, const Time &t)
Output for Time.
Definition: artstime.cc:90
Numeric Seconds() const
Definition: artstime.h:75
std::chrono::system_clock::time_point mtime
Definition: artstime.h:42
Numeric PartOfSecond() const
Definition: artstime.h:77
Time(std::tm t)
Definition: artstime.h:53
Class to handle time in ARTS.
Definition: artstime.h:40
Time next_even(const Time &t, const TimeStep &dt)
Returns the next time after t with an even time-step.
Definition: artstime.cc:50
std::chrono::duration< Numeric > TimeStep
A duration of time, 1 full tick should be 1 second.
Definition: artstime.h:37
InternalTimeStep EpochTime() const
Definition: artstime.h:63
Index Version() const noexcept
Definition: artstime.h:48
Time()
Definition: artstime.h:51
std::istream & operator>>(std::istream &is, Time &t)
Input for Time.
Definition: artstime.cc:108
void Seconds(Numeric x)
Definition: artstime.h:76
This file contains the definition of Array.
Time & operator-=(const std::chrono::duration< T, R > &dt)
Definition: artstime.h:70
TimeStep seconds_into_day() const
Definition: artstime.h:62
ArrayOfIndex time_steps(const ArrayOfTime &times, const String &step)
Finds the index matching demands in a list of times.
Definition: artstime.cc:60
decltype(mtime)::duration InternalTimeStep
Definition: artstime.h:45
const std::chrono::system_clock::time_point & Data() const
Definition: artstime.h:56
Time operator+(const std::chrono::duration< T, R > &dt) const
Definition: artstime.h:71
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
Time & operator+=(const std::chrono::duration< T, R > &dt)
Definition: artstime.h:69
This can be used to make arrays out of anything.
Definition: array.h:40
std::tm toGMTStruct() const
Definition: artstime.h:61
bool operator<(const Time &t) const
Definition: artstime.h:67
Time mean_time(const ArrayOfTime &ts, Index s=0, Index e=-1)
Computes the average time in a list.
Definition: artstime.cc:135
InternalTimeStep operator-(const Time &t) const
Definition: artstime.h:66
Time operator-(const std::chrono::duration< T, R > &dt) const
Definition: artstime.h:72
This file contains the definition of String, the ARTS string class.
bool operator==(const Time &t) const
Definition: artstime.h:68