diff options
author | Derick Rethans <github@derickrethans.nl> | 2017-07-28 13:05:32 +0100 |
---|---|---|
committer | Derick Rethans <github@derickrethans.nl> | 2017-07-30 15:40:46 +0100 |
commit | dcb9dbffa65d8ddcf5be80e88868fd11c01397df (patch) | |
tree | 158ea4e5f484f303976f31cdfa82477b17381ffd /src/mongo/db/query/datetime | |
parent | a402b45dfb18951a2c0de17d52e2f8dabc01b5d2 (diff) | |
download | mongo-dcb9dbffa65d8ddcf5be80e88868fd11c01397df.tar.gz |
SERVER-29284 Add support for UTC offsets to $dateFromString
Diffstat (limited to 'src/mongo/db/query/datetime')
-rw-r--r-- | src/mongo/db/query/datetime/date_time_support.cpp | 22 | ||||
-rw-r--r-- | src/mongo/db/query/datetime/date_time_support.h | 28 |
2 files changed, 28 insertions, 22 deletions
diff --git a/src/mongo/db/query/datetime/date_time_support.cpp b/src/mongo/db/query/datetime/date_time_support.cpp index 1ab22b6bc09..c7719014d9d 100644 --- a/src/mongo/db/query/datetime/date_time_support.cpp +++ b/src/mongo/db/query/datetime/date_time_support.cpp @@ -219,15 +219,9 @@ Date_t TimeZoneDatabase::fromString(StringData dateString, boost::optional<TimeZ "at the same time"); break; } - - // _tzInfo, although private, can be accessed because TimeZoneDatabase is a friend of - // TimeZone. - timelib_update_ts(parsedTime.get(), tz->_tzInfo.get()); - } else { - timelib_update_ts(parsedTime.get(), nullptr); } - timelib_unixtime2local(parsedTime.get(), parsedTime->sse); + tz->adjustTimeZone(parsedTime.get()); return Date_t::fromMillisSinceEpoch( durationCount<Milliseconds>(Seconds(parsedTime->sse) + Microseconds(parsedTime->us))); @@ -288,14 +282,14 @@ TimeZone TimeZoneDatabase::getTimeZone(StringData timeZoneId) const { str::stream() << "unrecognized time zone identifier: \"" << timeZoneId << "\""); } -void TimeZone::adjustTimeZone(timelib_time* t) const { - if (_tzInfo) { - timelib_set_timezone(t, _tzInfo.get()); - } else if (durationCount<Seconds>(_utcOffset)) { - timelib_set_timezone_from_offset(t, -durationCount<Seconds>(_utcOffset)); +void TimeZone::adjustTimeZone(timelib_time* timelibTime) const { + if (isTimeZoneIDZone()) { + timelib_set_timezone(timelibTime, _tzInfo.get()); + } else if (isUtcOffsetZone()) { + timelib_set_timezone_from_offset(timelibTime, -durationCount<Seconds>(_utcOffset)); } - timelib_update_ts(t, nullptr); - timelib_update_from_sse(t); + timelib_update_ts(timelibTime, nullptr); + timelib_update_from_sse(timelibTime); } Date_t TimeZone::createFromDateParts( diff --git a/src/mongo/db/query/datetime/date_time_support.h b/src/mongo/db/query/datetime/date_time_support.h index 9c85a3d6f6c..ddfd32853bc 100644 --- a/src/mongo/db/query/datetime/date_time_support.h +++ b/src/mongo/db/query/datetime/date_time_support.h @@ -49,7 +49,6 @@ namespace mongo { * for the hour, minute, or second of a date, even when given the same date. */ class TimeZone { - friend class TimeZoneDatabase; public: /** @@ -130,7 +129,21 @@ public: * Returns whether this is the zone representing UTC. */ bool isUtcZone() const { - return _tzInfo == nullptr; + return (_tzInfo == nullptr && !durationCount<Seconds>(_utcOffset)); + } + + /** + * Returns whether this is a zone representing a UTC offset, like "+04:00". + */ + bool isUtcOffsetZone() const { + return durationCount<Seconds>(_utcOffset) != 0; + } + + /** + * Returns whether this is a zone representing an Olson time zone, like "Europe/London". + */ + bool isTimeZoneIDZone() const { + return _tzInfo != nullptr; } /** @@ -166,6 +179,11 @@ public: Seconds utcOffset(Date_t) const; /** + * Adjusts 'timelibTime' according to this time zone definition. + */ + void adjustTimeZone(timelib_time* timelibTime) const; + + /** * Converts a date object to a string according to 'format'. 'format' can be any string literal, * containing 0 or more format specifiers like %Y (year) or %d (day of month). Callers must pass * a valid format string for 'format', i.e. one that has already been passed to @@ -297,12 +315,6 @@ private: void operator()(timelib_tzinfo* tzInfo); }; - /** - * Helper function to apply tzInfo and utcOffset information to the constructed timelib_time* - * value. - */ - void adjustTimeZone(timelib_time* t) const; - // null if this TimeZone represents the default UTC time zone, or a UTC-offset time zone std::shared_ptr<timelib_tzinfo> _tzInfo; |