8

I am having trouble understanding functions that use:

time = timezone.now() - datetime.timedelta(days=30)

Firstly the timezone.now() gives the time that is set in Django … now the datetime.timedelta(days=30)

does it use the internal settings in django as set setting.py or another.

Secondly if so … should: the variable time not be 30days behind the current timezone.now();

In the function below

def test_was_published_recently_with_old_question(self):
    """
    was_published_recently() should return False for questions whose
    pub_date is older than 1 day.
    """
    time = timezone.now() - datetime.timedelta(days=30)
    old_question = Question(pub_date=time)
    self.assertEqual(old_question.was_published_recently(), False)

how does this produce 1 day older … I think my issue is not understanding

time = timezone.now() - datetime.timedelta(days=30) in its entirely

I would really appreciate the help. I am new to python and med level programmer … but haven't worked much with time.

Miki
  • 40,887
  • 13
  • 123
  • 202
Curious Lambda
  • 537
  • 2
  • 7
  • 21
  • Run `python manage.py shell` and see that `timezone.now()` is a `datetime` object that is expected to work with `timedelta`. Unrelated: make sure `USE_TZ=True` so that `timezone.now()` returns an aware datetime object otherwise the result of `.was_published_recently()` may be inaccurate, see [Find if 24 hrs have passed between datetimes - Python](http://stackoverflow.com/q/26313520/4279) – jfs Sep 10 '15 at 21:26

2 Answers2

15

Firstly the timezone.now() gives the time that is set in Django … now the datetime.timedelta(days=30)

does it use the internal settings in django as set setting.py or another.

First of all timezone.now() is just an "improved" version of datetime.datetime.now() that's also timezone aware.

timezone.now()
>>> datetime.datetime(2015, 9, 10, 19, 45, 34, 105121, tzinfo=<UTC>)
datetime.datetime.now()
>>> datetime.datetime(2015, 9, 10, 19, 45, 48, 356860)

There is no django internal time. timezone.now() is literally just the datetime function + timezone.


timedelta then is just a difference, a delta as used in physics. It doesn't know anything about time.

When you substract/add a delta from a time you do the according thing to the time.

So today is 2015-09-10. If you add one day (timedelta(days=1)) to that that's obviously 2015-09-11.

A check whether something is more recent than a month in python is done by substracting 30 days from now (or 1 month if you like) and then comparing if the saved time is bigger than that.


To understand the datetime comparison better it may help converting these to unixtime and just seeing these abstract things as simple numbers by using the timestamp() function:

one_month_ago = (timezone.now() - datetime.timedelta(days=30)).timestamp()
now = timezone.now().timestamp()
print(one_month_ago)
print(now)
if one_month_ago < now:
    print("a month ago is smaller")

outputs

1439323463.786164
1441915463.786201
a month ago is smaller

Doing this without converting to timestamp prints a month ago is smaller too.

Community
  • 1
  • 1
Chris Schäpers
  • 1,238
  • 10
  • 17
  • 4
    [unless `USE_TZ=True`; `timezone.now()` may return a naive datetime object](https://docs.djangoproject.com/en/1.8/_modules/django/utils/timezone/#now) – jfs Sep 10 '15 at 21:29
1

I think you are confused from the description of the test method.

This test method is testing that was_published_recently() works correctly by not returning those questions that are older than 1 day.

To test it, what they are doing is setting a post's date 30 days less than today (that's the - datetime.timedelta(days=30), and then checking if was_published_today() is True or False for that post.

If its False, then the test passes.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Thank you, I think this is my main confusion, I kind of get it... still a bit lost why this function would then return false for post older than a day as stated in the comments... Surely it would be posts older than 30 days as specified by the delta, in light of Christophers answer.... – Curious Lambda Sep 12 '15 at 09:22