mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-17 01:32:37 +02:00
correct epoch_utc calculation. Fix for 12 hour vs. 24 hour representation
This commit is contained in:
@ -2,7 +2,8 @@
|
|||||||
# jc.parsers.date
|
# jc.parsers.date
|
||||||
jc - JSON CLI output utility `date` command output parser
|
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.
|
The `epoch` calculated timestamp field is naive (i.e. based on the local time of the system the parser is run on)
|
||||||
|
The `epoch_utc` calculated timestamp field is timezone-aware and is only available if the timezone field is UTC.
|
||||||
|
|
||||||
Usage (cli):
|
Usage (cli):
|
||||||
|
|
||||||
@ -27,29 +28,29 @@ Examples:
|
|||||||
{
|
{
|
||||||
"year": 2021,
|
"year": 2021,
|
||||||
"month_num": 3,
|
"month_num": 3,
|
||||||
"day": 22,
|
"day": 23,
|
||||||
"hour": 20,
|
"hour": 17,
|
||||||
"minute": 47,
|
"minute": 41,
|
||||||
"second": 3,
|
"second": 44,
|
||||||
"period": null,
|
"period": null,
|
||||||
"month": "Mar",
|
"month": "Mar",
|
||||||
"weekday": "Mon",
|
"weekday": "Tue",
|
||||||
"weekday_num": 1,
|
"weekday_num": 2,
|
||||||
"timezone": "PDT",
|
"timezone": "UTC",
|
||||||
"epoch": 1616471223
|
"epoch": 1616546504,
|
||||||
|
"epoch_utc": 1616571704
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$ date | jc --date -p -r
|
$ date | jc --date -p -r
|
||||||
{
|
{
|
||||||
"year": "2021",
|
"year": "2021",
|
||||||
"month": "Mar",
|
"month": "Mar",
|
||||||
"day": "22",
|
"day": "23",
|
||||||
"weekday": "Mon",
|
"weekday": "Tue",
|
||||||
"hour": "20",
|
"hour": "17",
|
||||||
"minute": "48",
|
"minute": "41",
|
||||||
"second": "12",
|
"second": "44",
|
||||||
"timezone": "PDT"
|
"timezone": "UTC"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -86,7 +87,8 @@ Returns:
|
|||||||
"weekday": string,
|
"weekday": string,
|
||||||
"weekday_num": integer,
|
"weekday_num": integer,
|
||||||
"timezone": string,
|
"timezone": string,
|
||||||
"epoch": integer
|
"epoch": integer, # naive timestamp
|
||||||
|
"epoch_utc": integer, # timezone-aware timestamp. Only available if timezone field is UTC
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""jc - JSON CLI output utility `date` command output parser
|
"""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.
|
The `epoch` calculated timestamp field is naive (i.e. based on the local time of the system the parser is run on)
|
||||||
|
The `epoch_utc` calculated timestamp field is timezone-aware and is only available if the timezone field is UTC.
|
||||||
|
|
||||||
Usage (cli):
|
Usage (cli):
|
||||||
|
|
||||||
@ -25,32 +26,32 @@ Examples:
|
|||||||
{
|
{
|
||||||
"year": 2021,
|
"year": 2021,
|
||||||
"month_num": 3,
|
"month_num": 3,
|
||||||
"day": 22,
|
"day": 23,
|
||||||
"hour": 20,
|
"hour": 17,
|
||||||
"minute": 47,
|
"minute": 41,
|
||||||
"second": 3,
|
"second": 44,
|
||||||
"period": null,
|
"period": null,
|
||||||
"month": "Mar",
|
"month": "Mar",
|
||||||
"weekday": "Mon",
|
"weekday": "Tue",
|
||||||
"weekday_num": 1,
|
"weekday_num": 2,
|
||||||
"timezone": "PDT",
|
"timezone": "UTC",
|
||||||
"epoch": 1616471223
|
"epoch": 1616546504,
|
||||||
|
"epoch_utc": 1616571704
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$ date | jc --date -p -r
|
$ date | jc --date -p -r
|
||||||
{
|
{
|
||||||
"year": "2021",
|
"year": "2021",
|
||||||
"month": "Mar",
|
"month": "Mar",
|
||||||
"day": "22",
|
"day": "23",
|
||||||
"weekday": "Mon",
|
"weekday": "Tue",
|
||||||
"hour": "20",
|
"hour": "17",
|
||||||
"minute": "48",
|
"minute": "41",
|
||||||
"second": "12",
|
"second": "44",
|
||||||
"timezone": "PDT"
|
"timezone": "UTC"
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
from datetime import datetime
|
from datetime import datetime, timezone
|
||||||
import jc.utils
|
import jc.utils
|
||||||
|
|
||||||
|
|
||||||
@ -92,9 +93,11 @@ def process(proc_data):
|
|||||||
"weekday": string,
|
"weekday": string,
|
||||||
"weekday_num": integer,
|
"weekday_num": integer,
|
||||||
"timezone": string,
|
"timezone": string,
|
||||||
"epoch": integer
|
"epoch": integer, # naive timestamp
|
||||||
|
"epoch_utc": integer, # timezone-aware timestamp. Only available if timezone field is UTC
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
# ISO 8601 month numberings
|
||||||
month_map = {
|
month_map = {
|
||||||
"Jan": 1,
|
"Jan": 1,
|
||||||
"Feb": 2,
|
"Feb": 2,
|
||||||
@ -110,6 +113,7 @@ def process(proc_data):
|
|||||||
"Dec": 12
|
"Dec": 12
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ISO 8601 weekday numberings
|
||||||
weekday_map = {
|
weekday_map = {
|
||||||
"Mon": 1,
|
"Mon": 1,
|
||||||
"Tue": 2,
|
"Tue": 2,
|
||||||
@ -124,26 +128,41 @@ def process(proc_data):
|
|||||||
dt_year = int(proc_data['year'])
|
dt_year = int(proc_data['year'])
|
||||||
dt_month = month_map[proc_data['month']]
|
dt_month = month_map[proc_data['month']]
|
||||||
dt_day = int(proc_data['day'])
|
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_minute = int(proc_data['minute'])
|
dt_minute = int(proc_data['minute'])
|
||||||
dt_second = int(proc_data['second'])
|
dt_second = int(proc_data['second'])
|
||||||
|
|
||||||
epoch_dt = datetime(dt_year, dt_month, dt_day, hour=dt_hour, minute=dt_minute, second=dt_second)
|
epoch_dt = datetime(dt_year, dt_month, dt_day, hour=dt_hour, minute=dt_minute, second=dt_second)
|
||||||
|
|
||||||
return {
|
date_obj = {
|
||||||
"year": dt_year,
|
'year': dt_year,
|
||||||
'month_num': dt_month,
|
'month_num': dt_month,
|
||||||
"day": dt_day,
|
'day': dt_day,
|
||||||
"hour": dt_hour,
|
'hour': dt_hour,
|
||||||
"minute": dt_minute,
|
'minute': dt_minute,
|
||||||
"second": dt_second,
|
'second': dt_second,
|
||||||
"period": proc_data['period'] if 'period' in proc_data else None,
|
'period': proc_data['period'] if 'period' in proc_data else None,
|
||||||
"month": proc_data['month'],
|
'month': proc_data['month'],
|
||||||
"weekday": proc_data['weekday'],
|
'weekday': proc_data['weekday'],
|
||||||
"weekday_num": weekday_map[proc_data['weekday']],
|
'weekday_num': weekday_map[proc_data['weekday']],
|
||||||
"timezone": proc_data['timezone'],
|
'timezone': proc_data['timezone'],
|
||||||
"epoch": int(epoch_dt.strftime('%s'))
|
'epoch': int(epoch_dt.timestamp())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
date_obj['epoch_utc'] = int(utc_epoch_dt.timestamp())
|
||||||
|
|
||||||
|
return date_obj
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
1
tests/fixtures/ubuntu-20.04/date2.out
vendored
Normal file
1
tests/fixtures/ubuntu-20.04/date2.out
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
Tue Mar 23 08:45:29 PM UTC 2021
|
Reference in New Issue
Block a user