diff --git a/CHANGELOG b/CHANGELOG index c764ef59..8b65a3ec 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,9 @@ jc changelog - Add upower parser tested on linux - Update date parser to make weekday numbering ISO 8601 compliant - Update date parser to add a calculated (naive) epoch timestamp +- Update date parser to add a calculated timezone-aware epoch_utc timestamp if the date output is in UTC +- Update date parser to always set the period value to uppercase AM or PM +- Update date parser to add an hour_24 field to always express the hour in 24-hour format 20210305 v1.14.4 - Packaging fix only for binaries and RPMs hosted on https://github.com/kellyjonbrazil/jc-packaging. diff --git a/jc/parsers/date.py b/jc/parsers/date.py index 3606e7d3..83745c7c 100644 --- a/jc/parsers/date.py +++ b/jc/parsers/date.py @@ -129,24 +129,25 @@ def process(proc_data): dt_year = int(proc_data['year']) dt_month = month_map[proc_data['month']] dt_day = int(proc_data['day']) - - # fix for 12 vs. 24 hour output - if proc_data['period']: - if proc_data['period'].lower() == 'pm': - dt_hour = int(proc_data['hour']) + 12 - else: - dt_hour = int(proc_data['hour']) - + dt_hour = int(proc_data['hour']) + dt_hour_24 = int(proc_data['hour']) dt_minute = int(proc_data['minute']) dt_second = int(proc_data['second']) - epoch_dt = datetime(dt_year, dt_month, dt_day, hour=dt_hour, minute=dt_minute, second=dt_second) + # fix for 12 vs. 24 hour output + if 'period' in proc_data: + if proc_data['period']: + if proc_data['period'].lower() == 'pm': + dt_hour_24 = dt_hour + 12 + + epoch_dt = datetime(dt_year, dt_month, dt_day, hour=dt_hour_24, minute=dt_minute, second=dt_second) date_obj = { 'year': dt_year, 'month_num': dt_month, 'day': dt_day, 'hour': dt_hour, + 'hour_24': dt_hour_24, 'minute': dt_minute, 'second': dt_second, 'period': proc_data['period'].upper() if 'period' in proc_data else None, @@ -159,7 +160,7 @@ def process(proc_data): # create aware datetime object only if the timezone is UTC if proc_data['timezone'] == 'UTC': - utc_epoch_dt = datetime(dt_year, dt_month, dt_day, hour=dt_hour, minute=dt_minute, second=dt_second, tzinfo=timezone.utc) + utc_epoch_dt = datetime(dt_year, dt_month, dt_day, hour=dt_hour_24, minute=dt_minute, second=dt_second, tzinfo=timezone.utc) date_obj['epoch_utc'] = int(utc_epoch_dt.timestamp()) return date_obj diff --git a/tests/fixtures/generic/date.json b/tests/fixtures/generic/date.json index be08e8c2..10a3f91b 100644 --- a/tests/fixtures/generic/date.json +++ b/tests/fixtures/generic/date.json @@ -1 +1 @@ -{"year": 2020, "month_num": 8, "day": 3, "hour": 9, "minute": 12, "second": 51, "period": null, "month": "Aug", "weekday": "Mon", "weekday_num": 1, "timezone": "PDT", "epoch": 1596471171} +{"year": 2020, "month_num": 8, "day": 3, "hour": 9, "hour_24": 9, "minute": 12, "second": 51, "period": null, "month": "Aug", "weekday": "Mon", "weekday_num": 1, "timezone": "PDT", "epoch": 1596471171} diff --git a/tests/fixtures/ubuntu-20.04/date.json b/tests/fixtures/ubuntu-20.04/date.json index 012f2f51..daa42617 100644 --- a/tests/fixtures/ubuntu-20.04/date.json +++ b/tests/fixtures/ubuntu-20.04/date.json @@ -1 +1 @@ -{"year": 2021, "month_num": 1, "day": 5, "hour": 1, "minute": 2, "second": 4, "period": "AM", "month": "Jan", "weekday": "Tue", "weekday_num": 2, "timezone": "UTC", "epoch": 1609837324} +{"year": 2021, "month_num": 1, "day": 5, "hour": 1, "hour_24": 1, "minute": 2, "second": 4, "period": "AM", "month": "Jan", "weekday": "Tue", "weekday_num": 2, "timezone": "UTC", "epoch": 1609837324, "epoch_utc": 1609808524} diff --git a/tests/fixtures/ubuntu-20.04/date2.json b/tests/fixtures/ubuntu-20.04/date2.json new file mode 100644 index 00000000..732e9535 --- /dev/null +++ b/tests/fixtures/ubuntu-20.04/date2.json @@ -0,0 +1 @@ +{"year": 2021, "month_num": 3, "day": 23, "hour": 8, "hour_24": 20, "minute": 45, "second": 29, "period": "PM", "month": "Mar", "weekday": "Tue", "weekday_num": 2, "timezone": "UTC", "epoch": 1616557529, "epoch_utc": 1616532329} diff --git a/tests/localtest_date.py b/tests/localtest_date.py index c13fcc9e..dd9e93b9 100644 --- a/tests/localtest_date.py +++ b/tests/localtest_date.py @@ -16,6 +16,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-20.04/date.out'), 'r', encoding='utf-8') as f: self.ubuntu_20_04_date = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-20.04/date2.out'), 'r', encoding='utf-8') as f: + self.ubuntu_20_04_date2 = f.read() + # output with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/date.json'), 'r', encoding='utf-8') as f: self.generic_date_json = json.loads(f.read()) @@ -23,6 +26,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-20.04/date.json'), 'r', encoding='utf-8') as f: self.ubuntu_20_04_date_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-20.04/date2.json'), 'r', encoding='utf-8') as f: + self.ubuntu_20_04_date2_json = json.loads(f.read()) + def test_date_nodata(self): """ Test 'date' with no data @@ -35,12 +41,18 @@ class MyTests(unittest.TestCase): """ self.assertEqual(jc.parsers.date.parse(self.generic_date, quiet=True), self.generic_date_json) - def test_date_ubuntu_20_04(self): + def test_date_am_ubuntu_20_04(self): """ - Test 'date' on Ubuntu 20.4 + Test 'date' on Ubuntu 20.4 with LANG=en_US.UTF-8 (uses 12-hour clock) with AM time """ self.assertEqual(jc.parsers.date.parse(self.ubuntu_20_04_date, quiet=True), self.ubuntu_20_04_date_json) + def test_date_pm_ubuntu_20_04(self): + """ + Test 'date' on Ubuntu 20.4 with LANG=en_US.UTF-8 (uses 12-hour clock) with PM time + """ + self.assertEqual(jc.parsers.date.parse(self.ubuntu_20_04_date2, quiet=True), self.ubuntu_20_04_date2_json) + if __name__ == '__main__': unittest.main()