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.
total_seconds() returns a float value, including microseconds. Note that you need to use a timezone aware datetime object, since the Django DateTimeField handles timezone aware datetime objects as well. See Django Timezones documentation. Because both objects are timezone aware (have a .
timezone. now() useful. This function returns the current date and time as a naive datetime when USE_TZ = False and as an aware datetime when USE_TZ = True .
The timedelta is a class in datetime module that represents duration. The delta means average of difference and so the duration expresses the difference between two date, datetime or time instances. By using timedelta, you may estimate the time for future and past.
First, open the views.py file of your Django application and import the datetime module. Next, use the datetime. now() method to get the current date and time value.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With