Skip to content

model: accept any UTC tzinfo for Edm.DateTime - #334

Open
VitorCoelho wants to merge 1 commit into
SAP:masterfrom
VitorCoelho:fix/datetime-accept-utc-tzinfo
Open

VitorCoelho wants to merge 1 commit into
SAP:masterfrom
VitorCoelho:fix/datetime-accept-utc-tzinfo

Conversation

@VitorCoelho

Copy link
Copy Markdown
Contributor

What

Edm.DateTime to_literal() and to_json() now accept UTC datetimes whose tzinfo is zoneinfo.ZoneInfo('UTC'), pytz.utc or dateutil.tz.UTC. Before, these raised PyODataModelError: Edm.DateTime accepts only UTC.

Why

The check was value.tzinfo != datetime.timezone.utc. Only fixed-offset datetime.timezone objects compare equal to timezone.utc, so a UTC datetime from the standard zoneinfo module or from pytz/dateutil failed with an error message that says the opposite of what happened. This affects $filter values (filter(Date__gte=...)) and create/update payloads:

from datetime import datetime
from zoneinfo import ZoneInfo
service.entity_sets.TemperatureMeasurements.create_entity().set(
    Sensor='Sensor1', Date=datetime(2017, 12, 24, 18, 0, tzinfo=ZoneInfo('UTC')), Value=34.0)
# PyODataModelError: Edm.DateTime accepts only UTC

How

A datetime is treated as UTC when its tzinfo equals datetime.timezone.utc (the previous rule, so everything accepted before is still accepted) or when its offset is zero and tzname() is "UTC".

I did not use a plain "offset is zero" rule: it would accept Europe/London in winter and reject it in summer, so the same code would start failing in March. With this rule, London is rejected all year, as before. For the same reason zones named "GMT" (Etc/GMT, pytz.timezone('GMT')) are still rejected, as London is also called "GMT" in winter. Naive datetimes are still rejected. If you prefer the simpler offset-only rule, I can change it.

Tests

  • New test_traits_datetime_accepts_any_utc_tzinfo (timezone.utc, zero offset, ZoneInfo('UTC'), ZoneInfo('Etc/UTC'), a fixed UTC tzinfo like pytz/dateutil use): the 3 non-timezone cases fail on master.
  • New test_traits_datetime_rejects_non_utc: naive, +01:00, a tzinfo named "UTC" with an offset, Europe/London in summer and winter.
  • Checked against the real libraries too (pytz, python-dateutil, zoneinfo): pytz.utc, pytz.timezone('UTC'), dateutil.tz.UTC, tzutc(), gettz('UTC'), ZoneInfo('UTC'/'Etc/UTC'/'Zulu') are accepted in January and July; London, GMT, Abidjan and +01:00 are rejected in both. For 20,000 random datetimes the literal and JSON are identical to timezone.utc.
  • Full suite passes on Python 3.10, 3.11, 3.12, 3.13 and 3.14; pylint 10.00/10, flake8 clean on pyodata.
  • CHANGELOG updated (Unreleased). It will conflict only on the ### Fixed heading with model: keep all digits of Edm.Double values in literals and payloads #332 if both are merged; whichever goes second just keeps both lines.

Edm.DateTime to_literal() and to_json() compared value.tzinfo with
datetime.timezone.utc. Only fixed-offset datetime.timezone objects compare
equal to it, so UTC datetimes built with zoneinfo.ZoneInfo('UTC'),
pytz.utc or dateutil.tz.UTC were rejected with the contradictory error
"Edm.DateTime accepts only UTC", both in $filter literals and in
create/update payloads.

Treat a datetime as UTC when its tzinfo equals datetime.timezone.utc (the
previous rule, unchanged) or when its offset is zero and its tzname() is
"UTC". Zones that only have a zero offset at that moment, such as
Europe/London in winter, are still rejected, so whether a value is
accepted does not depend on the season. Naive datetimes are still
rejected.

Signed-off-by: VitorCoelho <vitorc195@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant