On 2022-04-14 15:22:29 -0000, Jon Ribbens via Python-list wrote: > On 2022-04-14, Paul Bryan <pbr...@anode.ca> wrote: > > I think because minutes and hours can easily be composed by multiplying > > seconds. days is separate because you cannot compose days from seconds; > > leap seconds are applied to days at various times, due to > > irregularities in the Earth's rotation. > > That's an argument that timedelta should *not* have a 'days' attribute, > because a day is not a fixed number of seconds long (to know how long > a day is, you have to know which day you're talking about, and where).
Which is exactly why timedelta *must* have separate fields for seconds, days and months. You can't simply express the larger units as multiples of the smaller units, so they have to be stored separately for date arithmetic to work. > It's an undocumented feature of timedelta that by 'day' it means '86400 > seconds'. I'd call that a bug, not a feature: >>> from datetime import datetime, timedelta >>> t0 = datetime.fromisoformat("2022-03-26T12:00").astimezone() >>> t0 datetime.datetime(2022, 3, 26, 12, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'CET')) >>> d = timedelta(days=1) >>> t1 = t0 + d >>> t1 datetime.datetime(2022, 3, 27, 12, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'CET')) >>> t1.isoformat() '2022-03-27T12:00:00+01:00' Python missed the switch to DST here, the timezone is wrong. If I do the same thing in PostgreSQL: hjp=> select '2022-03-26T12:00'::timestamptz; ╔════════════════════════╗ ║ timestamptz ║ ╟────────────────────────╢ ║ 2022-03-26 12:00:00+01 ║ ╚════════════════════════╝ (1 row) Time: 5.542 ms hjp=> select '2022-03-26T12:00'::timestamptz + '1 day'::interval; ╔════════════════════════╗ ║ ?column? ║ ╟────────────────────────╢ ║ 2022-03-27 12:00:00+02 ║ ╚════════════════════════╝ (1 row) It correctly determines that DST is already in effect at noon of March 27th. hp -- _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | h...@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!"
signature.asc
Description: PGP signature
-- https://mail.python.org/mailman/listinfo/python-list