diff options
author | Derick Rethans <github@derickrethans.nl> | 2017-06-16 16:22:47 +0100 |
---|---|---|
committer | Derick Rethans <github@derickrethans.nl> | 2017-07-13 16:01:19 +0100 |
commit | a8b0a95eda28a6fbeb87fb347ec3449ce8eb3683 (patch) | |
tree | dfba4a34279409c19818e2b5b793cfe87aadfcd5 /src/mongo/db/query | |
parent | 943361fe17e0443d6b899ba10160fb1f68742f42 (diff) | |
download | mongo-a8b0a95eda28a6fbeb87fb347ec3449ce8eb3683.tar.gz |
SERVER-29208 Add the $dateFromString aggregation operator
Diffstat (limited to 'src/mongo/db/query')
-rw-r--r-- | src/mongo/db/query/datetime/date_time_support.cpp | 36 | ||||
-rw-r--r-- | src/mongo/db/query/datetime/date_time_support.h | 5 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/mongo/db/query/datetime/date_time_support.cpp b/src/mongo/db/query/datetime/date_time_support.cpp index 57dcd403458..71ce5033931 100644 --- a/src/mongo/db/query/datetime/date_time_support.cpp +++ b/src/mongo/db/query/datetime/date_time_support.cpp @@ -122,6 +122,42 @@ TimeZone TimeZoneDatabase::utcZone() { return TimeZone{nullptr}; } +static timelib_tzinfo* timezonedatabase_gettzinfowrapper(char* tz_id, + const _timelib_tzdb* db, + int* error) { + return nullptr; +} + +Date_t TimeZoneDatabase::fromString(StringData dateString) const { + std::unique_ptr<timelib_time, TimeZone::TimelibTimeDeleter> t( + timelib_strtotime(const_cast<char*>(dateString.toString().c_str()), + dateString.size(), + nullptr, + _timeZoneDatabase.get(), + timezonedatabase_gettzinfowrapper)); + + // If the time portion is fully missing, initialize to 0. This allows for the '%Y-%m-%d' format + // to be passed too, which is what the BI connector may request + if (t->h == TIMELIB_UNSET && t->i == TIMELIB_UNSET && t->s == TIMELIB_UNSET) { + t->h = t->i = t->s = t->f = 0; + } + + if (t->y == TIMELIB_UNSET || t->m == TIMELIB_UNSET || t->d == TIMELIB_UNSET || + t->h == TIMELIB_UNSET || t->i == TIMELIB_UNSET || t->s == TIMELIB_UNSET) { + uasserted(40545, + str::stream() + << "an incomplete date/time string has been found, with elements missing: \"" + << dateString + << "\""); + } + + timelib_update_ts(t.get(), nullptr); + timelib_unixtime2local(t.get(), t->sse); + + return Date_t::fromMillisSinceEpoch((static_cast<double>(t->sse) + static_cast<double>(t->f)) * + 1000); +} + TimeZone TimeZoneDatabase::getTimeZone(StringData timeZoneId) const { auto tz = _timeZones.find(timeZoneId); if (tz != _timeZones.end()) { diff --git a/src/mongo/db/query/datetime/date_time_support.h b/src/mongo/db/query/datetime/date_time_support.h index 3f2768d0616..7f2d891138d 100644 --- a/src/mongo/db/query/datetime/date_time_support.h +++ b/src/mongo/db/query/datetime/date_time_support.h @@ -319,6 +319,11 @@ public: std::unique_ptr<TimeZoneDatabase> timeZoneDatabase); /** + * Use the timezone database to create a Date_t from a string. + */ + Date_t fromString(StringData dateString) const; + + /** * Returns a TimeZone object representing the UTC time zone. */ static TimeZone utcZone(); |