ASAP  1.0.0
A C++ header-only library for creating, displaying, iterating and manipulating dates
datetime.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_DATETIME_H
22 #define ASAP_DATETIME_H
23 
24 #include "duration.h"
25 
26 namespace asap {
27  class period;
28 
29  class datetime {
30  public:
31  explicit datetime(time_t time = std::time(nullptr)) noexcept;
32  explicit datetime(const std::string & datetime, const std::string & format = "%x %X", const std::string & locale = "");
33  datetime(uint32_t year, uint32_t month, uint32_t day, uint32_t hours = 0, uint32_t minutes = 0, uint32_t seconds = 0);
34 
35  datetime & operator+=(const seconds & d);
36  datetime & operator+=(const minutes & d);
37  datetime & operator+=(const hours & d);
38  datetime & operator+=(const days & d);
39  datetime & operator+=(const weeks & d);
40  datetime & operator+=(const months & d);
41  datetime & operator+=(const years & d);
42  asap::seconds operator-(const datetime & other) const;
43  template<uint64_t convert> asap::datetime & operator-=(const asap::duration<convert> & c);
44  asap::datetime & operator+=(time_t stamp);
45  asap::datetime & operator-=(time_t stamp);
46 
50 
51  time_t timestamp() const;
52  std::string str(const std::string & fmt = "%x %X") const;
53  asap::period until(const asap::datetime & dt) const;
54 
55  int second() const;
56  asap::datetime & second(int value);
57 
58  int minute() const;
59  asap::datetime & minute(int value);
60 
61  int hour() const;
62  asap::datetime & hour(int value);
63 
64  int wday() const;
65  asap::datetime & wday(int value);
66 
67  int mday() const;
68  asap::datetime & mday(int value);
69 
70  int yday() const;
71  asap::datetime & yday(int value);
72 
73  int month() const;
74  asap::datetime & month(int value);
75 
76  int year() const;
77  asap::datetime & year(int value);
78 
79  private:
80  void add(long seconds);
81  std::tm when;
82  };
83 }
84 
85 #endif // ASAP_DATETIME_H
int yday() const
Returns the day in a year specified in this asap::datetime.
asap::period until(const asap::datetime &dt) const
Creates an instance of asap::period starting at this datetime and ending at dt.
int minute() const
Returns the minutes of an hour specified in this asap::datetime.
int mday() const
Returns the day of a month specified in this asap::datetime.
int wday() const
Returns the day of a week specified in this asap::datetime.
datetime & operator+=(const seconds &d)
Adds a duration specified in seconds to a datetime.
datetime(time_t time=std::time(nullptr)) noexcept
Constructs a asap::datetime from a time_t, defaulting to the current time.
int second() const
Returns the seconds of a minute specified in this asap::datetime.
asap::datetime afternoon()
Creates a new asap::datetime representing the afternoon of the same day.
asap::seconds operator-(const datetime &other) const
Returns a duration in seconds specifying the difference to another datetime.
int month() const
Returns the month in a year specified in this asap::datetime.
std::string str(const std::string &fmt="%x %X") const
Converts to an string using the format specified.
asap::datetime morning()
Creates a new asap::datetime representing the morning of the same day.
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
The asap::period class is a bound period, with starting and ending dates. Its purpose is to provide d...
Definition: period.h:31
asap::datetime midnight()
Creates a new asap::datetime representing the midnight of the same day.
int hour() const
Returns the hours of a day specified in this asap::datetime.
asap::datetime & operator-=(const asap::duration< convert > &c)
Subtracts a duration from this datetime.
Definition: asap.h:38
int year() const
Returns the year specified in this asap::datetime.