src/utilities/time/DateTime.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #ifndef UTILITIES_TIME_DATETIME_HPP | ||
| 7 | #define UTILITIES_TIME_DATETIME_HPP | ||
| 8 | |||
| 9 | #include "../UtilitiesAPI.hpp" | ||
| 10 | |||
| 11 | #include "Date.hpp" | ||
| 12 | #include "Time.hpp" | ||
| 13 | #include "../core/Macro.hpp" | ||
| 14 | #include "../core/Logger.hpp" | ||
| 15 | |||
| 16 | #include <vector> | ||
| 17 | |||
| 18 | #include <boost/date_time/gregorian/greg_date.hpp> | ||
| 19 | |||
| 20 | #include <ctime> | ||
| 21 | |||
| 22 | namespace openstudio { | ||
| 23 | |||
| 24 | /// DateTime is an absolute unit of time, resolution to the second | ||
| 25 | /// date is a valid Date | ||
| 26 | /// time is normalized to 0 <= time < 24 hrs | ||
| 27 | class UTILITIES_API DateTime | ||
| 28 | { | ||
| 29 | public: | ||
| 30 | − | REGISTER_LOGGER("utilities.time.DateTime"); | |
| 31 | |||
| 32 | /// get the local time now | ||
| 33 | static DateTime now(); | ||
| 34 | |||
| 35 | /// get the UTC time now | ||
| 36 | static DateTime nowUTC(); | ||
| 37 | |||
| 38 | /// get the current UTC offset | ||
| 39 | static double localOffsetUTC(); | ||
| 40 | |||
| 41 | /// default constructor, UTC time is assumed | ||
| 42 | DateTime(); | ||
| 43 | |||
| 44 | /// constructor from Date, UTC time is assumed | ||
| 45 | DateTime(const Date& date); | ||
| 46 | |||
| 47 | /// constructor from Date and Time, UTC time is assumed | ||
| 48 | DateTime(const Date& date, const Time& timeFromDate); | ||
| 49 | |||
| 50 | /// constructor from Date and Time and UTC Offset | ||
| 51 | DateTime(const Date& date, const Time& timeFromDate, double utcOffset); | ||
| 52 | |||
| 53 | /// copy constructor | ||
| 54 | DateTime(const DateTime& other); | ||
| 55 | |||
| 56 | /// constructor from simple string format, UTC time is assumed | ||
| 57 | DateTime(const std::string& string); | ||
| 58 | |||
| 59 | /// from system tm struct | ||
| 60 | DateTime(tm t_tm); | ||
| 61 | |||
| 62 | /// assignment operator | ||
| 63 | DateTime& operator=(const DateTime& other); | ||
| 64 | |||
| 65 | /// addition operator | ||
| 66 | DateTime operator+(const Time& time) const; | ||
| 67 | |||
| 68 | /// assignment by addition operator | ||
| 69 | DateTime& operator+=(const Time& time); | ||
| 70 | |||
| 71 | /// difference operator | ||
| 72 | DateTime operator-(const Time& time) const; | ||
| 73 | |||
| 74 | /// assignment by difference operator | ||
| 75 | DateTime& operator-=(const Time& time); | ||
| 76 | |||
| 77 | /// time duration | ||
| 78 | Time operator-(const DateTime& dateTime) const; | ||
| 79 | |||
| 80 | /// time duration | ||
| 81 | Time operator-(const Date& date) const; | ||
| 82 | |||
| 83 | /// equality operator | ||
| 84 | bool operator==(const DateTime& other) const; | ||
| 85 | |||
| 86 | /// non-equality operator | ||
| 87 | bool operator!=(const DateTime& other) const; | ||
| 88 | |||
| 89 | /// less than operator | ||
| 90 | bool operator<(const DateTime& rhs) const; | ||
| 91 | |||
| 92 | /// less than equals operator | ||
| 93 | bool operator<=(const DateTime& rhs) const; | ||
| 94 | |||
| 95 | /// greater than operator | ||
| 96 | bool operator>(const DateTime& rhs) const; | ||
| 97 | |||
| 98 | /// greater than equals operator | ||
| 99 | bool operator>=(const DateTime& rhs) const; | ||
| 100 | |||
| 101 | /// getter to date member | ||
| 102 | Date date() const; | ||
| 103 | |||
| 104 | /// getter to time member | ||
| 105 | Time time() const; | ||
| 106 | |||
| 107 | /// UTC offset in hours, local time = UTC time + UTC offset | ||
| 108 | double utcOffset() const; | ||
| 109 | |||
| 110 | /// convert to simple string format in UTC | ||
| 111 | std::string toString() const; | ||
| 112 | |||
| 113 | /// convert to ISO 8601 string | ||
| 114 | std::string toISO8601() const; | ||
| 115 | |||
| 116 | /// convert to xsd:dateTime format | ||
| 117 | std::string toXsdDateTime() const; | ||
| 118 | |||
| 119 | /// convert to epoch time_t in UTC time | ||
| 120 | time_t toEpoch() const; | ||
| 121 | |||
| 122 | /// construct from ISO 8601 string | ||
| 123 | static boost::optional<DateTime> fromISO8601(const std::string& str); | ||
| 124 | |||
| 125 | /// construct from xsd:dateTime string | ||
| 126 | static boost::optional<DateTime> fromXsdDateTime(const std::string& str); | ||
| 127 | |||
| 128 | /// construct from epoch time_t in UTC | ||
| 129 | static DateTime fromEpoch(const std::time_t& time); | ||
| 130 | |||
| 131 | private: | ||
| 132 | // ensure that stored time is less than 24 hrs and adjust date accordingly | ||
| 133 | void normalize(); | ||
| 134 | |||
| 135 | int utcOffsetHours() const; | ||
| 136 | int utcOffsetMinutes() const; | ||
| 137 | |||
| 138 | Date m_date; | ||
| 139 | Time m_time; | ||
| 140 | |||
| 141 | // utc offset in hours, some places have half hour offsets | ||
| 142 | double m_utcOffset; | ||
| 143 | }; | ||
| 144 | |||
| 145 | /// optional DateTime | ||
| 146 | using OptionalDateTime = boost::optional<DateTime>; | ||
| 147 | |||
| 148 | /// vector of DateTime | ||
| 149 | using DateTimeVector = std::vector<DateTime>; | ||
| 150 | |||
| 151 | // std::ostream operator<< | ||
| 152 | UTILITIES_API std::ostream& operator<<(std::ostream& os, const DateTime& dateTime); | ||
| 153 | |||
| 154 | } // namespace openstudio | ||
| 155 | |||
| 156 | #endif // UTILITIES_TIME_DATETIME_HPP | ||
| 157 |