omit microseconds from file name

This commit is contained in:
2024-10-20 19:45:09 +02:00
parent 40ad69eccd
commit 8691032665
2 changed files with 8 additions and 5 deletions
+3 -3
View File
@@ -55,7 +55,7 @@ def main():
record_id = 0
record_list = None
_date_last = None
initial_timestamp = None
file_timestamp = None
vehicle_data_last = None
# Collecting loop
@@ -68,7 +68,7 @@ def main():
_date_last = _date
record_id = 0
record_list = []
initial_timestamp = create_timestamp().replace(':', '-').replace('.', '-')
file_timestamp = create_timestamp(with_ms=False).replace(':', '-').replace('.', '-')
vehicle_data_last = {}
try:
@@ -85,7 +85,7 @@ def main():
# Store record list
print(f"{timestamp}: Store record #{record_id:04d}")
folder = f_mgr.update(f"results/{vin}", _date)
filename = f"vehicle_{vin}_{initial_timestamp}.json"
filename = f"vehicle_{vin}_{file_timestamp}.json"
_path = os.path.join(folder, filename)
with open(_path, "w") as fp:
json.dump(record_list, fp, indent=4)
+5 -2
View File
@@ -25,12 +25,15 @@ def to_date_str(_date: dict):
return f"{_date['year']:04d}-{_date['month']:02d}-{_date['day']:02d}"
def create_timestamp():
def create_timestamp(with_ms=True):
if sys.version_info.major > 2 and sys.version_info.minor > 9:
now = datetime.datetime.now(datetime.UTC)
else:
now = datetime.datetime.utcnow()
res = now.strftime('%Y-%m-%dT%H:%M:%S') + ('.%03dZ' % (now.microsecond / 1000))
res = now.strftime('%Y-%m-%dT%H:%M:%S')
if with_ms:
res += ('.%03dZ' % (now.microsecond / 1000))
return res