0

I have noticed that in python-3.x there is a datetime.py file in the python path which is what is included with import datetime, while there was no such file in python-2.7. At the end of this file an import _datetime statement imports, what I assume is, a compiled version. If this last import is removed, the basic datetime functionality works (with performance penalty) but if one imports the, new in 3.9, zoneinfo module a segmentation fault occurs (pytz continues to work).

My questions are:

  1. Why this is now included in python-3.x?
  2. Is datetime.py intended as a fully functional equivalent of _datetime?
  3. Why does import zoneinfo crash? Is this a bug, or zoneinfo needs, by design, the compiled version?

The idea is to modify datetime.py so as to include some functionality, basically a timedelta.__format__ and stop treating naive datetime objects as local times. Do you think this is feasible?

NameOfTheRose
  • 819
  • 1
  • 8
  • 18
  • 2
    `timezone` as a datetime class, not a module. to work with time zones (other than simple UTC offsets), use the [zoneinfo](https://docs.python.org/3/library/zoneinfo.html) module. – FObersteiner Dec 16 '20 at 10:52
  • ...and instead of subclassing datetime.datetime, don't you think it would be easier to work with aware datetime instead (e.g. always use tzinfo=timezone.utc)? That would avoid running into any default=localtime-traps*. – FObersteiner Dec 16 '20 at 11:01
  • @MrFuppes,Thank you, indeed I made a mistake, I meant the zoneinfo module. I edited the question. Concerning your 2nd comment, no I would not because it is so much more typing. Besides I am not thinking of subclassing but modifying the module to suit my preferences, which I agree that may very well look be arbitrary and unjustified to somebody else. I will explain why I do not prefer subclassing in a comment to [Xterm's answer](https://stackoverflow.com/a/65321711/2422503) below. – NameOfTheRose Dec 16 '20 at 14:55
  • BTW, here is the bpo where the python only implementation was done: https://bugs.python.org/issue7989 – Xtrem532 Dec 17 '20 at 11:08
  • On Python 3.9.1+ `import zoneinfo` does not crash any more but raises the exception `AttributeError: module 'datetime' has no attribute 'datetime_CAPI'`. That's progress I suppose. – NameOfTheRose Feb 20 '21 at 11:38
  • If one modifies the python path, so that zoneinfo **precedes** lib-dynload ,everything works `sys.path.insert(3,'/home/knoppix/.pyenv/versions/3.9-dev/lib/python3.9/zoneinfo/')`. I presume if I were to delete `_datetime.cpython-39-x86_64-linux-gnu.so` from the `lib_dynload` directory, it would do the trick too. – NameOfTheRose Feb 22 '21 at 10:18

1 Answers1

0

If you want to modify objects of the standard library, subclasss them. for your timedelta example:

from datetime import timedelta

class MyTimedelta(timedelta):
    def __format__(self, ...):
        ....

Modifying the stdlib only leads to many tears.

Xtrem532
  • 756
  • 8
  • 19
  • Thank you, I have tried your suggestion first. But in order to make the `MyTimedelta subclass` functional it is necessary to you need to reimplement also arithmetic methods to return the new class, and also modify the `datetime` and `date` subtract methods to return the new class too. After all I think `__format__` should exist anyway. But I agree that my road may _lead me to many tears_. – NameOfTheRose Dec 16 '20 at 15:06
  • I have no idea why `timedelta` has no format - after googleing around, I suspect nobody needed it till now. You should probably ask at https://bugs.python.org/ or on the mailing lists if there is a reason for it not existing, and if it could be implemented in 3.10. – Xtrem532 Dec 17 '20 at 11:10
  • I think at least [59 people would like](https://stackoverflow.com/questions/538666/format-timedelta-to-string#comment16142124_538666) such a functionality. Thank you for the link to the [bpo discussion](https://bugs.python.org/issue7989), very informative. Answers my questions 1, 2 and 3. – NameOfTheRose Dec 18 '20 at 14:43