0

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 i.e management command to be run every 1 hour in django

Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
user956424
  • 1,611
  • 2
  • 37
  • 67
  • 1
    There are three different types: `datetime.date` corresponds to a calendar date, so you can store year, month, and day of month. `datetime.time` is the time of a single day, with hours, minutes, seconds, and microseconds. And `datetime.datetime` combines both and gives you a time *with* an actual calendar date. – poke Jan 28 '15 at 07:22

2 Answers2

2

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)
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • note: `datetime.time(datetime.now())` is another spelling of the proper call `datetime.now().time()`. – jfs Jan 28 '15 at 12:53
  • please, do not substract naive datetime objects that represent local time. The operands may have different UTC offsets e.g., on the opposite sides of DST transition -- it may produce a wrong result (by an hour usually). See explanation in [Find if 24 hrs have passed between datetimes - Python](http://stackoverflow.com/a/26313848/4279) – jfs Jan 28 '15 at 12:55
  • Are you sure about the `datetime.time(datetime.now())` ? I get `TypeError: an integer is required` @J.F.Sebastian – Burhan Khalid Jan 29 '15 at 04:32
  • I'm sure. Run the code exactly as it shown in the question and in my comment. Ponder why the first argument in a method is called `self` in Python – jfs Jan 29 '15 at 08:47
0
  1. datetime.now() given date and time information.
  2. datetime.time() give only time information.

e.g

>>> from datetime import *
>>> datetime.now()
datetime.datetime(2015, 1, 28, 12, 52, 35, 164500)
>>> datetime.time(datetime.now())
datetime.time(12, 52, 41, 97521)
>>> 
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56