src/utilities/time/Time.cpp
| 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 | #include "Time.hpp" | ||
| 7 | |||
| 8 | using namespace std; | ||
| 9 | using namespace boost; | ||
| 10 | using namespace posix_time; | ||
| 11 | |||
| 12 | namespace openstudio { | ||
| 13 | |||
| 14 | /// round towards zero, need to move this | ||
| 15 | 11738036 | int floor0(double value) { | |
| 16 | 11738036 | int result(0); | |
| 17 |
2/2✓ Branch 0 taken 617 times.
✓ Branch 1 taken 11737419 times.
|
11738036 | if (value < 0.0) { |
| 18 | 617 | result = static_cast<int>(std::ceil(value)); | |
| 19 | } else { | ||
| 20 | 11737419 | result = static_cast<int>(std::floor(value)); | |
| 21 | }; | ||
| 22 | 11738036 | return result; | |
| 23 | } | ||
| 24 | |||
| 25 | /// get current local time of day | ||
| 26 | 618 | Time Time::currentTime() { | |
| 27 |
1/1✓ Branch 1 taken 618 times.
|
618 | posix_time::ptime now = posix_time::second_clock::local_time(); |
| 28 |
2/2✓ Branch 1 taken 618 times.
✓ Branch 4 taken 618 times.
|
618 | return Time(now.time_of_day()); |
| 29 | } | ||
| 30 | |||
| 31 | /// get current UTC time of day | ||
| 32 | ✗ | Time Time::currentTimeUTC() { | |
| 33 | ✗ | posix_time::ptime now = posix_time::second_clock::universal_time(); | |
| 34 | ✗ | return Time(now.time_of_day()); | |
| 35 | } | ||
| 36 | |||
| 37 | /// Default constructor | ||
| 38 | 290771 | Time::Time() : m_impl(0, 0, 0, 0) {} | |
| 39 | |||
| 40 | /// Time from number of days, fractional values ok | ||
| 41 | 3273355 | Time::Time(double fracDays) { | |
| 42 | 3273355 | double fracHours = HOURS_PER_DAY * fracDays; | |
| 43 | 3273355 | int hours = floor0(fracHours); | |
| 44 | |||
| 45 | 3273355 | double fracMinutes = MINUTES_PER_HOUR * (fracHours - hours); | |
| 46 | 3273355 | int minutes = floor0(fracMinutes); | |
| 47 | |||
| 48 | 3273355 | double fracSeconds = SECONDS_PER_MINUTE * (fracMinutes - minutes); | |
| 49 | 3273355 | int seconds = floor0(fracSeconds); | |
| 50 | |||
| 51 |
1/1✓ Branch 1 taken 3273355 times.
|
3273355 | m_impl = ImplType(hours, minutes, seconds, 0); |
| 52 | 3273355 | } | |
| 53 | |||
| 54 | /// Time from days, hours, minutes, seconds | ||
| 55 | 1059711 | Time::Time(int days, int hours, int minutes, int seconds) { | |
| 56 | 1059711 | hours += HOURS_PER_DAY * days; | |
| 57 |
5/6✓ Branch 0 taken 1059709 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1059708 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1059708 times.
✗ Branch 5 not taken.
|
1059711 | if ((hours * minutes >= 0) && (hours * seconds >= 0) && (minutes * seconds >= 0)) { |
| 58 | // same sign, carry on | ||
| 59 |
1/1✓ Branch 1 taken 1059708 times.
|
1059708 | m_impl = ImplType(hours, minutes, seconds, 0); |
| 60 | } else { | ||
| 61 | // mixed sign | ||
| 62 |
1/1✓ Branch 4 taken 3 times.
|
3 | ImplType negativeDuration(std::min(hours, 0), std::min(minutes, 0), std::min(seconds, 0), 0); |
| 63 |
1/1✓ Branch 4 taken 3 times.
|
3 | m_impl = ImplType(std::max(hours, 0), std::max(minutes, 0), std::max(seconds, 0), 0); |
| 64 |
1/1✓ Branch 1 taken 3 times.
|
3 | m_impl += negativeDuration; |
| 65 | } | ||
| 66 | 1059711 | } | |
| 67 | |||
| 68 | 345 | Time::Time(const std::string& string) { | |
| 69 |
1/1✓ Branch 1 taken 345 times.
|
345 | boost::posix_time::time_duration td = boost::posix_time::duration_from_string(string); |
| 70 | |||
| 71 | 345 | m_impl = boost::posix_time::time_duration(td); | |
| 72 | 345 | } | |
| 73 | |||
| 74 | 2 | Time::Time(tm t_tm) : m_impl(t_tm.tm_hour, t_tm.tm_min, t_tm.tm_sec) {} | |
| 75 | |||
| 76 | /// copy constructor | ||
| 77 | 12230117 | Time::Time(const Time& other) : m_impl(other.impl()) {} | |
| 78 | |||
| 79 | /// Time from impl | ||
| 80 | 17676 | Time::Time(const ImplType& implType) : m_impl(implType) {} | |
| 81 | |||
| 82 | /// assignment operator | ||
| 83 | 1131269 | Time& Time::operator=(const Time& other) { | |
| 84 | 1131269 | m_impl = other.impl(); | |
| 85 | 1131269 | return *this; | |
| 86 | } | ||
| 87 | |||
| 88 | /// addition operator | ||
| 89 | 2687994 | Time Time::operator+(const Time& time) const { | |
| 90 | 2687994 | Time result(*this); | |
| 91 | 2687994 | result += time; | |
| 92 | 2687994 | return result; | |
| 93 | } | ||
| 94 | |||
| 95 | /// assignment by addition operator | ||
| 96 | 3045553 | Time& Time::operator+=(const Time& time) { | |
| 97 |
2/2✓ Branch 1 taken 3045553 times.
✓ Branch 4 taken 3045553 times.
|
3045553 | m_impl += time.impl(); |
| 98 | 3045553 | return *this; | |
| 99 | } | ||
| 100 | |||
| 101 | ✗ | std::string Time::toString() const { | |
| 102 | ✗ | return boost::posix_time::to_simple_string(m_impl); | |
| 103 | } | ||
| 104 | |||
| 105 | /// difference operator | ||
| 106 | 1344281 | Time Time::operator-(const Time& time) const { | |
| 107 | 1344281 | Time result(*this); | |
| 108 | 1344281 | result -= time; | |
| 109 | 1344281 | return result; | |
| 110 | } | ||
| 111 | |||
| 112 | /// assignment by difference operator | ||
| 113 | 1657383 | Time& Time::operator-=(const Time& time) { | |
| 114 |
2/2✓ Branch 1 taken 1657383 times.
✓ Branch 4 taken 1657383 times.
|
1657383 | m_impl -= time.impl(); |
| 115 | 1657383 | return *this; | |
| 116 | } | ||
| 117 | |||
| 118 | /// multiplication operator | ||
| 119 | 21818 | Time Time::operator*(double mult) const { | |
| 120 | 21818 | return {this->totalDays() * mult}; | |
| 121 | } | ||
| 122 | |||
| 123 | /// division operator | ||
| 124 | ✗ | Time Time::operator/(double div) const { | |
| 125 | ✗ | return {this->totalDays() / div}; | |
| 126 | } | ||
| 127 | |||
| 128 | /// equality operator | ||
| 129 | 318 | bool Time::operator==(const Time& other) const { | |
| 130 | 318 | return totalSeconds() == other.totalSeconds(); | |
| 131 | } | ||
| 132 | |||
| 133 | /// non-equality operator | ||
| 134 | ✗ | bool Time::operator!=(const Time& other) const { | |
| 135 | ✗ | return totalSeconds() != other.totalSeconds(); | |
| 136 | } | ||
| 137 | |||
| 138 | /// less than operator | ||
| 139 | 919368 | bool Time::operator<(const Time& rhs) const { | |
| 140 | 919368 | return totalSeconds() < rhs.totalSeconds(); | |
| 141 | } | ||
| 142 | |||
| 143 | /// less than equals operator | ||
| 144 | ✗ | bool Time::operator<=(const Time& rhs) const { | |
| 145 | ✗ | return totalSeconds() <= rhs.totalSeconds(); | |
| 146 | } | ||
| 147 | |||
| 148 | /// greater than operator | ||
| 149 | ✗ | bool Time::operator>(const Time& rhs) const { | |
| 150 | ✗ | return totalSeconds() > rhs.totalSeconds(); | |
| 151 | } | ||
| 152 | |||
| 153 | /// greater than equals operator | ||
| 154 | ✗ | bool Time::operator>=(const Time& rhs) const { | |
| 155 | ✗ | return totalSeconds() >= rhs.totalSeconds(); | |
| 156 | } | ||
| 157 | |||
| 158 | /// whole number of days | ||
| 159 | 1917971 | int Time::days() const { | |
| 160 | 1917971 | return floor0(totalDays()); | |
| 161 | } | ||
| 162 | |||
| 163 | /// whole number of hours remaining after days | ||
| 164 | 224897 | int Time::hours() const { | |
| 165 | 224897 | return (m_impl.hours() % 24); | |
| 166 | } | ||
| 167 | |||
| 168 | /// whole number of minutes remaining after hours | ||
| 169 | 224894 | int Time::minutes() const { | |
| 170 | 224894 | return (int)m_impl.minutes(); | |
| 171 | } | ||
| 172 | |||
| 173 | /// whole number of seconds remaining after minutes | ||
| 174 | 75395 | int Time::seconds() const { | |
| 175 | 75395 | return (int)m_impl.seconds(); | |
| 176 | } | ||
| 177 | |||
| 178 | /// entire time in days | ||
| 179 | 4349097 | double Time::totalDays() const { | |
| 180 | 4349097 | return DAYS_PER_SECOND * totalSeconds(); | |
| 181 | } | ||
| 182 | |||
| 183 | /// entire time in hours | ||
| 184 | 20 | double Time::totalHours() const { | |
| 185 | 20 | return HOURS_PER_SECOND * totalSeconds(); | |
| 186 | } | ||
| 187 | |||
| 188 | /// entire time in minutes | ||
| 189 | 228369 | double Time::totalMinutes() const { | |
| 190 | 228369 | return MINUTES_PER_SECOND * totalSeconds(); | |
| 191 | } | ||
| 192 | |||
| 193 | /// entire time in seconds | ||
| 194 | 7977796 | int Time::totalSeconds() const { | |
| 195 | 7977796 | return (int)m_impl.total_seconds(); | |
| 196 | } | ||
| 197 | |||
| 198 | // reference to impl | ||
| 199 | 24290163 | const Time::ImplType Time::impl() const { | |
| 200 | 24290163 | return m_impl; | |
| 201 | } | ||
| 202 | |||
| 203 | // std::ostream operator<< | ||
| 204 | 370 | std::ostream& operator<<(std::ostream& os, const Time& time) { | |
| 205 | // DLM@2009_07_09: os << time.impl(); does not work on windows as strftime limits | ||
| 206 | // hours from 0 to 23. | ||
| 207 | |||
| 208 |
3/3✓ Branch 1 taken 370 times.
✓ Branch 4 taken 370 times.
✓ Branch 7 taken 370 times.
|
370 | os << boost::posix_time::to_simple_string(time.impl()); |
| 209 | 370 | return os; | |
| 210 | } | ||
| 211 | |||
| 212 | // compare two Dates | ||
| 213 | ✗ | bool TimeCompare::operator()(const Time& lhs, const Time& rhs) const { | |
| 214 | ✗ | return lhs < rhs; | |
| 215 | } | ||
| 216 | |||
| 217 | } // namespace openstudio | ||
| 218 |