Getting "last second of tomorrow" in 0.10 #322
|
In 0.9, this is how I wrote "the last second of tomorrow, in this given time zone": ZonedDateTime.now(TIME_ZONE).start_of_day() + days(2) - seconds(1)Going through 0.10 migration, I ended up with this (which seems a bit harder to read): ZonedDateTime.now(TIME_ZONE).start_of_day().add(ItemizedDelta(days=2, seconds=-1))I was wondering if there might be a clearer way of writing this? |
Answered by
ariebovenberg
Mar 21, 2026
Replies: 2 comments 7 replies
|
Hi @injust thanks for posting. The cleanest way to do this would be: ZonedDateTime.now(TZ).start_of_day().add(days=1, seconds=-1)Note that ZonedDateTime.now(TZ).round("day", mode="ceil").subtract(seconds=1)Other notes:
|
3 replies
Answer selected by
injust
|
Unrelated, but I wanted to mention this while I'm here: I really love the trimming pattern format! Was able to replace this: def format_time(time: Time) -> str:
if millis := time.nanosecond // 1_000_000:
format = f"%I:%M:%S.{millis:03d} %p"
elif time.second:
format = "%I:%M:%S %p"
else:
format = "%I:%M %p"
return time.py_datetime().strftime(format).removeprefix("0")with this: def format_time(time: Time) -> str:
pattern = "ii:mm:ss.FFF aa" if time.second or time.nanosecond else "ii:mm aa"
return time.format(pattern).removeprefix("0") |
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @injust thanks for posting.
The cleanest way to do this would be:
Note that
roundcould be used too, but not if the time is exactly00:00:00.000000000(it wouldn't round "up" in that case)Other notes:
ItemizedDelta(days=2, seconds=-1)in your example wouldn't work because it can't have a mixed sign.end_of(<unit>)method. Then it'd beZonedDateTime.now(TZ).end_of("day").subtract(seconds=1)