def local_schedule(schedule_or_webdata, tz_name):
if isinstance(schedule_or_webdata, dict):
schedule = schedule_or_webdata['schedule'].split()
is_dict = True
else:
schedule = schedule_or_webdata.split()
is_dict = False
try:
hour = schedule[1]
assert '*' not in hour
except (IndexError, AssertionError):
return schedule_or_webdata
try:
tz = pytz.timezone(tz_name)
except pytz.UnknownTimeZoneError:
return schedule_or_webdata
def to_local(match):
num = int(match.group(0))
now = datetime.utcnow().replace(hour=num, tzinfo=pytz.utc)
return str(tz.normalize(now).hour)
try:
schedule[1] = re.sub(r'(\d+)', to_local, hour)
except ValueError:
return schedule_or_webdata
else:
new_schedule = ' '.join(schedule)
if is_dict:
schedule_or_webdata['schedule'] = new_schedule
return schedule_or_webdata
return new_schedule
评论列表
文章目录