ROSE 0.11.145.147
Time.h
1// WARNING: Changes to this file must be contributed back to Sawyer or else they will
2// be clobbered by the next update from Sawyer. The Sawyer repository is at
3// https://gitlab.com/charger7534/sawyer.git.
4
5
6
7
8#ifndef Sawyer_Time_H
9#define Sawyer_Time_H
10
11#include <Sawyer/Optional.h>
12#include <Sawyer/Result.h>
13
14#include <ctime>
15#include <ostream>
16
17namespace Sawyer {
18
20class Time {
21 Optional<unsigned> year_; // year, >= 1583
22 Optional<unsigned> month_; // month of year, 1 through 12, only if year_ is defined
23 Optional<unsigned> day_; // day of month, 1 through 31, only if month_ is defined
24
25 Optional<unsigned> hour_; // 0 through 23
26 Optional<unsigned> minute_; // 0 through 59, only if hour_ is defined
27 Optional<unsigned> second_; // 0 through 60 (for a leap second), only if minute_ is defined
28
29 Optional<int> tz_hour_; // -23 through +23
30 Optional<int> tz_minute_; // -59 through 59, only if tz_hour_ is defined and having the same sign
31
32public:
37
41 static Result<Time, std::string> parse(const std::string&);
42
46 static Time now();
47
51 bool isEmpty() const;
52
56 bool hasDate() const;
57
61 bool hasTime() const;
62
66 bool hasZone() const;
67
71 bool hasSpecificDate() const;
72
76 bool hasSpecificTime() const;
77
82 Time resolve(const Time&) const;
83
96
114
118 Time noDate() const;
119
123 Time noTime() const;
124
128 Time noZone() const;
129
133 const Optional<unsigned>& year() const;
134
138 const Optional<unsigned>& month() const;
139
144 const Optional<unsigned>& day() const;
145
149 const Optional<unsigned>& hour() const;
150
155
161
167
173
186
193 std::string toString() const;
194
200
210 bool operator==(const Time&) const;
211 bool operator!=(const Time&) const;
223 bool operator<(const Time&) const;
224
225private:
226 // Normalization functions
227 void normalizeSecond();
228 void normalizeMinute();
229 void normalizeHour();
230 void normalizeMonth();
231 void normalize();
232};
233
234std::ostream& operator<<(std::ostream&, const Time&);
235
236} // namespace
237#endif
Holds a value or nothing.
Definition Optional.h:56
Result containing a value or an error.
Definition Result.h:315
Represents an ISO 8601 time point.
Definition Time.h:20
const Optional< int > & timeZoneHour() const
Returns a timezone hour, if any.
bool hasDate() const
Test whether date is present.
bool hasTime() const
Test whether time is present.
bool hasZone() const
Test whether a timezone is present.
Time noZone() const
Removes the timezone portion of a time point.
const Optional< unsigned > & month() const
Returns the month, if any.
Time lowerBound() const
Returns the lower bound for the time.
const Optional< unsigned > & second() const
Returns the second, if any.
static Time now()
Current time.
const Optional< int > & timeZoneMinute() const
Returns a timezone minute, if any.
bool operator==(const Time &) const
Compare two times for equality or inequality.
Result< Time, std::string > toZulu() const
Convert to timezone +0000.
const Optional< unsigned > & day() const
Returns the day of the month, if any.
Result< time_t, std::string > toUnix() const
Convert the time point to a Unix system time.
Time noDate() const
Removes the date portion of a time point.
Time()
Construct an empty time point.
static Result< Time, std::string > parse(const std::string &)
Parse an ISO 8601 time string.
bool operator!=(const Time &) const
Compare two times for equality or inequality.
const Optional< unsigned > & minute() const
Returns the minute, if any.
bool isEmpty() const
Test whether this object is empty.
bool hasSpecificDate() const
Test whether a date is fully specified.
const Optional< unsigned > & year() const
Returns the year, if any.
Time resolve(const Time &) const
Fill in missing fields.
bool operator<(const Time &) const
Compare two times for less-than.
Time noTime() const
Removes the time portion of a time point.
std::string toString() const
Convert a time point to ISO 8601 format.
bool hasSpecificTime() const
Test whether a time is fully specified.
Result< Time, std::string > upperBound() const
Returns an upper bound for the time.
const Optional< unsigned > & hour() const
Returns the hour, if any.
Sawyer support library.