Python
Dates & Times
37 flashcards · answers and spaced-repetition review in the KnowCard app
In the datetime module, which type holds a calendar day only (no clock), which holds a wall-clock time only, and which combines both?
You subtract two datetime objects to get a duration. What type comes back, and what do you call to turn it into a plain number of seconds?
What does this print?
```
from datetime import timedelta
d = timedelta(days=32, seconds=29700)
print(d.seconds)
```
strftime vs strptime: which one turns a datetime into a formatted string, and which parses a string into a datetime?
You want to parse '19.09.2007 14:30:00' into a datetime. What's wrong with this?
```
datetime.strftime(s, '%d.%m.%Y %H:%M:%S')
```
In strftime/strptime format codes, what's the difference between %Y, %m, %M, and %d?
datetime.now() vs datetime.utcnow(): why is the second one a trap?
What makes a datetime 'naive' vs 'aware', and why does the distinction cause bugs?
What happens here?
```
from datetime import datetime, timezone
a = datetime.now()
b = datetime.now(timezone.utc)
print(b - a)
```
For timing how long a function takes, why use time.perf_counter() instead of time.time()?
What does time.time() return, and what does its value mean?
Why does perf_counter_ns() exist alongside perf_counter()?
How do you attach a real time zone to a datetime, and what does ZoneInfo('Area/City') expect?
What does astimezone() do to a datetime — change the displayed time, the instant, or both?
Within one zone, what does adding timedelta(days=1) to an aware datetime do across a DST change?
What does this print?
```
d = datetime.date(1995, 2, 29)
```
What does delta.days hold for a small negative timedelta, and why does that bite datetime.date math?
How do you build a datetime from a Unix timestamp, and from an existing datetime back to a timestamp?
What's the result and why?
```
from datetime import timedelta
print(timedelta(days=0.5))
```
weekday() on a date/datetime: what integer does Monday map to, and what about Sunday?
Adding timedelta(days=1) to an aware datetime within one zone keeps the same wall-clock time (calendrical view). How does subtracting across two different zones behave, and how do you force an absolute 24-hour span within one zone?
Your ZoneInfo('Europe/Berlin') code works on Linux but raises ZoneInfoNotFoundError on Windows. Why, and what's the fix?
Given a datetime, how do you pull out just its calendar date, just its time of day, and change the 'T' separator in its ISO string?
While time.sleep(2.5) is running, is the program busy-waiting and burning CPU?
Python ships two time modules, time and datetime. What is each one's orientation, and when do you reach for one over the other?
A time.struct_time has nine fields you can read by index or name. Its tm_sec field is documented as ranging 0-61, not 0-59 - why the extra values?
The time module exposes the constants timezone, altzone, daylight, and tzname. Which UTC-offset constant ignores DST and which accounts for it?
time.gmtime(secs) and time.localtime(secs) both turn a Unix timestamp into a struct_time. What's the one difference, and what do they return with no argument?
time.asctime() and time.ctime() produce nearly identical strings. What input does each take, and what's fixed about the output?
time.localtime turns a Unix timestamp into a struct_time. Which function goes the other way, and on what time basis?
In strftime, both %U and %W give the week number of the year. What distinguishes them (and how are they different from %w)?
You call time.strptime(s) and leave off the format string. What format does it assume, and what kind of string does that parse?
Beyond date(y, m, d), how do you get today's date, a date from a Unix timestamp, and convert a date to/from a 'YYYY-MM-DD' string?
What does this print?
from datetime import time
print(time(14, 30, 5).isoformat())
You have a date and a separate time object. How do you fuse them into one datetime, and what happens if you pass a full datetime as the date argument?
What do these two prints produce?
from datetime import timedelta
week = timedelta(days=7)
print(week / 7)
print((52*week + week/7) / week)
On an aware datetime, what do tzname(), utcoffset(), and dst() each return?
Start learning today
Free to start — download the app or use it in your browser.
