2018-11-19 00:59:17 +11:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
2021-01-25 23:01:03 +11:00
|
|
|
from logging import getLogger
|
2018-11-19 00:59:17 +11:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from time import localtime, strftime
|
|
|
|
|
2021-01-25 23:01:03 +11:00
|
|
|
LOG = getLogger('PLEX.timing')
|
|
|
|
|
2018-11-19 00:59:17 +11:00
|
|
|
EPOCH = datetime.utcfromtimestamp(0)
|
|
|
|
|
|
|
|
|
|
|
|
def unix_timestamp(seconds_into_the_future=None):
|
|
|
|
"""
|
|
|
|
Returns a Unix time stamp (seconds passed since January 1 1970) for NOW as
|
|
|
|
an integer.
|
|
|
|
|
|
|
|
Optionally, pass seconds_into_the_future: positive int's will result in a
|
|
|
|
future timestamp, negative the past
|
|
|
|
"""
|
|
|
|
if seconds_into_the_future:
|
|
|
|
future = datetime.utcnow() + timedelta(seconds=seconds_into_the_future)
|
|
|
|
else:
|
|
|
|
future = datetime.utcnow()
|
|
|
|
return int((future - EPOCH).total_seconds())
|
|
|
|
|
|
|
|
|
|
|
|
def unix_date_to_kodi(unix_kodi_time):
|
|
|
|
"""
|
|
|
|
converts a Unix time stamp (seconds passed sinceJanuary 1 1970) to a
|
|
|
|
propper, human-readable time stamp used by Kodi
|
|
|
|
|
|
|
|
Output: Y-m-d h:m:s = 2009-04-05 23:16:04
|
|
|
|
"""
|
2021-01-25 23:01:03 +11:00
|
|
|
try:
|
|
|
|
return strftime('%Y-%m-%d %H:%M:%S', localtime(float(unix_kodi_time)))
|
2021-07-25 18:53:06 +10:00
|
|
|
except:
|
|
|
|
LOG.error('Received an illegal timestamp from Plex: %s. '
|
|
|
|
'Using 1970-01-01 12:00:00',
|
|
|
|
unix_kodi_time)
|
2021-01-25 23:01:03 +11:00
|
|
|
return '1970-01-01 12:00:00'
|
2018-11-19 00:59:17 +11:00
|
|
|
|
|
|
|
|
|
|
|
def plex_date_to_kodi(plex_timestamp):
|
|
|
|
"""
|
2019-06-29 01:43:54 +10:00
|
|
|
converts a PMS epoch time stamp (seconds passed since January 1 1970, Plex
|
|
|
|
sends timezone-independent epoch) to a propper, human-readable time stamp
|
|
|
|
used by Kodi (varies per time-zone!)
|
2018-11-19 00:59:17 +11:00
|
|
|
|
|
|
|
Output: Y-m-d h:m:s = 2009-04-05 23:16:04
|
2018-12-20 22:52:45 +11:00
|
|
|
|
|
|
|
Returns None if plex_timestamp is not valid (e.g. -1))
|
2018-11-19 00:59:17 +11:00
|
|
|
"""
|
2018-12-15 05:38:34 +11:00
|
|
|
try:
|
2019-06-29 01:43:54 +10:00
|
|
|
return unix_date_to_kodi(plex_timestamp)
|
2018-12-15 05:38:34 +11:00
|
|
|
except ValueError:
|
|
|
|
# the PMS can return -1 as plex_timestamp - great!
|
2019-06-29 01:43:54 +10:00
|
|
|
pass
|
2018-12-09 23:02:08 +11:00
|
|
|
|
|
|
|
|
|
|
|
def plex_now():
|
2019-06-29 01:43:54 +10:00
|
|
|
return unix_timestamp()
|
2018-12-09 23:02:08 +11:00
|
|
|
|
|
|
|
|
2018-11-19 00:59:17 +11:00
|
|
|
def kodi_timestamp(plex_timestamp):
|
|
|
|
return unix_date_to_kodi(plex_timestamp)
|
|
|
|
|
|
|
|
|
|
|
|
def kodi_now():
|
|
|
|
return unix_date_to_kodi(unix_timestamp())
|
|
|
|
|
|
|
|
|
|
|
|
def millis_to_kodi_time(milliseconds):
|
|
|
|
"""
|
2019-10-25 22:41:54 +11:00
|
|
|
Converts time in milliseconds [int or float] to the time dict used by the
|
|
|
|
Kodi JSON RPC:
|
2018-11-19 00:59:17 +11:00
|
|
|
{
|
|
|
|
'hours': [int],
|
|
|
|
'minutes': [int],
|
|
|
|
'seconds'[int],
|
|
|
|
'milliseconds': [int]
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
seconds = int(milliseconds / 1000)
|
|
|
|
minutes = int(seconds / 60)
|
2019-10-25 22:41:54 +11:00
|
|
|
return {'hours': int(minutes / 60),
|
|
|
|
'minutes': int(minutes % 60),
|
|
|
|
'seconds': int(seconds % 60),
|
|
|
|
'milliseconds': int(milliseconds % 1000)}
|
2018-11-19 00:59:17 +11:00
|
|
|
|
|
|
|
|
|
|
|
def kodi_time_to_millis(time):
|
|
|
|
"""
|
|
|
|
Converts the Kodi time dict
|
|
|
|
{
|
|
|
|
'hours': [int],
|
|
|
|
'minutes': [int],
|
|
|
|
'seconds'[int],
|
|
|
|
'milliseconds': [int]
|
|
|
|
}
|
|
|
|
to milliseconds [int]. Will not return negative results but 0!
|
|
|
|
"""
|
|
|
|
ret = (time['hours'] * 3600 +
|
|
|
|
time['minutes'] * 60 +
|
|
|
|
time['seconds']) * 1000 + time['milliseconds']
|
|
|
|
return 0 if ret < 0 else ret
|