2021-10-13 19:36:37 +00:00
|
|
|
from crc.api.common import ApiError
|
2021-10-14 15:02:16 +00:00
|
|
|
from crc.scripts.script import Script
|
|
|
|
|
|
|
|
import dateparser
|
|
|
|
import pytz
|
2021-10-13 19:36:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GetLocaltime(Script):
|
|
|
|
|
|
|
|
def get_description(self):
|
|
|
|
return """Converts a UTC Datetime object into a Datetime object with a different timezone.
|
|
|
|
Defaults to US/Eastern"""
|
|
|
|
|
2021-10-14 15:02:16 +00:00
|
|
|
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
|
|
|
|
if 'timestamp' in kwargs:
|
2021-10-13 19:36:37 +00:00
|
|
|
return True
|
|
|
|
raise ApiError(code='missing_timestamp',
|
|
|
|
message='You must include a timestamp to convert.')
|
|
|
|
|
2021-10-14 15:02:16 +00:00
|
|
|
def do_task(self, task, study_id, workflow_id, *args, **kwargs):
|
|
|
|
if 'timestamp' in kwargs:
|
|
|
|
timestamp = kwargs['timestamp']
|
|
|
|
if 'timezone' in kwargs:
|
|
|
|
timezone = kwargs['timezone']
|
|
|
|
else:
|
|
|
|
timezone = 'US/Eastern'
|
|
|
|
parsed_timestamp = dateparser.parse(timestamp)
|
|
|
|
localtime = parsed_timestamp.astimezone(pytz.timezone(timezone))
|
|
|
|
return str(localtime)
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise ApiError(code='missing_timestamp',
|
|
|
|
message='You must include a timestamp to convert.')
|
2021-10-13 19:36:37 +00:00
|
|
|
|