From 22b461eb4b1ca86ea9e8c1fde8a2c312d1526b5f Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 5 Jan 2021 09:42:05 -0800 Subject: [PATCH] Add period field for en_US.UTF-8 locale --- CHANGELOG | 1 + EXAMPLES.md | 1 + docs/parsers/date.md | 2 ++ jc/parsers/date.py | 40 +++++++++++++++++++-------- tests/fixtures/generic/date.json | 2 +- tests/fixtures/ubuntu-20.04/date.json | 1 + tests/test_date.py | 12 ++++++++ 7 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 tests/fixtures/ubuntu-20.04/date.json diff --git a/CHANGELOG b/CHANGELOG index 84d438ab..d70674d0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,7 @@ jc changelog 20210104 v1.14.1 - Add wi scan parser - Update date parser for support Ubuntu 20.04 +- Update last parser for last -F support - Update man page - Minor documentation updates diff --git a/EXAMPLES.md b/EXAMPLES.md index 34660796..63b14e99 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -412,6 +412,7 @@ date | jc --date -p # or: jc -p date "hour": 16, "minute": 48, "second": 11, + "period": null, "month": "Jul", "weekday": "Fri", "weekday_num": 6, diff --git a/docs/parsers/date.md b/docs/parsers/date.md index a07cc8f3..5aa75c7c 100644 --- a/docs/parsers/date.md +++ b/docs/parsers/date.md @@ -29,6 +29,7 @@ Examples: "hour": 16, "minute": 48, "second": 11, + "period": null, "month": "Jul", "weekday": "Fri", "weekday_num": 6, @@ -76,6 +77,7 @@ Returns: "hour": integer, "minute": integer, "second": integer, + "period": string, "month": string, "weekday": string, "weekday_num": integer, diff --git a/jc/parsers/date.py b/jc/parsers/date.py index f2cf49ca..8928ea62 100644 --- a/jc/parsers/date.py +++ b/jc/parsers/date.py @@ -27,6 +27,7 @@ Examples: "hour": 16, "minute": 48, "second": 11, + "period": null, "month": "Jul", "weekday": "Fri", "weekday_num": 6, @@ -49,7 +50,7 @@ import jc.utils class info(): - version = '1.0' + version = '1.1' description = 'date command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -81,6 +82,7 @@ def process(proc_data): "hour": integer, "minute": integer, "second": integer, + "period": string, "month": string, "weekday": string, "weekday_num": integer, @@ -120,6 +122,7 @@ def process(proc_data): "hour": int(proc_data['hour']), "minute": int(proc_data['minute']), "second": int(proc_data['second']), + "period": proc_data['period'] if 'period' in proc_data else None, "month": proc_data['month'], "weekday": proc_data['weekday'], "weekday_num": weekday_map[proc_data['weekday']], @@ -152,16 +155,31 @@ def parse(data, raw=False, quiet=False): data = data.replace(':', ' ') split_data = data.split() - raw_output = { - "year": split_data[7], - "month": split_data[1], - "day": split_data[2], - "weekday": split_data[0], - "hour": split_data[3], - "minute": split_data[4], - "second": split_data[5], - "timezone": split_data[6] - } + # date v8.32 uses a different format depending on locale, so need to support LANG=en_US.UTF-8 + if len(split_data) == 9 and ('AM' in split_data or 'am' in split_data or 'PM' in split_data or 'pm' in split_data): + raw_output = { + "year": split_data[8], + "month": split_data[1], + "day": split_data[2], + "weekday": split_data[0], + "hour": split_data[3], + "minute": split_data[4], + "second": split_data[5], + "period": split_data[6], + "timezone": split_data[7] + } + else: + # standard LANG=C date output + raw_output = { + "year": split_data[7], + "month": split_data[1], + "day": split_data[2], + "weekday": split_data[0], + "hour": split_data[3], + "minute": split_data[4], + "second": split_data[5], + "timezone": split_data[6] + } if raw: return raw_output diff --git a/tests/fixtures/generic/date.json b/tests/fixtures/generic/date.json index 5e5057ca..b507d257 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, "month": "Aug", "weekday": "Mon", "weekday_num": 2, "timezone": "PDT"} +{"year": 2020, "month_num": 8, "day": 3, "hour": 9, "minute": 12, "second": 51, "period": null, "month": "Aug", "weekday": "Mon", "weekday_num": 2, "timezone": "PDT"} diff --git a/tests/fixtures/ubuntu-20.04/date.json b/tests/fixtures/ubuntu-20.04/date.json new file mode 100644 index 00000000..478455b2 --- /dev/null +++ b/tests/fixtures/ubuntu-20.04/date.json @@ -0,0 +1 @@ +{"year": 2021, "month_num": 1, "day": 5, "hour": 1, "minute": 2, "second": 4, "period": "AM", "month": "Jan", "weekday": "Tue", "weekday_num": 3, "timezone": "UTC"} diff --git a/tests/test_date.py b/tests/test_date.py index 3274a1fd..c13fcc9e 100644 --- a/tests/test_date.py +++ b/tests/test_date.py @@ -13,10 +13,16 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/date.out'), 'r', encoding='utf-8') as f: self.generic_date = f.read() + 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() + # 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()) + 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()) + def test_date_nodata(self): """ Test 'date' with no data @@ -29,6 +35,12 @@ 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): + """ + Test 'date' on Ubuntu 20.4 + """ + self.assertEqual(jc.parsers.date.parse(self.ubuntu_20_04_date, quiet=True), self.ubuntu_20_04_date_json) + if __name__ == '__main__': unittest.main()