BoosterSeat  0.1
A C++ library that includes common utilities that are used in other projects.
time.hpp
Go to the documentation of this file.
1 #ifndef TIME_HPP_
2 #define TIME_HPP_
3 
4 #include <BoosterSeat/clock.hpp>
5 #include <iomanip>
6 #include <sstream>
7 #include <string>
8 
9 namespace bst {
10 
12 namespace time {
13 
14 enum class TimeZone { UTC, LOCAL };
15 
16 std::string elapsedAsciiClock(const bst::clck::TimePoint &time_point);
17 
26 std::string timeString(TimeZone time_zone = TimeZone::LOCAL,
27  char delimiter = ':',
28  bst::clck::TimePoint time_point = bst::clck::now());
29 
30 std::string dateString(TimeZone time_zone = TimeZone::LOCAL,
31  char delimiter = '-');
32 
37 std::string
39  char date_delimiter = '-', char between_delimiter = ' ',
40  char time_delimiter = ':',
41  bst::clck::TimePoint time_point = bst::clck::now());
42 
43 inline std::string dateAndTimeString(const bst::clck::TimePoint &time_point) {
44  return dateAndTimeString(TimeZone::LOCAL, '-', ' ', ':', time_point);
45 }
46 
59 bst::clck::TimePoint dateAndTimeToTimePoint(int year, int month, int day,
60  int hour, int minute, int second);
61 
62 } // namespace time
63 
64 } // namespace bst
65 
66 namespace bst {
67 
70 uint32_t getUnixTime();
71 
79 class Time {
80 public:
85  Time(bool set_to_now = false);
86  ~Time() = default;
87 
88  // operators
89  bool operator==(const Time &rhs) const;
90  bool operator!=(const Time &rhs) const;
91  bool operator<(const Time &rhs) const;
92  bool operator<=(const Time &rhs) const;
93  bool operator>(const Time &rhs) const;
94  bool operator>=(const Time &rhs) const;
95 
99  void setToNow();
100 
105  std::string toString() const;
106 
116  bool fromString(const std::string &time_string);
117 
122  unsigned int getYear() const;
123 
128  unsigned int getMonth() const;
129 
134  unsigned int getDay() const;
135 
140  unsigned int getHour() const;
141 
146  unsigned int getMinute() const;
147 
152  unsigned int getSecond() const;
153 
154  time_t getTimeType() const {
155  return time_;
156  }
157 
164  int64_t secondsFromNow() const;
165 
166 private:
167  time_t time_ = 0;
168 };
169 
170 } // namespace bst
171 
172 #endif // TIME_HPP_
The generic time class for BoosterSeat. Uses UTC time.
Definition: time.hpp:79
bool operator!=(const Time &rhs) const
Definition: time.cpp:221
Time(bool set_to_now=false)
Construct a new Time object. Defaults to the epoch.
Definition: time.cpp:141
bool operator<(const Time &rhs) const
Definition: time.cpp:201
unsigned int getMinute() const
Get the Minute.
Definition: time.cpp:191
bool operator<=(const Time &rhs) const
Definition: time.cpp:209
time_t time_
Definition: time.hpp:167
void setToNow()
Set the time to the current time.
Definition: time.cpp:147
unsigned int getMonth() const
Get the Month (1 - 12)
Definition: time.cpp:176
~Time()=default
bool operator>(const Time &rhs) const
Definition: time.cpp:205
time_t getTimeType() const
Definition: time.hpp:154
bool fromString(const std::string &time_string)
Definition: time.cpp:157
bool operator==(const Time &rhs) const
Definition: time.cpp:217
unsigned int getYear() const
Get the Year.
Definition: time.cpp:171
unsigned int getSecond() const
Get the Second.
Definition: time.cpp:196
unsigned int getHour() const
Get the Hour.
Definition: time.cpp:186
unsigned int getDay() const
Get the Day.
Definition: time.cpp:181
int64_t secondsFromNow() const
Returns the number of seconds from the current time to the stored time.
Definition: time.cpp:225
std::string toString() const
Get the time/date as a string in the format: YYYY-MM-DD HH:MM:SS.
Definition: time.cpp:151
bool operator>=(const Time &rhs) const
Definition: time.cpp:213
std::chrono::time_point< Clock > TimePoint
Definition: clock.hpp:16
TimePoint now()
Definition: clock.hpp:19
std::string dateAndTimeString(TimeZone time_zone=TimeZone::LOCAL, char date_delimiter='-', char between_delimiter=' ', char time_delimiter=':', bst::clck::TimePoint time_point=bst::clck::now())
Get the date and time in the format: YYYY-MM-DD HH:MM:SS.
Definition: time.cpp:56
std::string elapsedAsciiClock(const bst::clck::TimePoint &time_point)
Definition: time.cpp:15
std::string timeString(TimeZone time_zone=TimeZone::LOCAL, char delimiter=':', bst::clck::TimePoint time_point=bst::clck::now())
Get the time in format: HH:MM:SS (24 hour clock) for a given time zone.
Definition: time.cpp:39
bst::clck::TimePoint dateAndTimeToTimePoint(int year, int month, int day, int hour, int minute, int second)
Convert a date and time to a bst::clck::TimePoint.
Definition: time.cpp:73
std::string dateString(TimeZone time_zone=TimeZone::LOCAL, char delimiter='-')
Definition: time.cpp:48
TimeZone
Definition: time.hpp:14
Definition: filesystem.cpp:34
uint32_t getUnixTime()
Get the current Unix time.
Definition: time.cpp:134