ztsdb
ztime.hpp
1 // (C) 2016 Leonardo Silvestri
2 //
3 // This file is part of ztsdb.
4 //
5 // ztsdb is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // ztsdb is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with ztsdb. If not, see <http://www.gnu.org/licenses/>.
17 
18 
19 #ifndef ZTIME_HPP
20 #define ZTIME_HPP
21 
22 
23 #include "../globals.hpp"
24 #include "../misc.hpp"
25 #include "zone.hpp"
26 #include "interval.hpp"
27 
28 
29 namespace tz {
30 
31  template <class Rep, class Period, class = std::enable_if_t<
32  std::chrono::duration<Rep, Period>::min() < std::chrono::duration<Rep, Period>::zero()>>
33  constexpr std::chrono::duration<Rep, Period> abs(std::chrono::duration<Rep, Period> d)
34  {
35  return d >= d.zero() ? d : -d;
36  }
37 
38  Global::duration duration_from_string2(const std::string& s);
39 
40 
41  std::string to_string(Global::dtime dt,
42  const std::string& format,
43  const tz::Zone& timezone,
44  const std::string& timezone_str,
45  bool abbrev=false,
46  bool fractional=false);
47 
48  Global::dtime dtime_from_numbers(int year,
49  unsigned month,
50  unsigned day,
51  unsigned hour,
52  unsigned minute,
53  unsigned second,
54  unsigned nsecond,
55  const tz::Zone& z);
56 
57  Global::dtime dtime_from_string(const std::string& s,
58  const tz::Zones& tzones,
59  const std::string& fmt = "%Y-%m-%d %H:%M:%S[.%s] %Z",
60  const std::string& tz = "");
61 
62  std::string to_string(const tz::interval& i,
63  const std::string& format,
64  const tz::Zone& timezone,
65  const std::string& timezone_str,
66  bool abbrev=false,
67  bool fractional=false);
68 
69  interval interval_from_string(const std::string& s,
70  const tz::Zones& tzones,
71  const std::string& fmt = "%Y-%m-%d %H:%M:%S[.%s] %Z",
72  const std::string& tz = "");
73 
74  std::string to_string(Global::duration d);
75 
76  enum class Period : uint64_t { NANO, MICRO, MILLI, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, YEAR };
77 
78  tz::Period unqualified_period_from_string(const std::string& s);
79 }
80 
81 
82 namespace ztsdb {
83 
84  template<>
85  struct multiplies<Global::duration, double, Global::duration> {
86  inline Global::duration operator()(const Global::duration& t, const double& u) const {
87  if (std::isnan(u)) {
88  throw std::range_error("duration multiply by NaN");
89  }
90  else if (std::isinf(u)) {
91  throw std::range_error("duration multiply by Inf");
92  }
93  return Global::duration(static_cast<Global::duration::rep>(t.count() * u));
94  }
95  };
96 
97  template<>
98  struct multiplies<double, Global::duration, Global::duration> {
99  inline Global::duration operator()(const double& u, const Global::duration& t) const {
100  if (std::isnan(u)) {
101  throw std::range_error("duration multiply by NaN");
102  }
103  else if (std::isinf(u)) {
104  throw std::range_error("duration multiply by Inf");
105  }
106  return Global::duration(static_cast<Global::duration::rep>(t.count() * u));
107  }
108  };
109 
110  template<>
111  struct divides<Global::duration, double, Global::duration> {
112  inline Global::duration operator()(const Global::duration& t, const double& u) const {
113  if (std::isnan(u)) {
114  throw std::range_error("duration divide by NaN");
115  }
116  else if (u == 0) {
117  throw std::range_error("duration divide by 0");
118  }
119  else if (std::isinf(u)) {
120  throw std::range_error("duration divide by Inf");
121  }
122  return Global::duration(static_cast<Global::duration::rep>(t.count() / u));
123  }
124  };
125 
126  Global::dtime floor(Global::dtime t, tz::Period p);
127  tz::interval floor(tz::interval i, tz::Period p);
128 
129  Global::dtime floor_tz(Global::dtime t, tz::Period p, const tz::Zone& z);
130  tz::interval floor_tz(tz::interval i, tz::Period p, const tz::Zone& z);
131 
132  Global::dtime ceiling(Global::dtime t, tz::Period p);
133  tz::interval ceiling(tz::interval i, tz::Period p);
134 
135  Global::dtime ceiling_tz(Global::dtime t, tz::Period p, const tz::Zone& z);
136  tz::interval ceiling_tz(tz::interval i, tz::Period p, const tz::Zone& z);
137 
138  size_t dayweek(Global::dtime, const tz::Zone& z);
139  size_t daymonth(Global::dtime t, const tz::Zone& z);
140  size_t month(Global::dtime t, const tz::Zone& z);
141  ssize_t year(Global::dtime t, const tz::Zone& z);
142 
143 }
144 
145 
146 #endif
tz::Zone
Definition: zone.hpp:38
ztsdb::multiplies
Definition: misc.hpp:166
tz
Timezone handling and temporal types and functions depending on timezones.
Definition: period.hpp:28
ztsdb::divides
Definition: misc.hpp:173
tz::interval
Definition: interval.hpp:31
tz::Zones
Definition: zone.hpp:66