From f84a1f2429320be49d1a9f2ead3097074523863b Mon Sep 17 00:00:00 2001 From: Mathias Stearn Date: Thu, 15 Oct 2009 13:59:30 -0400 Subject: [PATCH] support appending time_t to BSONObjBuilder --- db/jsobj.h | 9 +++++++++ dbtests/jsobjtests.cpp | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/db/jsobj.h b/db/jsobj.h index 4861980968b..33ea7891e16 100644 --- a/db/jsobj.h +++ b/db/jsobj.h @@ -1196,6 +1196,15 @@ namespace mongo { void append( const char *fieldName, OID oid ) { appendOID( fieldName, &oid ); } + /** Append a time_t date. + @param dt a C-style 32 bit date value, that is + the number of seconds since January 1, 1970, 00:00:00 GMT + */ + void append(const char *fieldName, time_t dt) { + b.append((char) Date); + b.append(fieldName); + b.append(static_cast(dt) * 1000); + } /** Append a date. @param dt a Java-style 64 bit date value, that is the number of milliseconds since January 1, 1970, 00:00:00 GMT diff --git a/dbtests/jsobjtests.cpp b/dbtests/jsobjtests.cpp index b91078be155..ccac294865f 100644 --- a/dbtests/jsobjtests.cpp +++ b/dbtests/jsobjtests.cpp @@ -724,6 +724,24 @@ namespace JsobjTests { } }; + class TimeTBuilder { + public: + void run() { + unsigned long long before = jsTime(); + time_t now = time(NULL); + BSONObj o = BSON("now" << now); + unsigned long long after = jsTime(); + + + ASSERT( o.valid() ); + + BSONElement e = o["now"]; + ASSERT( e.type() == Date ); + ASSERT( e.date()/1000 >= before/1000 ); + ASSERT( e.date()/1000 <= after/1000 ); + } + }; + class MinMaxElementTest { public: @@ -1095,6 +1113,7 @@ namespace JsobjTests { add< ValueStreamTests::ElementAppend >(); add< SubObjectBuilder >(); add< DateNowBuilder >(); + add< TimeTBuilder >(); add< MinMaxElementTest >(); add< ComparatorTest >(); add< ExtractFieldsTest >();