python操作mongodb之七时间和时区
#时间和时区 import datetime db.test.insert_one({"datetime-datetime-utcnow":datetime.datetime.utcnow()}) db.objects.insert_one({"datetime-datetime-now": datetime.datetime.now()}) #{ "_id" : ObjectId("576ccfad6a72e6042524f646"), "datetime-datetime-now" : ISODate("2016-06-24T14:14:05.575Z") } #{ "_id" : ObjectId("576ccfcd6a72e6042524f647"), "datetime-datetime-utcnow" : ISODate("2016-06-24T06:14:37.392Z") } #遍历获取 [doc['last_modified'] for doc in db.objects.find()] #[datetime.datetime(2016, 6, 24, 14, 12, 15, 226000)] #存入时间时带入时区 import pytz pacific = pytz.timezone('US/Pacific') aware_datetime = pacific.localize(datetime.datetime(2002, 10, 27, 6, 0, 0)) result = db.times.insert_one({"date": aware_datetime}) db.times.find_one()['date'] #读取时间 from bson.codec_options import CodecOptions db.times.find_one()['date'] aware_times = db.times.with_options(codec_options=CodecOptions(tz_aware=True,tzinfo=pytz.timezone('US/Pacific'))) result = aware_times.find_one()