1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

add stat command timestamp detection

This commit is contained in:
Kelly Brazil
2021-03-29 14:45:13 -07:00
parent 8ad164eb34
commit 507999b117
3 changed files with 34 additions and 19 deletions

View File

@ -15,7 +15,7 @@ Parameters:
Returns: Returns:
no return, just prints output to STDERR None - just prints output to STDERR
## error_message ## error_message
@ -31,7 +31,7 @@ Parameters:
Returns: Returns:
no return, just prints output to STDERR None - just prints output to STDERR
## compatibility ## compatibility
@ -50,7 +50,7 @@ Parameters:
Returns: Returns:
no return, just prints output to STDERR None - just prints output to STDERR
## has_data ## has_data
@ -78,23 +78,23 @@ Input a date-time text string of several formats and convert to a naive or timez
Parameters: Parameters:
data: (string) a string representation of a date-time in several supported formats data: (string) a string representation of a date-time in several supported formats
Returns: Returns:
Dict/None A Dictionary of the following format: Dictionary A Dictionary of the following format:
{ {
"format": integer, # for debugging purposes "format": integer, # for debugging purposes. None if conversion fails
"timestamp_naive": integer, # timestamp based on locally configured timezone "timestamp_naive": integer, # timestamp based on locally configured timezone. None if conversion fails
"timestamp_utc": integer # aware timestamp only if UTC timezone detected "timestamp_utc": integer # aware timestamp only if UTC timezone detected. None if conversion fails
} }
The format integer denotes which date_time format conversion succeeded. The format integer denotes which date_time format conversion succeeded.
The timestamp_naive integer is the converted date-time string to a naive epoch timestamp. The timestamp_naive integer is the converted date-time string to a naive epoch timestamp.
The timestamp_utc integer is the converted date-time string to an aware epoch timestamp The timestamp_utc integer is the converted date-time string to an aware epoch timestamp
in the UTC timezone. If an aware conversion cannot be performed (e.g. the UTC timezone in the UTC timezone. If an aware conversion cannot be performed (e.g. the UTC timezone
is not found in the date-time string), then this field will be None. is not found in the date-time string), then this field will be None.
If the conversion completely fails, None is returned instead of a Dictionary If the conversion completely fails, all fields will be None.

View File

