def test_from_datetime(self): d = datetime.datetime.utcnow() d = d - datetime.timedelta(microseconds=d.microsecond) oid = ObjectId.from_datetime(d) self.assertEqual(d, oid.generation_time.replace(tzinfo=None)) self.assertEqual("0" * 16, str(oid)[8:]) aware = datetime.datetime(1993, 4, 4, 2, tzinfo=FixedOffset(555, "SomeZone")) as_utc = (aware - aware.utcoffset()).replace(tzinfo=utc) oid = ObjectId.from_datetime(aware) self.assertEqual(as_utc, oid.generation_time)
def query(): start = int(request.args.get('pms_js_time')) /1000 start = datetime.datetime.utcfromtimestamp(start) #start -= datetime.timedelta(hours=4) end = start + datetime.timedelta(hours=1) start_id = ObjectId.from_datetime(start) end_id = ObjectId.from_datetime(end) args = flatten(request.args) del args['pms_js_time'] query = { '_id': { '$gte': ObjectId.from_datetime(start), '$lte': ObjectId.from_datetime(end), } } query.update(args) l = [] for event in events.find(query).sort('_id'): wrap_event(event) l.append(event) return jsonify(events=l)
from pymongo.objectid import ObjectId import datetime obj = get_instance() c = obj.create_cursor("tuokouerchu") c.drop_collection("emotion") import random k = 0 for i in ["高兴","郁闷","伤心","沮丧","饿","宅","无聊"]: obj = get_instance() c = obj.create_cursor("tuokouerchu") row = c.emotion.insert({"id":k,"name":random.choice(range(1,100))}) k+=1 timebinray = ObjectId.from_datetime( datetime.datetime.now() ) for row in c.emotion.find(): obj = get_instance() obj.create_cursor("tuokouerchu") print row obj.auto_incr_id("emotion",row['_id']) del obj for row in c.emotion.find(): print row
def test_from_datetime(self): d = datetime.datetime.utcnow() d = d - datetime.timedelta(microseconds=d.microsecond) oid = ObjectId.from_datetime(d) self.assertEqual(d, oid.generation_time) self.assertEqual("0" * 16, str(oid)[8:])