[SERVER-350] Append a date as std::time_t to BSONObj from a stream Created: 09/Oct/09  Updated: 12/Jul/16  Resolved: 15/Oct/09

Status: Closed
Project: Core Server
Component/s: Internal Client, Usability
Affects Version/s: None
Fix Version/s: 1.1.2

Type: New Feature Priority: Trivial - P5
Reporter: metagoto Assignee: Mathias Stearn
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

A small enhancement just to be able to do:

BSONObj o = BSON("date" << std::time(0));

Currently, we have to manipulate BSONObjBuilder::appendDate() in order to append a date from a std::time_t.
There is no append() overload which takes an unsigned long long (in the case we would like to cast std::time_t)

diff --git a/db/jsobj.h b/db/jsobj.h
index 3afc916..8783f5e 100644
— a/db/jsobj.h
+++ b/db/jsobj.h
@@ -1167,6 +1167,13 @@ namespace mongo

{ b.append(n); return *this; }

+
+ /** Append a date from a std::time_t */
+ void append(const char *fieldName, std::time_t dt)

{ + b.append((char) Date); + b.append(fieldName); + b.append(dt*1000); + }

/** Append a BSON Object ID (OID type). */
void appendOID(const char *fieldName, OID *oid = 0 , bool generateIfBlank = false ) {



 Comments   
Comment by Eliot Horowitz (Inactive) [ 16/Oct/09 ]

release 10/16/2009

Comment by metagoto [ 16/Oct/09 ]

1), 2) Yes, you're right.

DATENOW is OK for me. A 'now date' from a stream is very useful. Thanks.
Specific dates still can be appended with BSONObjBuilder::appendDate() so TIMET() and DATE() are fine but not top priority as far as I'm concerned.

Comment by Mathias Stearn [ 16/Oct/09 ]

two problems with that:
1) It would force users to use 32 bit time_t
2) It still wouldn't make time_t a distinct type for overloading purposes

I forgot to mention that we added a DATENOW constant (non-macro) so you can do:
BSONObj o = BSON("created" << DATENOW);

if you really need to specify specific dates in the BSON macro I can add something like this:
BSON("somedate" << TIMET(sec_since_epoch))
BSON("somedate" << DATE(milisecs_since_epoch))

Comment by metagoto [ 16/Oct/09 ]

Did you consider _USE_32BIT_TIME_T ?
http://msdn.microsoft.com/en-us/library/323b6b3k.aspx
Sorry, I can't test on a Windows machine currently

Comment by Mathias Stearn [ 15/Oct/09 ]

Had to add a new appendTimeT() method because MSVC doesn't consider time_t to be distinct from __int64.

Generated at Thu Feb 08 02:53:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.