dtpy Flashcards

1
Q

naive datetimes do not know

A

what timezone they are in

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

aware datetimes know

A

what timezone you are in

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

to create a naive date, type

A

datetime.date(2000, 12, 30)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

you should not use a local time zone in apps for anything except

A

converting it to display for users.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

datetime.datetime.now(), datetime.time.now(), datetime.date.now() return the time/date in

A

my local timezone from computer, but in a naive form.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

to return the day of week in 0-6 format, type

A

my_datetime.weekday()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

a timedelta represents

A

the amount of time between 2 dates or times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

to create a timedelta of 7 days type

A

datetime.timedelta(days=7)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

to print out what the date will be in 7 days, type

A

datetime.date().today() + datetime.timedelta(days=7)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

if you subtract a date or time from a date or time you get a

A

timedelta

note: cannot add

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

to return the number of seconds of a timedelta, type

A

my_time_delta.total_seconds()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

the parameters to datetime.time() are

A

hours, minutes, seconds, milliseconds.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

to get the current datetime in utc

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

to make an aware datetime, type

A

datetime.datetime(2016,10,20, 20, 10, 20000, tzinfo = pytz.UTC)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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?

A

my_datetime.astimezone(pytz.timezone(‘TIMEZONE/STRING’))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

To know when a datetime is timezone aware, you can

A

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

17
Q

an unaware datetime is effectively just

A

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.

18
Q

to make a naive datetime into a timezone aware datetime, type

A

from pytz import timezone

unaware_datetime = datetime.datetime.now()

time_zone = timezone(‘TIMEZONE/STRING’)

aware_datetime = time_zone.localize(unaware_datetime)

19
Q

to convert a datetime to a string, type

A

my_datetime.strftime(‘%b %d %Y’)

returnstringf -> RSF

https://www.youtube.com/watch?v=eirjjyP2qcQ

20
Q

to convert a string to a datetime, type

A

datetime.datetime.strptime(“string”, “%b %d %Y”)

returndatetimep -> RDP

21
Q

to convert a timezone aware datetime into a naive datetime, type

A

aware_datetime.replace(tzinfo=None)

22
Q

to get the timedelta between to times, you need to

A

put those times into a datetime with the same date first

the bigger later/bigger date must come first to subtract

23
Q

to convert a naive datetime to a timezone aware datetime using the timezone in django’s settings, type

A

from django.utils.timezone import make_aware

make_aware(naive_datetime)

24
Q

auto_now_add is

A

timezone aware

25
Q

timedeltas can be

A

negative

26
Q

to get a datetime representing the monday in a given week, type

A

datetime.datetime.strptime(‘2000-07-3’, ‘%Y-%W-%w’)

%W = week number zero padded
%w = day of week 0-6
27
Q

To replace just the time in a datetime, type

A

datetime.datetime.utcnow().replace(tzinfo= pytz.UTC, hour= 3, minute= 0, second= 0)

28
Q

when I make pivot tables in pandas, I need to always catch

A

coonditions where there is no data, so I need to render an empty dataframe

29
Q

general dango_pandas rules

A

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

30
Q

when filtering for a particular date range in django make sure to use

A

__gte and __lte instead of just gt or lt, because you need to include the date that set the range.

31
Q

to get the first monday of every week, type

A
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
32
Q

datetime.date.isocalendar() returns

A

a tuple containing year, weeknumber and weekday

33
Q

to get the current datetime in a chosen timezone, type

A

datetime. datetime.now(pytz.timezone(‘US/Eastern’))

note: this is tz aware