dtpy Flashcards
naive datetimes do not know
what timezone they are in
aware datetimes know
what timezone you are in
to create a naive date, type
datetime.date(2000, 12, 30)
you should not use a local time zone in apps for anything except
converting it to display for users.
datetime.datetime.now(), datetime.time.now(), datetime.date.now() return the time/date in
my local timezone from computer, but in a naive form.
to return the day of week in 0-6 format, type
my_datetime.weekday()
a timedelta represents
the amount of time between 2 dates or times
to create a timedelta of 7 days type
datetime.timedelta(days=7)
to print out what the date will be in 7 days, type
datetime.date().today() + datetime.timedelta(days=7)
if you subtract a date or time from a date or time you get a
timedelta
note: cannot add
to return the number of seconds of a timedelta, type
my_time_delta.total_seconds()
the parameters to datetime.time() are
hours, minutes, seconds, milliseconds.
to get the current datetime in utc
datetime. datetime.now(tz= pytz.UTC)
note: Yes, this gets the time local to there, not my local timeand then just setting UTC as timezone.
to make an aware datetime, type
datetime.datetime(2016,10,20, 20, 10, 20000, tzinfo = pytz.UTC)
to convert a timezone aware datetime to the concurrent local time in a different timezone, type
Like at this time here, whats the local time there?
my_datetime.astimezone(pytz.timezone(‘TIMEZONE/STRING’))
To know when a datetime is timezone aware, you can
return it and see tzinfo
datetime.datetime(2018, 12, 25, 4, 15, 24, 686585, tzinfo=)
or print it and see the plus
2019-10-20 11-25-20.304943+08:00
an unaware datetime is effectively just
the numbers that make up all the date and time, but they don’t know what timezone they are in.
If you add a timezone, you get to know the time difference a datetime that knows what timezone it is in, and one that doesnt. If it is in a different timezone, it can account for the timezone difference to know the actually difference. Since there 6pm ET and 6pm PT look the same in numbers, but 6pm PT will happen in the future after 6pm ET.
to make a naive datetime into a timezone aware datetime, type
from pytz import timezone
unaware_datetime = datetime.datetime.now()
time_zone = timezone(‘TIMEZONE/STRING’)
aware_datetime = time_zone.localize(unaware_datetime)
to convert a datetime to a string, type
my_datetime.strftime(‘%b %d %Y’)
returnstringf -> RSF
https://www.youtube.com/watch?v=eirjjyP2qcQ
to convert a string to a datetime, type
datetime.datetime.strptime(“string”, “%b %d %Y”)
returndatetimep -> RDP
to convert a timezone aware datetime into a naive datetime, type
aware_datetime.replace(tzinfo=None)
to get the timedelta between to times, you need to
put those times into a datetime with the same date first
the bigger later/bigger date must come first to subtract
to convert a naive datetime to a timezone aware datetime using the timezone in django’s settings, type
from django.utils.timezone import make_aware
make_aware(naive_datetime)
auto_now_add is
timezone aware