ztsdb
period.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 #ifndef PERIOD_HPP
19 #define PERIOD_HPP
20 
21 
22 #include <sstream>
23 #include "globals.hpp"
24 #include "timezone/zone.hpp"
25 #include "timezone/interval.hpp"
26 
27 
28 namespace tz {
29 
30  struct period {
31  typedef int32_t month_t ;
32  typedef int32_t day_t;
33 
34  period();
35  period(int32_t months_p, int32_t days_p, Global::duration dur_p);
36  period(const std::string& s);
37 
38  inline int32_t getMonths() const { return months; }
39  inline int32_t getDays() const { return days; }
40  inline Global::duration getDuration() const { return dur; }
41  inline void setMonths(int64_t m) { months = m; }
42  inline void setDays(int64_t d) { days = d; }
43  inline void setDuration(Global::duration d) { dur = d; }
44  inline void addMonths(int64_t m) { months += m; }
45  inline void addDays(int64_t d) { days += d; }
46  inline void addDuration(Global::duration d) { dur += d; }
47 
48  inline bool operator==(const period& p) { return months==p.months && days==p.days; }
49  inline bool operator!=(const period& p) { return months!=p.months || days!=p.days; }
50 
51  private:
52  month_t months;
53  day_t days;
54  Global::duration dur;
55  };
56 
57  // move this block out so period doesn't have a dependency on
58  // interval and timezone, or can we consider it's a natural
59  // dependency? LLL
60  Global::dtime plus (const Global::dtime& dt, const period& p, const tz::Zone& z);
61  Global::dtime plus (const period& p, const Global::dtime& dt, const tz::Zone& z);
62  Global::dtime minus(const Global::dtime& dt, const period& p, const tz::Zone& z);
63  tz::interval plus (const tz::interval& i, const period& p, const tz::Zone& z);
64  tz::interval plus (const period& p, const tz::interval& i, const tz::Zone& z);
65  tz::interval minus(const tz::interval& i, const period& p, const tz::Zone& z);
66 
67  period operator-(const period& p);
68  period operator+(const period& p1, const period& p2);
69  period operator-(const period& p1, const period& p2);
70  period operator*(const period& p1, double d);
71  period operator*(double d, const period& p1);
72  period operator/(const period& p1, double d);
73 
74  bool operator==(const period& p1, const period& p2);
75  bool operator!=(const period& p1, const period& p2);
76 
79  inline bool operator<(const period& p1, const period& p2) { return false; }
80 
81  std::string to_string(const period& p);
82 
83 }
84 
85 
86 #endif
tz::operator<
bool operator<(const period &p1, const period &p2)
Definition: period.hpp:79
tz::Zone
Definition: zone.hpp:38
tz::period
Definition: period.hpp:30
tz
Timezone handling and temporal types and functions depending on timezones.
Definition: period.hpp:28
tz::interval
Definition: interval.hpp:31