mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
naive updated_epoch timestamp calculation added
This commit is contained in:
@ -1,5 +1,7 @@
|
|||||||
"""jc - JSON CLI output utility `upower` command output parser
|
"""jc - JSON CLI output utility `upower` command output parser
|
||||||
|
|
||||||
|
Calculated epoch time fields are 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 `upower` command output.
|
||||||
|
|
||||||
Usage (cli):
|
Usage (cli):
|
||||||
|
|
||||||
$ upower -d | jc --upower
|
$ upower -d | jc --upower
|
||||||
@ -125,8 +127,8 @@ Examples:
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
"""
|
"""
|
||||||
# import locale
|
import locale
|
||||||
# from datetime import datetime, timezone
|
from datetime import datetime
|
||||||
import jc.utils
|
import jc.utils
|
||||||
|
|
||||||
|
|
||||||
@ -165,6 +167,7 @@ def process(proc_data):
|
|||||||
"native_path": string,
|
"native_path": string,
|
||||||
"power_supply": boolean,
|
"power_supply": boolean,
|
||||||
"updated": string,
|
"updated": string,
|
||||||
|
"updated_epoch": integer, # works best with C locale. null if conversion fails
|
||||||
"updated_seconds_ago": integer,
|
"updated_seconds_ago": integer,
|
||||||
"has_history": boolean,
|
"has_history": boolean,
|
||||||
"has_statistics": boolean,
|
"has_statistics": boolean,
|
||||||
@ -223,38 +226,22 @@ def process(proc_data):
|
|||||||
entry['updated'] = ' '.join(updated_list[:-3])
|
entry['updated'] = ' '.join(updated_list[:-3])
|
||||||
entry['updated_seconds_ago'] = int(updated_list[-3])
|
entry['updated_seconds_ago'] = int(updated_list[-3])
|
||||||
|
|
||||||
# Removing attempted epoch conversions since, depending on locale, timezone info may not be present in output.
|
|
||||||
# May investigate if there is a way to reliably convert the datetime string to an epoch timestamp
|
|
||||||
# if the timezone is present in a future release.
|
|
||||||
#
|
|
||||||
# If we assume current OS timezone, that works for interactive use: upower -d | jc --upower
|
|
||||||
#
|
|
||||||
# But, this wont necessarily convert timestamps correctly if converting text output sourced from another system:
|
|
||||||
# cat upower.out | jc --upower
|
|
||||||
#
|
|
||||||
# In this scenario, we don't always know what timezone was used on the source, so timestamp calculations will
|
|
||||||
# probably be wrong.
|
|
||||||
#
|
|
||||||
# try C locale. If that fails, try current locale. If that fails, give up
|
# try C locale. If that fails, try current locale. If that fails, give up
|
||||||
# entry['updated_epoch'] = None
|
entry['updated_epoch'] = None
|
||||||
# try:
|
try:
|
||||||
# locale.setlocale(locale.LC_TIME, None)
|
locale.setlocale(locale.LC_TIME, None)
|
||||||
# epoch_dt = datetime.strptime(entry['updated'], '%c').astimezone(tz=None)
|
epoch_dt = datetime.strptime(entry['updated'], '%c')
|
||||||
# entry['updated_epoch'] = int(epoch_dt.strftime('%s'))
|
entry['updated_epoch'] = int(epoch_dt.strftime('%s'))
|
||||||
# entry['date_from_timestamp_local'] = datetime.fromtimestamp(entry['updated_epoch']).strftime('%c')
|
except Exception:
|
||||||
# entry['updated_epoch_utc'] = int(epoch_dt.replace(tzinfo=timezone.utc).strftime('%s'))
|
try:
|
||||||
# entry['date_from_timestamp_utc'] = datetime.fromtimestamp(entry['updated_epoch_utc']).strftime('%c')
|
locale.setlocale(locale.LC_TIME, '')
|
||||||
# except Exception:
|
entry['updated_epoch'] = int(epoch_dt.strftime('%s'))
|
||||||
# try:
|
except Exception:
|
||||||
# locale.setlocale(locale.LC_TIME, '')
|
pass
|
||||||
# entry['updated_epoch'] = int(datetime.strptime(entry['updated'], '%c').astimezone(tz=timezone.utc).strftime('%s'))
|
finally:
|
||||||
|
locale.setlocale(locale.LC_TIME, None)
|
||||||
# except Exception:
|
finally:
|
||||||
# pass
|
locale.setlocale(locale.LC_TIME, None)
|
||||||
# finally:
|
|
||||||
# locale.setlocale(locale.LC_TIME, None)
|
|
||||||
# finally:
|
|
||||||
# locale.setlocale(locale.LC_TIME, None)
|
|
||||||
|
|
||||||
# top level boolean conversions
|
# top level boolean conversions
|
||||||
bool_list = ['power_supply', 'has_history', 'has_statistics', 'on_battery', 'lid_is_closed', 'lid_is_present']
|
bool_list = ['power_supply', 'has_history', 'has_statistics', 'on_battery', 'lid_is_closed', 'lid_is_present']
|
||||||
|
Reference in New Issue
Block a user