77 lines
1.4 KiB
Python
77 lines
1.4 KiB
Python
import time
|
|
import datetime
|
|
import os
|
|
|
|
|
|
def get_date() -> dict:
|
|
_result = {}
|
|
now = time.gmtime()
|
|
_result["year"] = now.tm_year
|
|
_result["month"] = now.tm_mon
|
|
_result["day"] = now.tm_mday
|
|
_result["hour"] = now.tm_hour
|
|
_result["min"] = now.tm_min
|
|
_result["sec"] = now.tm_sec
|
|
|
|
return _result
|
|
|
|
|
|
def to_time_str(_date: dict):
|
|
return f"{_date['hour']:02d}-{_date['min']:02d}-{_date['sec']:02d}"
|
|
|
|
|
|
def to_date_str(_date: dict):
|
|
return f"{_date['year']:04d}-{_date['month']:02d}-{_date['day']:02d}"
|
|
|
|
|
|
def create_timestamp():
|
|
now = datetime.datetime.now(datetime.UTC)
|
|
res = now.strftime('%Y-%m-%dT%H:%M:%S') + ('.%03dZ' % (now.microsecond / 1000))
|
|
return res
|
|
|
|
|
|
def date_has_changed(d1: dict, d2: dict, fields=['year', 'month', 'day']):
|
|
count = 0
|
|
|
|
try:
|
|
for field in fields:
|
|
if d1[field] != d2[field]:
|
|
count += 1
|
|
except KeyError:
|
|
return True
|
|
|
|
return count > 0
|
|
|
|
|
|
class FolderMgr:
|
|
def __init__(self):
|
|
pass
|
|
|
|
@staticmethod
|
|
def update(base_folder: str, _date: dict, fields=['year', 'month', 'day']) -> str:
|
|
|
|
appendix = ''
|
|
for field in fields:
|
|
appendix += f"{_date[field]}/"
|
|
|
|
folder = os.path.join(base_folder, appendix[:-1])
|
|
try:
|
|
os.makedirs(folder)
|
|
except FileExistsError:
|
|
pass
|
|
|
|
return folder
|
|
|
|
|
|
if __name__ == '__main__':
|
|
date = get_date()
|
|
print(date)
|
|
mgr = FolderMgr()
|
|
|
|
for i in range(0, 10):
|
|
folder = mgr.update("./", get_date(), fields=['year', 'month', 'day', 'hour', 'min', 'sec'])
|
|
print(folder)
|
|
time.sleep(1)
|
|
|
|
|