From 4c4ecfe245494dd0dd67081fa4869375b6886805 Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 2 Feb 2021 14:59:24 -0500 Subject: [PATCH] Dates, when read in from IVY are coming across as EST, and they must be ingested as such. --- communicator/services/ivy_service.py | 3 +++ tests/services/test_ivy_service.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/communicator/services/ivy_service.py b/communicator/services/ivy_service.py index c61c96d..945d1eb 100644 --- a/communicator/services/ivy_service.py +++ b/communicator/services/ivy_service.py @@ -3,6 +3,7 @@ from datetime import datetime from parser import ParserError import globus_sdk +import pytz import sentry_sdk from dateutil import parser @@ -70,6 +71,8 @@ class IvyService(object): try: try: sample.date = parser.parse(dictionary["Test Date Time"]) + tz = pytz.timezone("America/New_York") + sample.date = tz.localize(sample.date) except Exception as pe: sentry_sdk.capture_message(f"Failed to parse date for barcode '{dictionary['Test Bar Code']}', '{pe}'") sample.date = datetime.now() diff --git a/tests/services/test_ivy_service.py b/tests/services/test_ivy_service.py index 8e49625..0161641 100644 --- a/tests/services/test_ivy_service.py +++ b/tests/services/test_ivy_service.py @@ -1,3 +1,7 @@ +import datetime + +import pytz + from tests.base_test import BaseTest import os import unittest @@ -31,6 +35,22 @@ class IvyServiceTest(BaseTest): self.assertEquals(4, len(records)) self.assertEquals('987655321-TN-20212719-4321', records[2].barcode) + def test_timezone_offset(self): + """The date and time returned from the lab / Globus is in EST, be sure to save it as such to + avoid a 5 hour offset, when it is assumed to be in GMT.""" + records = IvyService.samples_from_ivy_file(self.ivy_file) + self.assertEqual("987654321", records[0].student_id) + self.assertIsNotNone(records[0].date.tzinfo, "on ingestion, the date should be in EST") + + # original date "202009030809" + date_string = '202009031209' # UTC is 4 hours head for this date + date = datetime.datetime.strptime(date_string, '%Y%m%d%H%M') + + db.session.add(records[0]) + db.session.commit() + self.assertEqual(date, records[0].date) + + def test_load_directory(self): self.assertEqual(0, db.session.query(IvyFile).count()) app.config['IVY_IMPORT_DIR'] = os.path.join(app.root_path, '..', 'tests', 'data', 'import_directory')