Commit 992f7257b50589d3cd6fc5b95448dd31d06c34e8

  • avatar
  • Sergey 'Jin' Bostandzhyan <jin @deve…per.digitalstrom.org>
  • Sun Aug 08 16:05:48 CEST 2010
Localtime <-> UTC setup

Added a way to remember the local time offsets before we switch to the
UTC timezone. This will allow us to correctly convert UTC to local time
and also to convert local time input to UTC.

A possibility to solve this would be to write an own singleton class that would
get initialized with the correct offset in the main function, however I
considered this an overhead and went for an static function in the DateTime
class which initializes a static variable.

Note, that on non linux system the conversion will not account for
daylight savings and might be off (this is how the existing
implementation was handling it).

Added code for linux systems, that will use tm_gmtoff which does deliver
the correct offset accounting for daylight savings and thus ensures taht
the conversion is correct.
core/datetools.cpp
(24 / 3)
  
2626
2727namespace dss {
2828
29 static long int g_GMT_Offset = 0;
30
2931 //================================================== DateTime
3032
3133 DateTime::DateTime() {
298298 tm.tm_hour = hour;
299299 tm.tm_min = min;
300300 tm.tm_sec = sec;
301 tm.tm_isdst = -1;
301302
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 }
303310 } // fromISO
304311
305312 DateTime DateTime::fromUTC(const time_t& _time) {
306 return DateTime(_time - timezone);
313 return DateTime(_time - g_GMT_Offset);
307314 } // fromUTC
308315
309316 DateTime DateTime::toUTC(const time_t& _time) {
310 return DateTime(_time + timezone);
317 return DateTime(_time + g_GMT_Offset);
311318 } // 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 }
312331
313332 DateTime DateTime::NullDate(0);
314333
core/datetools.h
(13 / 0)
  
171171 /** Creates an instance from a time_t struct and converts the internal
172172 * time to UTC */
173173 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);
174187 }; // DateTime
175188
176189 std::ostream& operator<<(std::ostream& out, const DateTime& _dt);
main.cpp
(15 / 0)
  
3232#include "core/logger.h"
3333#include "core/ds485client.h"
3434#include "core/ds485/ds485.h"
35#include "core/datetools.h"
3536#ifdef WITH_TESTS
3637#include "tests/tests.h"
3738#endif
7070
7171 // make sure timezone gets set
7272 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);
7387
7488 char* tzNameCopy = strdup("GMT");
7589 tzname[0] = tzname[1] = tzNameCopy;