Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#13622 closed Bug Report - General (fixed)

Python Bindings: traceback in datetime.timestamp()

Reported by: rcrdnalor Owned by: rcrdnalor
Priority: minor Milestone: 31.1
Component: Bindings - Python Version: v31-fixes
Severity: medium Keywords: Python timestamp
Cc: Ticket locked: no

Description

Using Python3.6, the following sequence works:

$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from MythTV import datetime
>>> t = datetime.now()
>>> t.timestamp()
1590160731.214045

In contrast, on Python3.8, this gives an error:

$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from MythTV import datetime
>>> t = datetime.now()
>>> t.timestamp()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/local-admin/MythTV/mythservices/MythTV/utility/dt.py", line 483, in timestamp
    return ((utc_naive - utc_epoch).total_seconds())
TypeError: can't subtract offset-naive and offset-aware datetimes

Error reported on the MythTV forum: https://forum.mythtv.org/viewtopic.php?f=6&t=3793

Change History (4)

comment:1 Changed 4 years ago by rcrdnalor

From the Python docs: https://docs.python.org/3/whatsnew/3.8.html

Arithmetic operations between subclasses of datetime.date or datetime.datetime and datetime.timedelta objects now return an instance of the subclass, rather than the base class. This also affects the return type of operations whose implementation (directly or indirectly) uses datetime.timedelta arithmetic, such as astimezone(). (Contributed by Paul Ganssle in bpo-32417.)

Complete review of MythTV's datetime implementation needed.

comment:2 Changed 4 years ago by richardfearn

As a workaround, I'm doing this:

naive_datetime = mythtv_datetime.asnaiveutc()
python_datetime = datetime(naive_datetime.year, naive_datetime.month, naive_datetime.day,
                           naive_datetime.hour, naive_datetime.minute,
                           tzinfo=pytz.utc)

comment:3 Changed 4 years ago by Roland Ernst <rcrernst@…>

Resolution: fixed
Status: assignedclosed

In 24db137ee/mythtv:

Python: fix timestamp calculation

Python 3.8 changed the handling of the 'datetime' class:
According release notes ("What's New In Python 3.8"):
Arithmetic operations between subclasses of datetime.date or
datetime.datetime and datetime.timedelta objects now return an
instance of the subclass, rather than the base class.

This caused an error in the calculation of a 'timestamp'.

Tested with python2.7, python3.6 and python3.8.
No other occurences of similar arithmetic operations identified.

Fixes #13622

comment:4 Changed 4 years ago by Roland Ernst <rcrernst@…>

In d8ecd8fe7/mythtv:

Python: fix timestamp calculation

Python 3.8 changed the handling of the 'datetime' class:
According release notes ("What's New In Python 3.8"):
Arithmetic operations between subclasses of datetime.date or
datetime.datetime and datetime.timedelta objects now return an
instance of the subclass, rather than the base class.

This caused an error in the calculation of a 'timestamp'.

Tested with python2.7, python3.6 and python3.8.
No other occurences of similar arithmetic operations identified.

Fixes #13622

(cherry picked from commit 24db137ee6435ae0f1ecc51c580ef1b3d5936402)

Note: See TracTickets for help on using tickets.