2014-03-10 05:18:05 +00:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# This file is part of SickGear.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is free software: you can redistribute it and/or modify
|
2014-03-10 05:18:05 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is distributed in the hope that it will be useful,
|
2014-03-10 05:18:05 +00:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-11-12 16:43:14 +00:00
|
|
|
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
import datetime
|
|
|
|
import locale
|
|
|
|
import functools
|
2014-12-17 18:25:23 +00:00
|
|
|
import re
|
2016-09-04 20:00:44 +00:00
|
|
|
import time
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
import sickbeard
|
2014-11-05 11:33:05 +00:00
|
|
|
from sickbeard.network_timezones import sb_timezone
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
date_presets = ('%Y-%m-%d',
|
|
|
|
'%a, %Y-%m-%d',
|
|
|
|
'%A, %Y-%m-%d',
|
|
|
|
'%y-%m-%d',
|
|
|
|
'%a, %y-%m-%d',
|
|
|
|
'%A, %y-%m-%d',
|
|
|
|
'%m/%d/%Y',
|
|
|
|
'%a, %m/%d/%Y',
|
|
|
|
'%A, %m/%d/%Y',
|
|
|
|
'%m/%d/%y',
|
|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d, %Y',
|
|
|
|
'%B %d, %Y',
|
|
|
|
'%a, %B %d, %Y',
|
2014-12-17 18:25:23 +00:00
|
|
|
'%A, %B %d, %Y')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
time_presets = ('%I:%M:%S %p',
|
2014-12-17 18:25:23 +00:00
|
|
|
'%I:%M:%S %P',
|
|
|
|
'%H:%M:%S')
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
# helper class
|
|
|
|
class static_or_instance(object):
|
|
|
|
def __init__(self, func):
|
|
|
|
self.func = func
|
|
|
|
|
|
|
|
def __get__(self, instance, owner):
|
|
|
|
return functools.partial(self.func, instance)
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
# subclass datetime.datetime to add function to display custom date and time formats
|
|
|
|
class sbdatetime(datetime.datetime):
|
|
|
|
has_locale = True
|
2014-11-05 11:33:05 +00:00
|
|
|
|
2016-08-14 23:16:35 +00:00
|
|
|
@static_or_instance
|
|
|
|
def is_locale_eng(self):
|
|
|
|
return (sbdatetime.sbdatetime.sbfdate(datetime.datetime.now(), '%A').lower() in [
|
|
|
|
'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'] and
|
|
|
|
sbdatetime.sbdatetime.sbfdate(datetime.datetime.now(), '%B').lower() in [
|
|
|
|
'january', 'february', 'march', 'april', 'may', 'june',
|
|
|
|
'july', 'august', 'september', 'october', 'november', 'december'])
|
|
|
|
|
2014-11-05 11:33:05 +00:00
|
|
|
@static_or_instance
|
|
|
|
def convert_to_setting(self, dt=None):
|
2014-12-17 18:25:23 +00:00
|
|
|
obj = (dt, self)[self is not None]
|
2014-11-05 11:33:05 +00:00
|
|
|
try:
|
2014-12-17 18:25:23 +00:00
|
|
|
if 'local' == sickbeard.TIMEZONE_DISPLAY:
|
|
|
|
return obj.astimezone(sb_timezone)
|
2014-11-05 11:33:05 +00:00
|
|
|
except:
|
2014-12-17 18:25:23 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
return obj
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
@static_or_instance
|
2014-12-17 18:25:23 +00:00
|
|
|
def setlocale(self, setlocale=True, use_has_locale=None, locale_str=''):
|
|
|
|
if setlocale:
|
|
|
|
try:
|
|
|
|
if None is use_has_locale or use_has_locale:
|
|
|
|
locale.setlocale(locale.LC_TIME, locale_str)
|
2016-08-14 23:16:35 +00:00
|
|
|
except locale.Error:
|
2014-12-17 18:25:23 +00:00
|
|
|
if None is not use_has_locale:
|
|
|
|
sbdatetime.has_locale = False
|
|
|
|
pass
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-12-17 18:25:23 +00:00
|
|
|
# display Time in SickGear Format
|
|
|
|
@static_or_instance
|
|
|
|
def sbftime(self, dt=None, show_seconds=False, t_preset=None, setlocale=True, markup=False):
|
2014-05-10 12:02:05 +00:00
|
|
|
|
2014-12-17 18:25:23 +00:00
|
|
|
sbdatetime.setlocale(setlocale=setlocale, use_has_locale=sbdatetime.has_locale, locale_str='us_US')
|
2014-05-18 18:21:18 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
strt = ''
|
2014-12-17 18:25:23 +00:00
|
|
|
|
2016-08-14 23:16:35 +00:00
|
|
|
obj = (dt, self)[self is not None]
|
|
|
|
if None is not obj:
|
|
|
|
tmpl = (((sickbeard.TIME_PRESET, sickbeard.TIME_PRESET_W_SECONDS)[show_seconds]),
|
|
|
|
t_preset)[None is not t_preset]
|
|
|
|
tmpl = (tmpl.replace(':%S', ''), tmpl)[show_seconds]
|
|
|
|
|
|
|
|
pm_token = tmpl.replace('%P', '%p')
|
|
|
|
try:
|
|
|
|
strt = obj.strftime(pm_token)
|
|
|
|
except ValueError:
|
|
|
|
strt = obj.replace(tzinfo=None).strftime(pm_token)
|
2014-12-17 18:25:23 +00:00
|
|
|
|
2016-08-14 23:16:35 +00:00
|
|
|
if sickbeard.TRIM_ZERO:
|
|
|
|
strt = re.sub(r'^0(\d:\d\d)', r'\1', strt)
|
2014-05-18 18:21:18 +00:00
|
|
|
|
2016-08-14 23:16:35 +00:00
|
|
|
if re.search(r'(?im)%p$', tmpl):
|
|
|
|
if '%p' in tmpl:
|
|
|
|
strt = strt.upper()
|
|
|
|
elif '%P' in tmpl:
|
|
|
|
strt = strt.lower()
|
|
|
|
|
|
|
|
if sickbeard.TRIM_ZERO:
|
|
|
|
strt = re.sub(r'(?im)^(\d+)(?::00)?(\s?[ap]m)', r'\1\2', strt)
|
|
|
|
|
|
|
|
if markup:
|
|
|
|
match = re.search(r'(?im)(\d{1,2})(?:(.)(\d\d)(?:(.)(\d\d))?)?(?:\s?([ap]m))?$', strt)
|
|
|
|
if match:
|
|
|
|
strt = ('%s%s%s%s%s%s' % (
|
|
|
|
('<span class="time-hr">%s</span>' % match.group(1), '')[None is match.group(1)],
|
|
|
|
('<span class="time-hr-min">%s</span>' % match.group(2), '')[None is match.group(2)],
|
|
|
|
('<span class="time-min">%s</span>' % match.group(3), '')[None is match.group(3)],
|
|
|
|
('<span class="time-min-sec">%s</span>' % match.group(4), '')[None is match.group(4)],
|
|
|
|
('<span class="time-sec">%s</span>' % match.group(5), '')[None is match.group(5)],
|
|
|
|
('<span class="time-am-pm">%s</span>' % match.group(6), '')[None is match.group(6)]))
|
|
|
|
|
|
|
|
sbdatetime.setlocale(setlocale=setlocale, use_has_locale=sbdatetime.has_locale)
|
|
|
|
return strt
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-11-12 16:43:14 +00:00
|
|
|
# display Date in SickGear Format
|
2014-03-10 05:18:05 +00:00
|
|
|
@static_or_instance
|
2014-12-17 18:25:23 +00:00
|
|
|
def sbfdate(self, dt=None, d_preset=None, setlocale=True):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-12-17 18:25:23 +00:00
|
|
|
sbdatetime.setlocale(setlocale=setlocale)
|
2014-05-10 12:02:05 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
strd = ''
|
|
|
|
try:
|
2014-12-17 18:25:23 +00:00
|
|
|
obj = (dt, self)[self is not None]
|
|
|
|
if None is not obj:
|
2016-08-14 23:16:35 +00:00
|
|
|
preset = (sickbeard.DATE_PRESET, d_preset)[None is not d_preset]
|
|
|
|
try:
|
|
|
|
strd = obj.strftime(preset)
|
|
|
|
except ValueError:
|
|
|
|
strd = obj.replace(tzinfo=None).strftime(preset)
|
2014-05-18 18:21:18 +00:00
|
|
|
|
2014-12-17 18:25:23 +00:00
|
|
|
finally:
|
|
|
|
sbdatetime.setlocale(setlocale=setlocale)
|
2014-03-10 05:18:05 +00:00
|
|
|
return strd
|
|
|
|
|
2014-11-12 16:43:14 +00:00
|
|
|
# display Datetime in SickGear Format
|
2014-03-10 05:18:05 +00:00
|
|
|
@static_or_instance
|
2014-12-17 18:25:23 +00:00
|
|
|
def sbfdatetime(self, dt=None, show_seconds=False, d_preset=None, t_preset=None, markup=False):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-12-17 18:25:23 +00:00
|
|
|
sbdatetime.setlocale()
|
2014-05-10 12:02:05 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
strd = ''
|
2014-12-17 18:25:23 +00:00
|
|
|
obj = (dt, self)[self is not None]
|
2014-03-10 05:18:05 +00:00
|
|
|
try:
|
2014-12-17 18:25:23 +00:00
|
|
|
if None is not obj:
|
2016-08-14 23:16:35 +00:00
|
|
|
preset = (sickbeard.DATE_PRESET, d_preset)[None is not d_preset]
|
|
|
|
try:
|
|
|
|
strd = obj.strftime(preset),
|
|
|
|
except ValueError:
|
|
|
|
strd = obj.replace(tzinfo=None).strftime(preset)
|
|
|
|
strd = u'%s, %s' % (strd, sbdatetime.sbftime(dt, show_seconds, t_preset, False, markup))
|
2014-05-18 18:21:18 +00:00
|
|
|
|
2014-12-17 18:25:23 +00:00
|
|
|
finally:
|
|
|
|
sbdatetime.setlocale(use_has_locale=sbdatetime.has_locale)
|
2014-05-26 21:32:47 +00:00
|
|
|
return strd
|
2016-09-04 20:00:44 +00:00
|
|
|
|
|
|
|
@static_or_instance
|
|
|
|
def totimestamp(self, dt=None, default=None):
|
|
|
|
obj = (dt, self)[self is not None]
|
|
|
|
timestamp = default
|
|
|
|
try:
|
|
|
|
timestamp = time.mktime(obj.timetuple())
|
|
|
|
finally:
|
|
|
|
return (default, timestamp)[isinstance(timestamp, float)]
|