ASAP  1.0.0
A C++ header-only library for creating, displaying, iterating and manipulating dates
duration.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_DURATION_H
22 #define ASAP_DURATION_H
23 
24 #include "constants.h"
25 
26 namespace asap {
27  template<uint64_t convert = 1>
28  class duration {
29  public:
30  explicit duration(double v = 0);
31  template<uint64_t convertfrom> duration(const asap::duration<convertfrom> & other);
32  explicit operator double() const;
33  duration<convert> & operator=(double v);
34  double operator*() const;
36  template<uint64_t other> operator duration<other>() const;
37  template<uint64_t convert2> duration<convert> & operator+=(const duration<convert2> & other);
39  template<uint64_t convert2> duration<convert> & operator-=(const duration<convert2> & other);
40  template<uint64_t convert2> duration<convert> & operator=(const duration<convert2> & other);
41  std::string str() const;
42 
43  private:
44  double value;
45  };
46 
47  using seconds = duration<1>; using second = seconds;
54 }
55 
56 #endif
duration< SECONDS_IN_HOUR > hours
A duration specified in hours.
Definition: duration.h:49
duration(double v=0)
Constructs a duration with the value set to v.
duration< 1 > seconds
A duration specified in seconds.
Definition: duration.h:47
duration< SECONDS_IN_MINUTE > minutes
A duration specified in minutes.
Definition: duration.h:48
duration< SECONDS_IN_DAY > days
A duration specified in days.
Definition: duration.h:50
duration< convert > operator-()
Changes the sign of a duration.
duration< SECONDS_IN_YEAR > years
A duration specified in years.
Definition: duration.h:53
duration< convert > & operator+=(const duration< convert2 > &other)
Adds another duration to this duration, converting to this convertion factor (e.g, 1_day + 1_week == 1_day + 7_days)
duration< convert > & operator=(double v)
Sets the internal value to v.
duration< convert > & operator-=(const duration< convert2 > &other)
Subtracts another duration from this duration, converting to this convertion factor (e...
duration< SECONDS_IN_WEEK > weeks
A duration specified in weeks (7 days)
Definition: duration.h:51
duration< SECONDS_IN_MONTH > months
A duration specified in months (30 days)
Definition: duration.h:52
The asap::duration class exposes methods to create, manipulate and print durations.
Definition: duration.h:28
double operator*() const
the same as asap::duration::operator double()
Definition: asap.h:38
std::string str() const
Pretty print this duration, breaking down all the units (e.g: std::cout << 10_days would print 1 week...