mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-08-08 22:36:48 +02:00
use jc.utils.parse_datetime_to_timestamp() function for timestamp creation
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
# jc.parsers.upower
|
||||
jc - JSON CLI output utility `upower` 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 `upower` command output.
|
||||
The `updated_epoch` calculated timestamp field is naive (i.e. based on the local time of the system the parser is run on)
|
||||
|
||||
The `updated_epoch_utc` calculated timestamp field is timezone-aware and is only available if the timezone field is UTC.
|
||||
|
||||
Usage (cli):
|
||||
|
||||
@@ -158,7 +160,8 @@ Returns:
|
||||
"native_path": string,
|
||||
"power_supply": boolean,
|
||||
"updated": string,
|
||||
"updated_epoch": integer, # works best with C locale. null if conversion fails
|
||||
"updated_epoch": integer, # null if date-time conversion fails
|
||||
"updated_epoch_utc": integer, # null if date-time conversion fails
|
||||
"updated_seconds_ago": integer,
|
||||
"has_history": boolean,
|
||||
"has_statistics": boolean,
|
||||
|
@@ -1,6 +1,8 @@
|
||||
"""jc - JSON CLI output utility `upower` 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 `upower` command output.
|
||||
The `updated_epoch` calculated timestamp field is naive (i.e. based on the local time of the system the parser is run on)
|
||||
|
||||
The `updated_epoch_utc` calculated timestamp field is timezone-aware and is only available if the timezone field is UTC.
|
||||
|
||||
Usage (cli):
|
||||
|
||||
@@ -127,8 +129,6 @@ Examples:
|
||||
}
|
||||
]
|
||||
"""
|
||||
import locale
|
||||
from datetime import datetime
|
||||
import jc.utils
|
||||
|
||||
|
||||
@@ -142,7 +142,6 @@ class info():
|
||||
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
||||
compatible = ['linux']
|
||||
magic_commands = ['upower']
|
||||
timezone_support = True
|
||||
|
||||
|
||||
__version__ = info.version
|
||||
@@ -167,7 +166,8 @@ def process(proc_data):
|
||||
"native_path": string,
|
||||
"power_supply": boolean,
|
||||
"updated": string,
|
||||
"updated_epoch": integer, # works best with C locale. null if conversion fails
|
||||
"updated_epoch": integer, # null if date-time conversion fails
|
||||
"updated_epoch_utc": integer, # null if date-time conversion fails
|
||||
"updated_seconds_ago": integer,
|
||||
"has_history": boolean,
|
||||
"has_statistics": boolean,
|
||||
@@ -225,24 +225,15 @@ def process(proc_data):
|
||||
updated_list = entry['updated'].replace('(', '').replace(')', '').split()
|
||||
entry['updated'] = ' '.join(updated_list[:-3])
|
||||
entry['updated_seconds_ago'] = int(updated_list[-3])
|
||||
|
||||
# try C locale. If that fails, try current locale. If that fails, give up
|
||||
entry['updated_epoch'] = None
|
||||
try:
|
||||
locale.setlocale(locale.LC_TIME, None)
|
||||
epoch_dt = datetime.strptime(entry['updated'], '%c')
|
||||
entry['updated_epoch'] = int(epoch_dt.strftime('%s'))
|
||||
except Exception:
|
||||
try:
|
||||
locale.setlocale(locale.LC_TIME, '')
|
||||
epoch_dt = datetime.strptime(entry['updated'], '%c')
|
||||
entry['updated_epoch'] = int(epoch_dt.strftime('%s'))
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
locale.setlocale(locale.LC_TIME, None)
|
||||
finally:
|
||||
locale.setlocale(locale.LC_TIME, None)
|
||||
entry['updated_epoch_utc'] = None
|
||||
|
||||
timestamps = jc.utils.parse_datetime_to_timestamp(entry['updated'])
|
||||
if timestamps:
|
||||
if timestamps['timestamp_naive']:
|
||||
entry['updated_epoch'] = timestamps['timestamp_naive']
|
||||
if timestamps['timestamp_utc']:
|
||||
entry['updated_epoch_utc'] = timestamps['timestamp_utc']
|
||||
|
||||
# top level boolean conversions
|
||||
bool_list = ['power_supply', 'has_history', 'has_statistics', 'on_battery', 'lid_is_closed', 'lid_is_present']
|
||||
|
Reference in New Issue
Block a user