My Project
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 <ctime>
25 #include <string>
26 #include <unordered_map>
27 #include <opm/input/eclipse/Deck/DeckRecord.hpp>
28 
29 
30 namespace Opm {
31 
32  using time_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<int64_t, std::ratio<1,1000>>>;
33 
34  namespace TimeService {
35  std::time_t to_time_t(const time_point& tp);
36  time_point from_time_t(std::time_t t);
37  time_point now();
38 
39  std::time_t advance(const std::time_t tp, const double sec);
40  std::time_t makeUTCTime(std::tm timePoint);
41  const std::unordered_map<std::string , int>& eclipseMonthIndices();
42  const std::unordered_map<int, std::string>& eclipseMonthNames();
43  int eclipseMonth(const std::string& name);
44  bool valid_month(const std::string& month_name);
45 
46  std::time_t mkdatetime(int in_year, int in_month, int in_day, int hour, int minute, int second);
47  std::time_t mkdate(int in_year, int in_month, int in_day);
48  std::time_t timeFromEclipse(const DeckRecord &dateRecord);
49  }
50 
52  {
53  public:
54  struct YMD {
55  int year{0};
56  int month{0};
57  int day{0};
58 
59  bool operator==(const YMD& data) const
60  {
61  return year == data.year &&
62  month == data.month &&
63  day == data.day;
64  }
65 
66  template<class Serializer>
67  void serializeOp(Serializer& serializer)
68  {
69  serializer(year);
70  serializer(month);
71  serializer(day);
72  }
73  };
74 
75  TimeStampUTC() = default;
76 
77  explicit TimeStampUTC(const std::time_t tp);
78  explicit TimeStampUTC(const YMD& ymd);
79  TimeStampUTC(int year, int month, int day);
80  TimeStampUTC(const YMD& ymd,
81  int hour,
82  int minutes,
83  int seconds,
84  int usec);
85 
86  TimeStampUTC& operator=(const std::time_t tp);
87  bool operator==(const TimeStampUTC& data) const;
88 
89  TimeStampUTC& hour(const int h);
90  TimeStampUTC& minutes(const int m);
91  TimeStampUTC& seconds(const int s);
92  TimeStampUTC& microseconds(const int us);
93 
94  const YMD& ymd() const { return ymd_; }
95  int year() const { return this->ymd_.year; }
96  int month() const { return this->ymd_.month; }
97  int day() const { return this->ymd_.day; }
98  int hour() const { return this->hour_; }
99  int minutes() const { return this->minutes_; }
100  int seconds() const { return this->seconds_; }
101  int microseconds() const { return this->usec_; }
102 
103  template<class Serializer>
104  void serializeOp(Serializer& serializer)
105  {
106  ymd_.serializeOp(serializer);
107  serializer(hour_);
108  serializer(minutes_);
109  serializer(seconds_);
110  serializer(usec_);
111  }
112 
113  private:
114 
115  YMD ymd_{};
116  int hour_{0};
117  int minutes_{0};
118  int seconds_{0};
119  int usec_{0};
120  };
121 
122  TimeStampUTC operator+(const TimeStampUTC& lhs, std::chrono::duration<double> delta);
123  std::time_t asTimeT(const TimeStampUTC& tp);
124  std::time_t asLocalTimeT(const TimeStampUTC& tp);
125  time_point asTimePoint(const TimeStampUTC& tp);
126 
127 
128 } // namespace Opm
129 
130 #endif // OPM_TIMESERVICE_HEADER_INCLUDED
Definition: Serializer.hpp:38
Definition: TimeService.hpp:52
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29
Definition: TimeService.hpp:54