I am trying to compare the current date and time with dates and times specified in models using comparison operators:
if challenge.datetime_start <= datetime.now() <= challenge.datetime_end:
The script errors out with:
TypeError: can"t compare offset-naive and offset-aware datetimes
The models look like this:
class Fundraising_Challenge(models.Model): name = models.CharField(max_length=100) datetime_start = models.DateTimeField() datetime_end = models.DateTimeField()
I also have django using locale date and times.
What I haven"t been able to find is the format django uses for DateTimeField(). Is it naive or aware? And how do I get datetime.now() to recognize locale datetime?
By default, the
datetime object is
naive in Python, so you need to make both of them either naive or aware
datetime objects. This can be done using:
import datetime import pytz utc=pytz.UTC challenge.datetime_start = utc.localize(challenge.datetime_start) challenge.datetime_end = utc.localize(challenge.datetime_end) # now both the datetime objects are aware, and you can compare them
Note: This would raise a
tzinfo is already set. If you are not sure about that, just use
start_time = challenge.datetime_start.replace(tzinfo=utc) end_time = challenge.datetime_end.replace(tzinfo=utc)
BTW, you could format a UNIX timestamp in datetime.datetime object with timezone info as following
d = datetime.datetime.utcfromtimestamp(int(unix_timestamp)) d_with_tz = datetime.datetime( year=d.year, month=d.month, day=d.day, hour=d.hour, minute=d.minute, second=d.second, tzinfo=pytz.UTC)
if timezone_aware_var <= datetime.datetime.now(timezone_aware_var.tzinfo): pass #some code
# Timezone info of your timezone aware variable timezone = your_timezone_aware_variable.tzinfo # Current datetime for the timezone of your variable now_in_timezone = datetime.datetime.now(timezone) # Now you can do a fair comparison, both datetime variables have the same time zone if your_timezone_aware_variable <= now_in_timezone: pass #some code
You must add the timezone info to your
However, you must add the same timezone of the reference variable; that is why I first read the
The ability to identify patterns is an essential component of sensory intelli- gent machines. Pattern recognition is therefore an indispensible component of the so-called “Intelligent Control System...
A Gentle Introduction to Numerical Simulations with Python 3.6. Computing, in the sense of doing mathematical calculations, is a skill that mankind has developed over thousands of years. Programmin...
Efficiently perform data collection, wrangling, analysis, and visualization using Python. Recent advancements in computing and artificial intelligence have completely changed the way we understand ...
Black Hat Python, 2nd Edition: Python Programming for Hackers and Pentesters PDF, 2nd Edition. Fully updated for Python 3, the second edition of this worldwide bestseller (over 100,000 copies sold)...