MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Seleziona in base al timestamp e aggiorna il timestamp con zero

ISODate() è rappresentato come un datetime oggetto di PyMongo. MongoDB presuppone che le date e gli orari siano in UTC. Esistono diversi modi per ottenere la mezzanotte (inizio di un giorno) per un determinato orario UTC d :

>>> from datetime import datetime, time, timedelta
>>> d = datetime(2015, 10, 13, 1, 4, 30, 515000)
>>> datetime(d.year, d.month, d.day) # @user3100115' answer
datetime.datetime(2015, 10, 13, 0, 0)   # 369 ns
>>> datetime.fromordinal(d.toordinal()) # 451 ns
datetime.datetime(2015, 10, 13, 0, 0)
>>> datetime.combine(d, time.min)       # 609 ns
datetime.datetime(2015, 10, 13, 0, 0)
>>> d - (d - d.min) % timedelta(days=1) # Python 3
datetime.datetime(2015, 10, 13, 0, 0)   # 1.87 µs
>>> datetime(*d.timetuple()[:3])
datetime.datetime(2015, 10, 13, 0, 0)   # 2.34 µs
>>> from calendar import timegm
>>> datetime.utcfromtimestamp((timegm(d.timetuple()) // 86400) * 86400) # POSIX
datetime.datetime(2015, 10, 13, 0, 0)   # 4.72 µs