Pre-Pre-PEP: The datetime.timedeltacal class

Peter J. Holzer hjp-python at hjp.at
Sat Apr 16 14:18:16 EDT 2022


On 2022-04-16 19:35:51 +0200, Peter J. Holzer wrote:
> Note that t + d - d is in general not equal to t.
> 
> We can't cnange the semantics of datetime - datetime, so there must be a
> function to compute the difference between to datetimes as a
> timedeltacal. It could be a method on datetime (maybe t.sub(u) for t-u
> like in Go) or a constructor which takes two datetime objects.

Just noticed this. Using a method or constructor instead of an operator
adjusting behaviour via additional parameters. 

So for example a parameter "maxunit" could be used to restrict the units
used in the result:

Given:

>>> CET = zoneinfo.ZoneInfo('Europe/Vienna')
>>> t0 = datetime.datetime(2022, 3, 1, tzinfo=CET)
>>> t1 = datetime.datetime(2022, 4, 16, 20, tzinfo=CET)

we could get these results:

>>> timedeltacal(t0, t1, maxunit="month")
timedeltacal(months=1, days=15, seconds=72000)
>>> timedeltacal(t0, t1, maxunit="days")
timedeltacal(days=46, seconds=72000)
>>> timedeltacal(t0, t1, maxunit="seconds")
timedeltacal(seconds=4042800)

(note that 4042800 == 46 * 86400 + 19 * 3600)

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20220416/6918c5e9/attachment.sig>


More information about the Python-list mailing list