ROSE  0.11.109.0
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://github.com/matzke1/sawyer.
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 
17 namespace Sawyer {
18 
20 class 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 
32 public:
36  Time();
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 
95  Time lowerBound() const;
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 
154  const Optional<unsigned>& minute() const;
155 
160  const Optional<unsigned>& second() const;
161 
166  const Optional<int>& timeZoneHour() const;
167 
172  const Optional<int>& timeZoneMinute() const;
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 
225 private:
226  // Normalization functions
227  void normalizeSecond();
228  void normalizeMinute();
229  void normalizeHour();
230  void normalizeMonth();
231  void normalize();
232 };
233 
234 std::ostream& operator<<(std::ostream&, const Time&);
235 
236 } // namespace
237 #endif
const Optional< unsigned > & hour() const
Returns the hour, if any.
Represents an ISO 8601 time point.
Definition: Time.h:20
bool operator<(const Time &) const
Compare two times for less-than.
Result containing a value or an error.
Definition: Result.h:270
Time noTime() const
Removes the time portion of a time point.
Time()
Construct an empty time point.
const Optional< unsigned > & minute() const
Returns the minute, if any.
bool hasTime() const
Test whether time is present.
const Optional< unsigned > & month() const
Returns the month, if any.
Name space for the entire library.
Definition: FeasiblePath.h:767
bool hasSpecificTime() const
Test whether a time is fully specified.
bool operator!=(const Time &) const
Compare two times for equality or inequality.
const Optional< int > & timeZoneMinute() const
Returns a timezone minute, if any.
Result< time_t, std::string > toUnix() const
Convert the time point to a Unix system time.
bool hasSpecificDate() const
Test whether a date is fully specified.
std::string toString() const
Convert a time point to ISO 8601 format.
static Result< Time, std::string > parse(const std::string &)
Parse an ISO 8601 time string.
bool hasZone() const
Test whether a timezone is present.
const Optional< unsigned > & year() const
Returns the year, if any.
Result< Time, std::string > upperBound() const
Returns an upper bound for the time.
Time noZone() const
Removes the timezone portion of a time point.
const Optional< unsigned > & day() const
Returns the day of the month, if any.
Time resolve(const Time &) const
Fill in missing fields.
Time lowerBound() const
Returns the lower bound for the time.
bool operator==(const Time &) const
Compare two times for equality or inequality.
bool isEmpty() const
Test whether this object is empty.
Result< Time, std::string > toZulu() const
Convert to timezone +0000.
bool hasDate() const
Test whether date is present.
const Optional< unsigned > & second() const
Returns the second, if any.
Time noDate() const
Removes the date portion of a time point.
const Optional< int > & timeZoneHour() const
Returns a timezone hour, if any.
static Time now()
Current time.