1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

Add period field for en_US.UTF-8 locale

This commit is contained in:
Kelly Brazil
2021-01-05 09:42:05 -08:00
parent b37ee8555a
commit 22b461eb4b
7 changed files with 47 additions and 12 deletions

View File

@ -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

View File

@ -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,

View File

@ -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,

View File

@ -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,6 +155,21 @@ def parse(data, raw=False, quiet=False):
data = data.replace(':', ' ')
split_data = data.split()
# 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],

View File

@ -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"}

1
tests/fixtures/ubuntu-20.04/date.json vendored Normal file
View File

@ -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"}

View File

@ -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()