diff --git a/docgen.sh b/docgen.sh index 66996dfa..96fb2ab3 100755 --- a/docgen.sh +++ b/docgen.sh @@ -62,8 +62,8 @@ pydocmd simple jc.parsers.systemctl_luf+ > ../docs/parsers/systemctl_luf.md pydocmd simple jc.parsers.timedatectl+ > ../docs/parsers/timedatectl.md pydocmd simple jc.parsers.tracepath+ > ../docs/parsers/tracepath.md pydocmd simple jc.parsers.traceroute+ > ../docs/parsers/traceroute.md -pydocmd simple jc.parsers.upower+ > ../docs/parsers/upower.md pydocmd simple jc.parsers.uname+ > ../docs/parsers/uname.md +pydocmd simple jc.parsers.upower+ > ../docs/parsers/upower.md pydocmd simple jc.parsers.uptime+ > ../docs/parsers/uptime.md pydocmd simple jc.parsers.w+ > ../docs/parsers/w.md pydocmd simple jc.parsers.wc+ > ../docs/parsers/wc.md diff --git a/docs/parsers/upower.md b/docs/parsers/upower.md index 2c9cc494..ea546349 100644 --- a/docs/parsers/upower.md +++ b/docs/parsers/upower.md @@ -157,7 +157,7 @@ Returns: "native_path": string, "power_supply": boolean, "updated": string, - "updated_epoch": integer, # works best with C locale. null if conversion fails + "updated_epoch": integer, # as UTC. Works best with C locale. null if conversion fails "updated_seconds_ago": integer, "has_history": boolean, "has_statistics": boolean, diff --git a/jc/parsers/upower.py b/jc/parsers/upower.py index 540fe11b..9a48cce3 100644 --- a/jc/parsers/upower.py +++ b/jc/parsers/upower.py @@ -127,7 +127,7 @@ Examples: ] """ import locale -from datetime import datetime +from datetime import datetime, timezone import jc.utils @@ -141,6 +141,7 @@ class info(): # compatible options: linux, darwin, cygwin, win32, aix, freebsd compatible = ['linux'] magic_commands = ['upower'] + timezone_support = True __version__ = info.version @@ -165,7 +166,7 @@ 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, # as UTC. Works best with C locale. null if conversion fails "updated_seconds_ago": integer, "has_history": boolean, "has_statistics": boolean, @@ -226,13 +227,19 @@ def process(proc_data): # try C locale. If that fails, try current locale. If that fails, give up entry['updated_epoch'] = None try: - entry['updated_epoch'] = int(datetime.strptime(entry['updated'], '%c').strftime('%s')) + locale.setlocale(locale.LC_TIME, None) + entry['updated_epoch'] = int(datetime.strptime(entry['updated'], '%c').astimezone(tz=timezone.utc).strftime('%s')) except Exception: try: locale.setlocale(locale.LC_TIME, '') - entry['updated_epoch'] = int(datetime.strptime(entry['updated'], '%c').strftime('%s')) + entry['updated_epoch'] = int(datetime.strptime(entry['updated'], '%c').astimezone(tz=timezone.utc).strftime('%s')) + except Exception: pass + finally: + locale.setlocale(locale.LC_TIME, None) + finally: + locale.setlocale(locale.LC_TIME, None) entry['updated_seconds_ago'] = int(updated_list[-3])