简体   繁体   中英

Difference between the time objects in python?

What is the difference between the 2 statements:

import datetime
print datetime.datetime.now()

datetime.datetime(2015, 1, 28, 12, 32, 9, 762118)

from datetime import *
>> datetime.time(datetime.now())

datetime.time(12, 33, 3, 693084)

Actually I want to compare TimeField of a django model with the current day's 1 hour less time. My code snippet for the same:

Mymodel.objects.filter(
            follow_up_date=datetime.datetime.now().date,
            # commented now  
            # preferred_time__lt=(
            #     datetime.datetime.now() - datetime.timedelta(hours=1)),
            preferred_time__lt=(datetime.time(datetime.now()) - datetime.timedelta(hours=1)),
            )

Mymodel:

class Mymodel(models.Model):
  follow_up_date = models.DateField("Follow up Date",null=True,blank=True)
  preferred_time = models.TimeField(_("Preferred time"),default=now,
                                      null=True,blank=True)

I am trying to extract all the instances which are scheduled for the day, whose preferred time has elapsed just 1 hour back. Which should be the correct filter for the 'preferred_time'? I got wrong results for the commented code. I am not clear.

This is a cron job ie management command to be run every 1 hour in django

In the first instance:

datetime.datetime(2015, 1, 28, 12, 32, 9, 762118)

You have a datetime object. It has both the date (first three numbers) and the time (last four numbers).

The second object you mention:

datetime.time(12, 33, 3, 693084)

This is just the time component.

To compare a TimeField , you need just the time component; for a DateField , just the date component.

In your code, you have the following datetime.datetime.now().date this is just the name of the built-in function date . You need to call it:

>>> datetime.datetime.now().date
<built-in method date of datetime.datetime object at 0xb74ac9b0>
>>> datetime.datetime.now().date()
datetime.date(2015, 1, 28)

You also cannot do datetime.time(datetime.datetime.now()) , datetime.time() is a constructor method, it is not a way to covert other objects.

You also cannot subtract timedelta from a time object:

To get the correct result, you need to subtract one hour from the datetime object, then convert it to time:

>>> (datetime.datetime.now() - datetime.timedelta(hours=1)).time()
datetime.time(9, 27, 16, 93746)

In the end, your filter would look like:

filter_date = datetime.datetime.now().date()
filter_time = (datetime.datetime.now() - datetime.timedelta(hours=1)).time()

Mymodel.objects.filter(follow_up_date=filter_date,
                       preferred_time__lt=filter_time)
  1. datetime.now() given date and time information.
  2. datetime.time() give only time information.

eg

>>> from datetime import *
>>> datetime.now()
datetime.datetime(2015, 1, 28, 12, 52, 35, 164500)
>>> datetime.time(datetime.now())
datetime.time(12, 52, 41, 97521)
>>> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM