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
timedeltas can be
negative
to get a datetime representing the monday in a given week, type
datetime.datetime.strptime(‘2000-07-3’, ‘%Y-%W-%w’)
%W = week number zero padded %w = day of week 0-6
To replace just the time in a datetime, type
datetime.datetime.utcnow().replace(tzinfo= pytz.UTC, hour= 3, minute= 0, second= 0)
when I make pivot tables in pandas, I need to always catch
coonditions where there is no data, so I need to render an empty dataframe
general dango_pandas rules
after you read_frame, create a new df filtering for just the columns need so model changes don’t later change the df
To make a pivot table with cell links, include all the links as later columns of the pivot table, and then in template loop using iterrows.
Make the df look exactly how you want it to be displayed, dont rely on template a lot, but template filters are ok.
Account for the possibility that your data will be empty, make sure table still populates
keep in the template to see the real data
when filtering for a particular date range in django make sure to use
__gte and __lte instead of just gt or lt, because you need to include the date that set the range.
to get the first monday of every week, type
def get_monday(year, week): d = datetime.date(year,1,1) if(d.weekday()>3): d = d+datetime.timedelta(7-d.weekday()) else: d = d - datetime.timedelta(d.weekday()) dlt = datetime.timedelta(days = (week-1)*7) return d + dlt
datetime.date.isocalendar() returns
a tuple containing year, weeknumber and weekday
to get the current datetime in a chosen timezone, type
datetime. datetime.now(pytz.timezone(‘US/Eastern’))
note: this is tz aware