opm-common
TimeService.hpp
1 /*
2  Copyright 2019 Equinor ASA.
3 
4  This file is part of the Open Porous Media Project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef OPM_TIMESERVICE_HEADER_INCLUDED
21 #define OPM_TIMESERVICE_HEADER_INCLUDED
22 
23 #include <chrono>
24 #include <cstdint>
25 #include <ctime>
26 #include <string>
27 #include <unordered_map>
28 
29 namespace Opm {
30 
31  class DeckRecord;
32 
33  using time_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<std::int64_t, std::ratio<1,1000>>>;
34 
35  namespace TimeService {
36  std::time_t to_time_t(const time_point& tp);
37  time_point from_time_t(std::time_t t);
38  time_point now();
39 
40  std::time_t advance(const std::time_t tp, const double sec);
41  std::time_t makeUTCTime(std::tm timePoint);
42  const std::unordered_map<std::string , int>& eclipseMonthIndices();
43  const std::unordered_map<int, std::string>& eclipseMonthNames();
44  int eclipseMonth(const std::string& name);
45  bool valid_month(const std::string& month_name);
46 
47  std::time_t mkdatetime(int in_year, int in_month, int in_day, int hour, int minute, int second);
48  std::time_t mkdate(int in_year, int in_month, int in_day);
49  std::time_t portable_timegm(const std::tm* t);
50  std::time_t timeFromEclipse(const DeckRecord &dateRecord);
51  }
52 
54  {
55  public:
56  struct YMD {
57  int year{0};
58  int month{0};
59  int day{0};
60 
61  bool operator==(const YMD& data) const
62  {
63  return year == data.year &&
64  month == data.month &&
65  day == data.day;
66  }
67 
68  template<class Serializer>
69  void serializeOp(Serializer& serializer)
70  {
71  serializer(year);
72  serializer(month);
73  serializer(day);
74  }
75  };
76 
77  TimeStampUTC() = default;
78 
79  explicit TimeStampUTC(const std::time_t tp);
80  explicit TimeStampUTC(const YMD& ymd);
81  TimeStampUTC(int year, int month, int day);
82  TimeStampUTC(const YMD& ymd,
83  int hour,
84  int minutes,
85  int seconds,
86  int usec);
87 
88  TimeStampUTC& operator=(const std::time_t tp);
89  bool operator==(const TimeStampUTC& data) const;
90 
91  TimeStampUTC& hour(const int h);
92  TimeStampUTC& minutes(const int m);
93  TimeStampUTC& seconds(const int s);
94  TimeStampUTC& microseconds(const int us);
95 
96  const YMD& ymd() const { return ymd_; }
97  int year() const { return this->ymd_.year; }
98  int month() const { return this->ymd_.month; }
99  int day() const { return this->ymd_.day; }
100  int hour() const { return this->hour_; }
101  int minutes() const { return this->minutes_; }
102  int seconds() const { return this->seconds_; }
103  int microseconds() const { return this->usec_; }
104 
105  template<class Serializer>
106  void serializeOp(Serializer& serializer)
107  {
108  serializer(ymd_);
109  serializer(hour_);
110  serializer(minutes_);
111  serializer(seconds_);
112  serializer(usec_);
113  }
114 
115  private:
116 
117  YMD ymd_{};
118  int hour_{0};
119  int minutes_{0};
120  int seconds_{0};
121  int usec_{0};
122  };
123 
124  TimeStampUTC operator+(const TimeStampUTC& lhs, std::chrono::duration<double> delta);
125  std::time_t asTimeT(const TimeStampUTC& tp);
126  std::time_t asLocalTimeT(const TimeStampUTC& tp);
127  time_point asTimePoint(const TimeStampUTC& tp);
128 
129 
130 } // namespace Opm
131 
132 #endif // OPM_TIMESERVICE_HEADER_INCLUDED
Definition: TimeService.hpp:53
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
Class for (de-)serializing.
Definition: Serializer.hpp:95
Definition: TimeService.hpp:56