Merge pull request #402 from sartography/convert-timestamp-499

Convert timestamp #499
This commit is contained in:
Dan Funk 2021-10-19 09:50:10 -04:00 committed by GitHub
commit 8415ef9776
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,34 @@
from crc.api.common import ApiError
from crc.scripts.script import Script
import dateparser
import pytz
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"""
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, *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.')

View File

@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_0szq8v9" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.2.0">
<bpmn:process id="Process_1dxw783" name="Get Localtime" isExecutable="true">
<bpmn:startEvent id="StartEvent_1">
<bpmn:outgoing>Flow_0lnc9x0</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_0lnc9x0" sourceRef="StartEvent_1" targetRef="Activity_0aq21yg" />
<bpmn:sequenceFlow id="Flow_0gtgzcf" sourceRef="Activity_0aq21yg" targetRef="Activity_1by2ose" />
<bpmn:sequenceFlow id="Flow_0k1hbif" sourceRef="Activity_1by2ose" targetRef="Activity_0d5fjpa" />
<bpmn:endEvent id="Event_1vxo45i">
<bpmn:incoming>Flow_0kgtoh1</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_0kgtoh1" sourceRef="Activity_0d5fjpa" targetRef="Event_1vxo45i" />
<bpmn:scriptTask id="Activity_0aq21yg" name="Send Email">
<bpmn:documentation>This is my email</bpmn:documentation>
<bpmn:incoming>Flow_0lnc9x0</bpmn:incoming>
<bpmn:outgoing>Flow_0gtgzcf</bpmn:outgoing>
<bpmn:script>email_model = email(subject='My Email Subject', recipients='user@example.com')</bpmn:script>
</bpmn:scriptTask>
<bpmn:scriptTask id="Activity_1by2ose" name="Get Localtime">
<bpmn:documentation>timestamp = email_model.timestamp
localtime = get_localtime(str(timestamp))</bpmn:documentation>
<bpmn:incoming>Flow_0gtgzcf</bpmn:incoming>
<bpmn:outgoing>Flow_0k1hbif</bpmn:outgoing>
<bpmn:script>timestamp=email_model.timestamp
localtime = get_localtime(timestamp=timestamp)</bpmn:script>
</bpmn:scriptTask>
<bpmn:manualTask id="Activity_0d5fjpa" name="Display Times">
<bpmn:documentation># Timestamp
{{ timestamp }}
# Localtime
{{ localtime }}</bpmn:documentation>
<bpmn:incoming>Flow_0k1hbif</bpmn:incoming>
<bpmn:outgoing>Flow_0kgtoh1</bpmn:outgoing>
</bpmn:manualTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1dxw783">
<bpmndi:BPMNEdge id="Flow_0lnc9x0_di" bpmnElement="Flow_0lnc9x0">
<di:waypoint x="215" y="117" />
<di:waypoint x="270" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0gtgzcf_di" bpmnElement="Flow_0gtgzcf">
<di:waypoint x="370" y="117" />
<di:waypoint x="430" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0k1hbif_di" bpmnElement="Flow_0k1hbif">
<di:waypoint x="530" y="117" />
<di:waypoint x="590" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0kgtoh1_di" bpmnElement="Flow_0kgtoh1">
<di:waypoint x="690" y="117" />
<di:waypoint x="752" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="99" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_1vxo45i_di" bpmnElement="Event_1vxo45i">
<dc:Bounds x="752" y="99" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_01qg6wo_di" bpmnElement="Activity_0aq21yg">
<dc:Bounds x="270" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0q4ycxr_di" bpmnElement="Activity_1by2ose">
<dc:Bounds x="430" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_178gn50_di" bpmnElement="Activity_0d5fjpa">
<dc:Bounds x="590" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>

View File

@ -0,0 +1,17 @@
from tests.base_test import BaseTest
from crc.scripts.get_localtime import GetLocaltime
class TestGetLocaltime(BaseTest):
def test_get_localtime(self):
self.load_example_data()
workflow = self.create_workflow('get_localtime')
workflow_api = self.get_workflow_api(workflow)
task = workflow_api.next_task
timestamp = task.data['timestamp']
localtime = task.data['localtime']
self.assertEqual(localtime, GetLocaltime().do_task(None, None, None, timestamp=timestamp))