Commit 992f7257b50589d3cd6fc5b95448dd31d06c34e8
- Diff rendering mode:
- inline
- side by side
core/datetools.cpp
(24 / 3)
|   | |||
| 26 | 26 | ||
| 27 | 27 | namespace dss { | |
| 28 | 28 | ||
| 29 | static long int g_GMT_Offset = 0; | ||
| 30 | |||
| 29 | 31 | //================================================== DateTime | |
| 30 | 32 | ||
| 31 | 33 | DateTime::DateTime() { | |
| … | … | ||
| 298 | 298 | tm.tm_hour = hour; | |
| 299 | 299 | tm.tm_min = min; | |
| 300 | 300 | tm.tm_sec = sec; | |
| 301 | tm.tm_isdst = -1; | ||
| 301 | 302 | ||
| 302 | return DateTime::fromUTC(mktime(&tm)); | ||
| 303 | // if string ends with "Z" it is in UTC, otherwise it is considered to | ||
| 304 | // be in local time | ||
| 305 | if(_isoStr.at(_isoStr.size()-1) != 'Z') { | ||
| 306 | return DateTime::toUTC(mktime(&tm)); | ||
| 307 | } else { | ||
| 308 | return DateTime(tm); | ||
| 309 | } | ||
| 303 | 310 | } // fromISO | |
| 304 | 311 | ||
| 305 | 312 | DateTime DateTime::fromUTC(const time_t& _time) { | |
| 306 | return DateTime(_time - timezone); | ||
| 313 | return DateTime(_time - g_GMT_Offset); | ||
| 307 | 314 | } // fromUTC | |
| 308 | 315 | ||
| 309 | 316 | DateTime DateTime::toUTC(const time_t& _time) { | |
| 310 | return DateTime(_time + timezone); | ||
| 317 | return DateTime(_time + g_GMT_Offset); | ||
| 311 | 318 | } // toUTC | |
| 319 | |||
| 320 | DateTime DateTime::fromUTC() { | ||
| 321 | return DateTime(mktime(&m_DateTime) - g_GMT_Offset); | ||
| 322 | } | ||
| 323 | |||
| 324 | DateTime DateTime::toUTC() { | ||
| 325 | return DateTime(mktime(&m_DateTime) + g_GMT_Offset); | ||
| 326 | } | ||
| 327 | |||
| 328 | void DateTime::configureUTCOffset(long int _offset) { | ||
| 329 | g_GMT_Offset = _offset; | ||
| 330 | } | ||
| 312 | 331 | ||
| 313 | 332 | DateTime DateTime::NullDate(0); | |
| 314 | 333 |
core/datetools.h
(13 / 0)
|   | |||
| 171 | 171 | /** Creates an instance from a time_t struct and converts the internal | |
| 172 | 172 | * time to UTC */ | |
| 173 | 173 | static DateTime toUTC(const time_t& _time); | |
| 174 | |||
| 175 | /** Assumes current DateTime instance to be in UTC and creates an | ||
| 176 | * instance a DateTime instance that is in local time */ | ||
| 177 | DateTime fromUTC(); | ||
| 178 | |||
| 179 | /** Assumes current DateTime instance to be in local time and creates an | ||
| 180 | * instance a DateTime instance that is in UTC */ | ||
| 181 | DateTime toUTC(); | ||
| 182 | |||
| 183 | /** Configures the offset between UTC and local time which will be used | ||
| 184 | * for all subsequent calculations; the offset mst be in seconds West | ||
| 185 | * of GMT */ | ||
| 186 | static void configureUTCOffset(long int _offset); | ||
| 174 | 187 | }; // DateTime | |
| 175 | 188 | ||
| 176 | 189 | std::ostream& operator<<(std::ostream& out, const DateTime& _dt); |
main.cpp
(15 / 0)
|   | |||
| 32 | 32 | #include "core/logger.h" | |
| 33 | 33 | #include "core/ds485client.h" | |
| 34 | 34 | #include "core/ds485/ds485.h" | |
| 35 | #include "core/datetools.h" | ||
| 35 | 36 | #ifdef WITH_TESTS | |
| 36 | 37 | #include "tests/tests.h" | |
| 37 | 38 | #endif | |
| … | … | ||
| 70 | 70 | ||
| 71 | 71 | // make sure timezone gets set | |
| 72 | 72 | tzset(); | |
| 73 | |||
| 74 | long int time_offset = timezone; | ||
| 75 | |||
| 76 | #if defined(__linux__) | ||
| 77 | time_t now; | ||
| 78 | struct tm t; | ||
| 79 | time(&now); | ||
| 80 | localtime_r(&now, &t); | ||
| 81 | mktime(&t); | ||
| 82 | // gmtoff is east of UTC, however timezone is west of UTC | ||
| 83 | time_offset = t.tm_gmtoff * (-1); | ||
| 84 | #endif | ||
| 85 | |||
| 86 | dss::DateTime::configureUTCOffset(time_offset); | ||
| 73 | 87 | ||
| 74 | 88 | char* tzNameCopy = strdup("GMT"); | |
| 75 | 89 | tzname[0] = tzname[1] = tzNameCopy; |

