Convert UTC datetime object to a different timezone.

JSON doesn't know about dates, so we have to return a string
This commit is contained in:
mike cullerton 2021-10-14 11:02:16 -04:00
parent 1ef063de37
commit c2c79bd014
1 changed files with 20 additions and 5 deletions

View File

@ -1,5 +1,8 @@
from crc.scripts.script import Script
from crc.api.common import ApiError
from crc.scripts.script import Script
import dateparser
import pytz
class GetLocaltime(Script):
@ -8,12 +11,24 @@ class GetLocaltime(Script):
return """Converts a UTC Datetime object into a Datetime object with a different timezone.
Defaults to US/Eastern"""
def do_task_validate_only(self, task, study_id, workflow_id, **kwargs):
if len(kwargs):
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
if 'timestamp' in kwargs:
return True
raise ApiError(code='missing_timestamp',
message='You must include a timestamp to convert.')
def do_task(self, task, study_id, workflow_id, **kwargs):
pass
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.')