ASAP  1.0.0
A C++ header-only library for creating, displaying, iterating and manipulating dates
operators.h
1 /* * Copyright (C) 2018 Leonardo Guilherme Lucena de Freitas
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy of
4  * this software and associated documentation files (the "Software"), to deal in
5  * the Software without restriction, including without limitation the rights to
6  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7  * the Software, and to permit persons to whom the Software is furnished to do so,
8  * subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in all
11  * copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #ifndef ASAP_OPERATORS_H
22 #define ASAP_OPERATORS_H
23 
24 #include "datetime.h"
25 #include "duration.h"
26 
27 namespace asap {
35  template<uint64_t convert>
36  inline static asap::datetime operator+(const asap::datetime & a, const duration<convert> & b) {
37  asap::datetime r = a;
38  r += b;
39  return r;
40  }
41 
49  template<uint64_t convert>
50  inline static asap::datetime operator-(const asap::datetime & a, const duration<convert> & b) {
51  asap::datetime r = a;
52  r -= b;
53  return r;
54  }
55 
56 
63  inline static asap::datetime operator+(const asap::datetime & a, std::time_t b) {
64  asap::datetime r = a;
65  r += b;
66  return r;
67  }
68 
75  inline static asap::datetime operator-(const asap::datetime & a, std::time_t b) {
76  asap::datetime r = a;
77  r -= b;
78  return r;
79  }
80 
89  template<uint64_t c1, uint64_t c2>
90  inline static duration<c1> operator+(const duration<c1> & a, const duration<c2> & b) {
91  duration<c1> r = a;
92  r += b;
93  return r;
94  };
95 
103  template<uint64_t conv>
104  inline static duration<conv> operator+(const duration<conv> & a, const duration<conv> & b) {
105  duration<conv> r = a;
106  r += b;
107  return r;
108  };
109 
118  template<uint64_t c1, uint64_t c2>
119  inline static duration<c1> operator-(const duration<c1> & a, const duration<c2> & b) {
120  duration<c1> r = a;
121  r -= b;
122  return r;
123  };
124 
131  template<uint64_t c1>
132  inline static duration<c1> operator-(const duration<c1> & a) {
133  duration<c1> r;
134  r -= a;
135  return r;
136  };
137 
144  inline static bool operator<(const asap::datetime & a, const asap::datetime & b) {
145  return a.timestamp() < b.timestamp();
146  }
147 
154  inline static bool operator>(const asap::datetime & a, const asap::datetime & b) {
155  return a.timestamp() > b.timestamp();
156  }
157 
164  inline static bool operator==(const asap::datetime & a, const asap::datetime & b) {
165  return a.timestamp() == b.timestamp();
166  }
167 
176  template<typename ostream>
177  inline static ostream & operator<<(ostream & os, const asap::datetime & dt) {
178  return os << dt.str(), os;
179  }
180 
190  template<typename ostream, uint64_t convert>
191  inline static ostream & operator<<(ostream & os, const duration<convert> & du) {
192  return os << du.str(), os;
193  }
194 }
195 
196 #endif //ASAP_OPERATORS_H
static bool operator==(const asap::datetime &a, const asap::datetime &b)
Compares if a == b is true for two asap::datetime instances.
Definition: operators.h:164
static bool operator>(const asap::datetime &a, const asap::datetime &b)
Compares if a > b is true for two asap::datetime instances.
Definition: operators.h:154
static asap::datetime operator-(const asap::datetime &a, const duration< convert > &b)
Subtracts a duration from a datetime.
Definition: operators.h:50
static bool operator<(const asap::datetime &a, const asap::datetime &b)
Compares if a < b is true for two asap::datetime instances.
Definition: operators.h:144
std::string str(const std::string &fmt="%x %X") const
Converts to an string using the format specified.
The asap::datetime class exposes methods to create, manipulate and print dates.
Definition: datetime.h:29
The asap::duration class exposes methods to create, manipulate and print durations.
Definition: duration.h:28
time_t timestamp() const
Converts an asap::datetime to a time_t
static asap::datetime operator+(const asap::datetime &a, const duration< convert > &b)
Adds a duration to a datetime.
Definition: operators.h:36
Definition: asap.h:38
static ostream & operator<<(ostream &os, const asap::datetime &dt)
Inserts a datetime into an standard output stream.
Definition: operators.h:177