Skip to content

Commit

Permalink
The conversion to timestamps from MythTV's datetime object
Browse files Browse the repository at this point in the history
 and vice versa does not work if current time is in daylight saving zone
 (dst).

Because of this bug, the methods 'fromEtree' and 'fromJSON' of the class 'Program' of the Python bindings
cannot convert fetched time values of format '2016-03-14T01:59:21Z' to correct timestamps.

For additional info, see
https://stackoverflow.com/questions/8777753/converting-datetime-date-to-utc-timestamp-in-python

I added a revised 'timestamp' method to MythTV's python binding 'datetime' and compared the output:

    def revised_timestamp(self):
         # utc time = local time - utc offset
         utc_naive = self.replace(tzinfo=None) - self.utcoffset()
         utc_epoch = self.utcfromtimestamp(0).replace(tzinfo=None)
         return ((utc_naive - utc_epoch).total_seconds())

    def timestamp(self):
        return time.mktime(self.timetuple()) + self.microsecond/1000000.

In the follwing example, times are taken at
Saturday, June 1, 2019 2:03:48.066 PM GMT+02:00 DST (for variable 'now') and
Saturday, June 1, 2019 2:04:07.954 PM GMT+02:00 DST (for variable('now_utc')

$ python2
>>> from MythTV import datetime
>>> now = datetime.now()
>>> now
datetime(2019, 6, 1, 14, 3, 48, 66210, tzinfo=<MythTV.utility.dt.posixtzinfo object at 0x7ff760301910>)
>>> now.utcoffset()
datetime.timedelta(0, 7200)
>>> now.timestamp()
1559390628.06621

Check https://www.epochconverter.com/ :
Convert epoch to human readable date and vice versa
1559390628.06621
GMT: Saturday, June 1, 2019 12:03:48.066 PM
Your time zone: Saturday, June 1, 2019 2:03:48.066 PM GMT+02:00 DST
Relative: 4 minutes ago

---> That's correct.

>>> now_utc = datetime.utcnow()
>>> now_utc
datetime(2019, 6, 1, 12, 4, 7, 954273, tzinfo=<MythTV.utility.dt.posixtzinfo object at 0x7ff7602f22d0>)
>>> now_utc.utcoffset()
datetime.timedelta(0)
>>> now_utc.timestamp()
1559387047.954273

Check https://www.epochconverter.com/ :
Convert epoch to human readable date and vice versa
1559387047.954273
GMT: Saturday, June 1, 2019 11:04:07.954 AM
Your time zone: Saturday, June 1, 2019 1:04:07.954 PM GMT+02:00 DST

---> That's wrong !

Now let's do the same with the 'revised' method of datetime:
>>> now.revised_timestamp()
1559390628.06621
https://www.epochconverter.com/
Convert epoch to human readable date and vice versa
1559390628.06621
GMT: Saturday, June 1, 2019 12:03:48.066 PM
Your time zone: Saturday, June 1, 2019 2:03:48.066 PM GMT+02:00 DST

---> That's correct.

>>> now_utc.revised_timestamp()
1559390647.954273

https://www.epochconverter.com/
1559390647.954273
GMT: Saturday, June 1, 2019 12:04:07.954 PM
Your time zone: Saturday, June 1, 2019 2:04:07.954 PM GMT+02:00 DST

---> That's correct as well!
  • Loading branch information
rcrdnalor authored and Bill Meek committed Nov 15, 2019
1 parent 7717965 commit 695e68d
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mythtv/bindings/python/MythTV/utility/dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,10 @@ def mythformat(self):
return self.astimezone(self.UTCTZ()).strftime('%Y%m%d%H%M%S')

def timestamp(self):
return time.mktime(self.timetuple()) + self.microsecond/1000000.
# utc time = local time - utc offset
utc_naive = self.replace(tzinfo=None) - self.utcoffset()
utc_epoch = self.utcfromtimestamp(0).replace(tzinfo=None)
return ((utc_naive - utc_epoch).total_seconds())

def rfcformat(self):
return self.strftime('%a, %d %b %Y %H:%M:%S %z')
Expand Down

0 comments on commit 695e68d

Please sign in to comment.