Skip to content

Commit

Permalink
improve DST-related error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ariebovenberg committed Sep 12, 2024
1 parent 1f5c79c commit 978affa
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
🚀 Changelog
============

0.6.9 (2024-09-12)
------------------

- Clarify DST-related error messages (#169)

0.6.8 (2024-09-05)
------------------

Expand Down
2 changes: 1 addition & 1 deletion docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ DST-safe arithmetic
Date and time arithmetic can be tricky due to daylight saving time (DST)
and other timezone changes.
The API of the different classes is designed to avoid implicitly ignoring these.
The type annotations and descriptive error messages should automatically guide you
The type annotations and descriptive error messages should guide you
to the correct usage.

- :class:`~whenever.Instant` has no calendar, so it doesn't support
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ authors = [
{name = "Arie Bovenberg", email = "a.c.bovenberg@gmail.com"},
]
readme = "README.md"
version = "0.6.8"
version = "0.6.9"
description = "Modern datetime library for Python, written in Rust"
requires-python = ">=3.9"
classifiers = [
Expand Down
13 changes: 8 additions & 5 deletions pysrc/whenever/_pywhenever.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# - It saves some overhead
from __future__ import annotations

__version__ = "0.6.8"
__version__ = "0.6.9"

import enum
import re
Expand Down Expand Up @@ -2942,7 +2942,8 @@ def now(
"and other timezone changes. Instead, use `Instant.now()` or "
"`ZonedDateTime.now(<tz name>)` if you know the timezone. "
"Or, if you want to ignore DST and accept potentially incorrect offsets, "
"pass `ignore_dst=True` to this method."
"pass `ignore_dst=True` to this method. For more information, see "
"whenever.rtfd.io/en/latest/overview.html#dst-safe-arithmetic"
)
secs, nanos = divmod(time_ns(), 1_000_000_000)
return cls._from_py_unchecked(
Expand Down Expand Up @@ -4425,15 +4426,17 @@ class ImplicitlyIgnoringDST(TypeError):
"and other timezone changes. To perform a DST-safe conversion, use "
"ZonedDateTime.from_timestamp() instead. "
"Or, if you don't know the timezone and accept potentially incorrect results "
"during DST transitions, pass `ignore_dst=True`."
"during DST transitions, pass `ignore_dst=True`. For more information, see "
"whenever.rtfd.io/en/latest/overview.html#dst-safe-arithmetic"
)


# FUTURE: docs link
_IGNORE_DST_SUGGESTION = """\
To perform DST-safe operations, convert to a ZonedDateTime first. \
Or, if you don't know the timezone and accept potentially incorrect results \
during DST transitions, pass `ignore_dst=True`."""
during DST transitions, pass `ignore_dst=True`. For more information, see \
whenever.rtfd.io/en/latest/overview.html#dst-safe-arithmetic"
"""


_EXC_ADJUST_OFFSET_DATETIME = ImplicitlyIgnoringDST(
Expand Down
6 changes: 4 additions & 2 deletions src/local_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ unsafe fn _shift_method(
"Adding time units to a LocalDateTime implicitly ignores \
Daylight Saving Time. Instead, convert to a ZonedDateTime first \
using assume_tz(). Or, if you're sure you want to ignore DST, \
explicitly pass ignore_dst=True."
explicitly pass ignore_dst=True. For more information, see \
whenever.rtfd.io/en/latest/overview.html#dst-safe-arithmetic"
))?
}
DateTime::extract(slf)
Expand All @@ -505,7 +506,8 @@ unsafe fn difference(
"The difference between two local datetimes implicitly ignores DST transitions. \
and other timezone changes. To perform DST-safe arithmetic, convert to a ZonedDateTime \
first using assume_tz(). Or, if you're sure you want to ignore DST, explicitly pass \
ignore_dst=True.",
ignore_dst=True. For more information, see \
whenever.rtfd.io/en/latest/overview.html#dst-safe-arithmetic",
)?;
let [arg] = *args else {
Err(type_err!("difference() takes exactly 1 argument"))?
Expand Down
18 changes: 11 additions & 7 deletions src/offset_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,11 @@ unsafe fn __sub__(obj_a: *mut PyObject, obj_b: *mut PyObject) -> PyReturn {
{
Err(py_err!(
state.exc_implicitly_ignoring_dst,
"Subtracting a delta from a LocalDateTime implicitly ignores \
"Subtracting a delta from an OffsetDateTime implicitly ignores \
Daylight Saving Time. Instead, convert to a ZonedDateTime first \
using to_tz(). Or, if you're sure you want to ignore DST, \
explicitly pass ignore_dst=True."
explicitly pass ignore_dst=True. For more information, see \
whenever.rtfd.io/en/latest/overview.html#dst-safe-arithmetic"
))?
} else {
return Ok(newref(Py_NotImplemented()));
Expand Down Expand Up @@ -659,7 +660,8 @@ unsafe fn now(
and other timezone changes. Instead, use `Instant.now()` or \
`ZonedDateTime.now(<tz name>)` if you know the timezone. \
Or, if you want to ignore DST and accept potentially incorrect offsets, \
pass `ignore_dst=True` to this method.",
pass `ignore_dst=True` to this method. For more information, see \
whenever.rtfd.io/en/latest/overview.html#dst-safe-arithmetic",
)?;

let offset_secs = extract_offset(offset, state.time_delta_type)?;
Expand Down Expand Up @@ -796,10 +798,11 @@ unsafe fn _shift_method(
if !ignore_dst {
Err(py_err!(
state.exc_implicitly_ignoring_dst,
"Adding time units to a LocalDateTime implicitly ignores \
"Adding time units to an OffsetDateTime implicitly ignores \
Daylight Saving Time. Instead, convert to a ZonedDateTime first \
using assume_tz(). Or, if you're sure you want to ignore DST, \
explicitly pass ignore_dst=True."
explicitly pass ignore_dst=True. For more information, see \
whenever.rtfd.io/en/latest/overview.html#dst-safe-arithmetic"
))?
}
let OffsetDateTime {
Expand Down Expand Up @@ -1264,9 +1267,10 @@ static mut GETSETTERS: &[PyGetSetDef] = &[
];

static IGNORE_DST_MSG: &str =
"Adjusting a fixed offset datetime implicitly ignores DST and other timezone changes. \
"Adjusting a fixed-offset datetime implicitly ignores DST and other timezone changes. \
To perform DST-safe operations, convert to a ZonedDateTime first using `to_tz()`. \
Or, if you don't know the timezone and accept potentially incorrect results \
during DST transitions, pass `ignore_dst=True`.";
during DST transitions, pass `ignore_dst=True`. For more information, see \
whenever.rtfd.io/en/latest/overview.html#dst-safe-arithmetic";

type_spec!(OffsetDateTime, SLOTS);

0 comments on commit 978affa

Please sign in to comment.