From 156501996688e62dd61a8456206218582a6cbe1b Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Mon, 22 Mar 2021 20:56:57 -0700 Subject: [PATCH] fix weekday numbering to be ISO 8601 compliant. Add naive calculated epoch timestamp field --- EXAMPLES.md | 21 +++---- docs/parsers/date.md | 41 ++++++++------ jc/parsers/date.py | 82 ++++++++++++++++----------- tests/fixtures/generic/date.json | 2 +- tests/fixtures/ubuntu-20.04/date.json | 2 +- 5 files changed, 85 insertions(+), 63 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index aa07a83f..fa8ff4fa 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -464,17 +464,18 @@ date | jc --date -p # or: jc -p date ``` ```json { - "year": 2020, - "month_num": 7, - "day": 31, - "hour": 16, - "minute": 48, - "second": 11, + "year": 2021, + "month_num": 3, + "day": 22, + "hour": 20, + "minute": 54, + "second": 39, "period": null, - "month": "Jul", - "weekday": "Fri", - "weekday_num": 6, - "timezone": "PDT" + "month": "Mar", + "weekday": "Mon", + "weekday_num": 1, + "timezone": "PDT", + "epoch": 1616471679 } ``` ### df diff --git a/docs/parsers/date.md b/docs/parsers/date.md index 5aa75c7c..48079d7f 100644 --- a/docs/parsers/date.md +++ b/docs/parsers/date.md @@ -2,6 +2,8 @@ # jc.parsers.date jc - JSON CLI output utility `date` command output parser +Calculated epoch time field is naive (i.e. based on the local time of the system the parser is run on) since there is no unambiguous timezone information in the `date` command output. + Usage (cli): $ date | jc --date @@ -23,28 +25,30 @@ Examples: $ date | jc --date -p { - "year": 2020, - "month_num": 7, - "day": 31, - "hour": 16, - "minute": 48, - "second": 11, + "year": 2021, + "month_num": 3, + "day": 22, + "hour": 20, + "minute": 47, + "second": 3, "period": null, - "month": "Jul", - "weekday": "Fri", - "weekday_num": 6, - "timezone": "PDT" + "month": "Mar", + "weekday": "Mon", + "weekday_num": 1, + "timezone": "PDT", + "epoch": 1616471223 } + $ date | jc --date -p -r { - "year": "2020", - "month": "Jul", - "day": "31", - "weekday": "Fri", - "hour": "16", - "minute": "50", - "second": "01", + "year": "2021", + "month": "Mar", + "day": "22", + "weekday": "Mon", + "hour": "20", + "minute": "48", + "second": "12", "timezone": "PDT" } @@ -81,7 +85,8 @@ Returns: "month": string, "weekday": string, "weekday_num": integer, - "timezone": string + "timezone": string, + "epoch": integer } diff --git a/jc/parsers/date.py b/jc/parsers/date.py index 8928ea62..1d98acd1 100644 --- a/jc/parsers/date.py +++ b/jc/parsers/date.py @@ -1,5 +1,7 @@ """jc - JSON CLI output utility `date` command output parser +Calculated epoch time field is naive (i.e. based on the local time of the system the parser is run on) since there is no unambiguous timezone information in the `date` command output. + Usage (cli): $ date | jc --date @@ -21,36 +23,39 @@ Examples: $ date | jc --date -p { - "year": 2020, - "month_num": 7, - "day": 31, - "hour": 16, - "minute": 48, - "second": 11, + "year": 2021, + "month_num": 3, + "day": 22, + "hour": 20, + "minute": 47, + "second": 3, "period": null, - "month": "Jul", - "weekday": "Fri", - "weekday_num": 6, - "timezone": "PDT" + "month": "Mar", + "weekday": "Mon", + "weekday_num": 1, + "timezone": "PDT", + "epoch": 1616471223 } + $ date | jc --date -p -r { - "year": "2020", - "month": "Jul", - "day": "31", - "weekday": "Fri", - "hour": "16", - "minute": "50", - "second": "01", + "year": "2021", + "month": "Mar", + "day": "22", + "weekday": "Mon", + "hour": "20", + "minute": "48", + "second": "12", "timezone": "PDT" } """ +from datetime import datetime import jc.utils class info(): - version = '1.1' + version = '1.2' description = 'date command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -86,7 +91,8 @@ def process(proc_data): "month": string, "weekday": string, "weekday_num": integer, - "timezone": string + "timezone": string, + "epoch": integer } """ month_map = { @@ -105,28 +111,38 @@ def process(proc_data): } weekday_map = { - "Sun": 1, - "Mon": 2, - "Tue": 3, - "Wed": 4, - "Thu": 5, - "Fri": 6, - "Sat": 7 + "Mon": 1, + "Tue": 2, + "Wed": 3, + "Thu": 4, + "Fri": 5, + "Sat": 6, + "Sun": 7 } if proc_data: + dt_year = int(proc_data['year']) + dt_month = month_map[proc_data['month']] + dt_day = int(proc_data['day']) + dt_hour = 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) + return { - "year": int(proc_data['year']), - 'month_num': month_map[proc_data['month']], - "day": int(proc_data['day']), - "hour": int(proc_data['hour']), - "minute": int(proc_data['minute']), - "second": int(proc_data['second']), + "year": dt_year, + 'month_num': dt_month, + "day": dt_day, + "hour": dt_hour, + "minute": dt_minute, + "second": dt_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']], - "timezone": proc_data['timezone'] + "timezone": proc_data['timezone'], + "epoch": int(epoch_dt.strftime('%s')) } else: return {} diff --git a/tests/fixtures/generic/date.json b/tests/fixtures/generic/date.json index b507d257..be08e8c2 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": 2, "timezone": "PDT"} +{"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} diff --git a/tests/fixtures/ubuntu-20.04/date.json b/tests/fixtures/ubuntu-20.04/date.json index 478455b2..012f2f51 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": 3, "timezone": "UTC"} +{"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}