diff --git a/db/jsobj.cpp b/db/jsobj.cpp index 35192b2c42c..7d5a4ae14d8 100644 --- a/db/jsobj.cpp +++ b/db/jsobj.cpp @@ -1474,6 +1474,16 @@ namespace mongo { } } + time_t OID::asTimeT(){ + int time; + char* T = (char *) &time; + T[0] = data[3]; + T[1] = data[2]; + T[2] = data[1]; + T[3] = data[0]; + return time; + } + Labeler::Label GT( "$gt" ); Labeler::Label GTE( "$gte" ); Labeler::Label LT( "$lt" ); diff --git a/db/jsobj.h b/db/jsobj.h index 3b08d6f1f3e..002ae28975b 100644 --- a/db/jsobj.h +++ b/db/jsobj.h @@ -172,6 +172,9 @@ namespace mongo { /** Set to the hex string value specified. */ void init( string s ); + + time_t asTimeT(); + Date_t asDateT() { return asTimeT() * (long long)1000; } }; ostream& operator<<( ostream &s, const OID &o ); diff --git a/dbtests/jsobjtests.cpp b/dbtests/jsobjtests.cpp index be730e0adae..844b97af803 100644 --- a/dbtests/jsobjtests.cpp +++ b/dbtests/jsobjtests.cpp @@ -729,6 +729,30 @@ namespace JsobjTests { ASSERT( a.woCompare( c ) < 0 ); } }; + + class time { + public: + void run(){ + OID oid; + + { + time_t before = ::time(0); + oid.init(); + time_t after = ::time(0); + ASSERT( oid.asTimeT() >= before ); + ASSERT( oid.asTimeT() <= after ); + } + + { + Date_t before = jsTime(); + sleepsecs(1); + oid.init(); + Date_t after = jsTime(); + ASSERT( oid.asDateT() >= before ); + ASSERT( oid.asDateT() <= after ); + } + } + }; } // namespace OIDTests namespace ValueStreamTests { @@ -1497,6 +1521,7 @@ namespace JsobjTests { add< OIDTests::initParse1 >(); add< OIDTests::append >(); add< OIDTests::increasing >(); + add< OIDTests::time >(); add< ValueStreamTests::LabelBasic >(); add< ValueStreamTests::LabelShares >(); add< ValueStreamTests::LabelDouble >();