@ -1,6 +1,6 @@
"""jc - JSON CLI output utility utils""" """jc - JSON CLI output utility utils"""
import textwrap
import sys import sys
import re
import locale import locale
from datetime import datetime, timezone from datetime import datetime, timezone
@ -52,7 +52,7 @@ def compatibility(mod_name, compatible):
Returns: Returns:
no return, just prints output to STDERR None - just prints output to STDERR
""" """
platform_found = False platform_found = False
@ -109,6 +109,7 @@ def parse_datetime_to_timestamp(data):
If the conversion completely fails, all fields will be None. If the conversion completely fails, all fields will be None.
""" """
normalized_datetime = ''
utc_tz = False utc_tz = False
dt = None dt = None
dt_utc = None dt_utc = None
@ -128,6 +129,8 @@ def parse_datetime_to_timestamp(data):
utc_tz = True utc_tz = True
else: else:
utc_tz = False utc_tz = False
elif '+0000' in data or '-0000' in data:
utc_tz = True
formats = [ formats = [
{'id': 1000, 'format': '%c', 'locale': None}, # C locale format conversion, or date cli command in C locale with non-UTC tz: Tue Mar 23 16:12:11 2021 or Tue Mar 23 16:12:11 IST 2021 {'id': 1000, 'format': '%c', 'locale': None}, # C locale format conversion, or date cli command in C locale with non-UTC tz: Tue Mar 23 16:12:11 2021 or Tue Mar 23 16:12:11 IST 2021
@ -137,6 +140,8 @@ def parse_datetime_to_timestamp(data):
{'id': 5000, 'format': '%A %d %B %Y %I:%M:%S %p', 'locale': None}, # European-style local format with non-UTC tz (found in upower cli output): Tuesday 01 October 2019 12:50:41 PM IST {'id': 5000, 'format': '%A %d %B %Y %I:%M:%S %p', 'locale': None}, # European-style local format with non-UTC tz (found in upower cli output): Tuesday 01 October 2019 12:50:41 PM IST
{'id': 6000, 'format': '%a %b %d %I:%M:%S %p %Z %Y', 'locale': None}, # en_US.UTF-8 format (found in date cli): Wed Mar 24 06:16:19 PM UTC 2021 {'id': 6000, 'format': '%a %b %d %I:%M:%S %p %Z %Y', 'locale': None}, # en_US.UTF-8 format (found in date cli): Wed Mar 24 06:16:19 PM UTC 2021
{'id': 7000, 'format': '%a %b %d %H:%M:%S %Z %Y', 'locale': None}, # C locale format (found in date cli): Wed Mar 24 11:11:30 UTC 2021 {'id': 7000, 'format': '%a %b %d %H:%M:%S %Z %Y', 'locale': None}, # C locale format (found in date cli): Wed Mar 24 11:11:30 UTC 2021
{'id': 7100, 'format': '%b %d %X %Y', 'locale': None}, # C locale format (found in stat cli output - osx): # Mar 29 11:49:05 2021
{'id': 7200, 'format': '%Y-%m-%d %X.%f %z', 'locale': None}, # C locale format (found in stat cli output - linux): 2019-08-13 18:13:43.555604315 -0400
# attempt locale changes last # attempt locale changes last
{'id': 8000, 'format': '%a %d %b %Y %H:%M:%S %Z', 'locale': ''}, # current locale format (found in upower cli output): # mar. 23 mars 2021 23:12:11 UTC {'id': 8000, 'format': '%a %d %b %Y %H:%M:%S %Z', 'locale': ''}, # current locale format (found in upower cli output): # mar. 23 mars 2021 23:12:11 UTC
{'id': 8100, 'format': '%a %d %b %Y %H:%M:%S', 'locale': ''}, # current locale format with non-UTC tz (found in upower cli output): # mar. 23 mars 2021 19:12:11 EDT {'id': 8100, 'format': '%a %d %b %Y %H:%M:%S', 'locale': ''}, # current locale format with non-UTC tz (found in upower cli output): # mar. 23 mars 2021 19:12:11 EDT
@ -179,6 +184,10 @@ def parse_datetime_to_timestamp(data):
normalized_datetime = ' '.join(normalized_datetime_list) normalized_datetime = ' '.join(normalized_datetime_list)
# normalize further by converting any greater-than 6-digit subsecond to 6-digits
p = re.compile(r'(\W\d\d:\d\d:\d\d\.\d{6})\d+\W')
normalized_datetime = p.sub(r'\g<1> ', normalized_datetime)
for fmt in formats: for fmt in formats:
try: try:
locale.setlocale(locale.LC_TIME, fmt['locale']) locale.setlocale(locale.LC_TIME, fmt['locale'])

View File

@ -21,7 +21,13 @@ class MyTests(unittest.TestCase):
# date cli command in en_US.UTF-8 format # date cli command in en_US.UTF-8 format
'Wed Mar 24 06:16:19 PM UTC 2021': {'format': 6000, 'timestamp_naive': 1616634979, 'timestamp_utc': 1616609779}, 'Wed Mar 24 06:16:19 PM UTC 2021': {'format': 6000, 'timestamp_naive': 1616634979, 'timestamp_utc': 1616609779},
# date cli command in C locale format # date cli command in C locale format
'Wed Mar 24 11:11:30 UTC 2021': {'format': 7000, 'timestamp_naive': 1616609490, 'timestamp_utc': 1616584290} 'Wed Mar 24 11:11:30 UTC 2021': {'format': 7000, 'timestamp_naive': 1616609490, 'timestamp_utc': 1616584290},
# C locale format (found in stat cli output - OSX)
'Mar 29 11:49:05 2021': {'format': 7100, 'timestamp_naive': 1617043745, 'timestamp_utc': None},
# C local format (found in stat cli output - linux) non-UTC tz
'2019-08-13 18:13:43.555604315 -0400': {'format': 7200, 'timestamp_naive': 1565745223, 'timestamp_utc': None},
# C local format (found in stat cli output - linux) UTC
'2019-08-13 18:13:43.555604315 -0000': {'format': 7200, 'timestamp_naive': 1565745223, 'timestamp_utc': 1565720023}
} }
for input_string, expected_output in datetime_map.items(): for input_string, expected_output in datetime_map.items():