Impl. using localtime timestamps if set in config

This commit is contained in:
Stephen Seo 2023-09-10 21:25:46 +09:00
parent ffc86897d9
commit 46216ebf37

View file

@ -26,6 +26,7 @@ GLOBAL_LOG_FILE = "log.txt"
DEFAULT_EDITOR = "/usr/bin/nano" DEFAULT_EDITOR = "/usr/bin/nano"
IS_DIGIT_REGEX = re.compile("^[0-9]+$") IS_DIGIT_REGEX = re.compile("^[0-9]+$")
STRFTIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" STRFTIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
STRFTIME_LOCAL_FORMAT = "%Y-%m-%dT%H:%M:%S"
class ArchPkgVersion: class ArchPkgVersion:
@ -201,6 +202,26 @@ class ArchPkgVersion:
return self_str return self_str
def timedelta_to_offset_string(timed: datetime.timedelta):
seconds = timed.days * 24 * 60 * 60 + timed.seconds
minutes_offset = int(seconds / 60)
hours_offset = int(minutes_offset / 60)
minutes_offset = minutes_offset - hours_offset * 60
return f"{hours_offset:+03d}:{minutes_offset:02d}"
def get_datetime_timezone_now(other_state):
if other_state["datetime_in_local_time"]:
lt = datetime.datetime.now(datetime.timezone.utc).astimezone()
return lt.strftime(STRFTIME_LOCAL_FORMAT) + timedelta_to_offset_string(
lt.tzinfo.utcoffset(None)
)
else:
return datetime.datetime.now(datetime.timezone.utc).strftime(
STRFTIME_FORMAT
)
def log_print(*args, **kwargs): def log_print(*args, **kwargs):
"""Prints to stdout, then logs to GLOBAL_LOG_FILE.""" """Prints to stdout, then logs to GLOBAL_LOG_FILE."""
@ -209,9 +230,7 @@ def log_print(*args, **kwargs):
and "is_timed" in kwargs["other_state"] and "is_timed" in kwargs["other_state"]
and kwargs["other_state"]["is_timed"] and kwargs["other_state"]["is_timed"]
): ):
t = datetime.datetime.now(datetime.timezone.utc).strftime( t = get_datetime_timezone_now(kwargs["other_state"])
STRFTIME_FORMAT
)
print(t, end=" ") print(t, end=" ")
with open(GLOBAL_LOG_FILE, "a", encoding="utf-8") as lf: with open(GLOBAL_LOG_FILE, "a", encoding="utf-8") as lf:
print(t, end=" ", file=lf) print(t, end=" ", file=lf)
@ -1125,9 +1144,7 @@ def handle_output_stream(
if not limit_reached: if not limit_reached:
if other_state["is_log_timed"]: if other_state["is_log_timed"]:
nowstring = datetime.datetime.now( nowstring = get_datetime_timezone_now(other_state)
datetime.timezone.utc
).strftime(STRFTIME_FORMAT + " ")
line = nowstring + line line = nowstring + line
log_count += len(line) log_count += len(line)
if log_count > other_state["log_limit"]: if log_count > other_state["log_limit"]:
@ -1256,9 +1273,7 @@ def update_pkg_list(
2, f'SCCACHE_CACHE_SIZE={pkg_state[pkg]["sccache_cache_size"]}' 2, f'SCCACHE_CACHE_SIZE={pkg_state[pkg]["sccache_cache_size"]}'
) )
post_command_list.insert(3, "RUSTC_WRAPPER=/usr/bin/sccache") post_command_list.insert(3, "RUSTC_WRAPPER=/usr/bin/sccache")
nowstring = datetime.datetime.now(datetime.timezone.utc).strftime( nowstring = get_datetime_timezone_now(other_state)
STRFTIME_FORMAT
)
if "link_cargo_registry" in pkg_state[pkg]: if "link_cargo_registry" in pkg_state[pkg]:
command_list.insert(2, "-d") command_list.insert(2, "-d")
command_list.insert( command_list.insert(
@ -1738,9 +1753,7 @@ if __name__ == "__main__":
if args_logs_dir is not None: if args_logs_dir is not None:
GLOBAL_LOG_FILE = args_logs_dir + "/update.py_logs" GLOBAL_LOG_FILE = args_logs_dir + "/update.py_logs"
log_print( log_print(
datetime.datetime.now(datetime.timezone.utc).strftime( get_datetime_timezone_now(other_state),
STRFTIME_FORMAT
),
other_state=other_state, other_state=other_state,
) )
log_print( log_print(
@ -1823,9 +1836,7 @@ if __name__ == "__main__":
if other_state["logs_dir"] is not None: if other_state["logs_dir"] is not None:
GLOBAL_LOG_FILE = other_state["logs_dir"] + "/update.py_logs" GLOBAL_LOG_FILE = other_state["logs_dir"] + "/update.py_logs"
log_print( log_print(
datetime.datetime.now(datetime.timezone.utc).strftime( get_datetime_timezone_now(other_state),
STRFTIME_FORMAT
),
other_state=other_state, other_state=other_state,
) )
log_print( log_print(
@ -1891,6 +1902,14 @@ if __name__ == "__main__":
other_state["error_on_limit"] other_state["error_on_limit"]
) )
) )
if (
"datetime_in_local_time" in d
and type(d["datetime_in_local_time"]) is bool
and d["datetime_in_local_time"]
):
other_state["datetime_in_local_time"] = True
else:
other_state["datetime_in_local_time"] = False
else: else:
log_print( log_print(
'ERROR: At least "--config" or "--pkg" must be specified', 'ERROR: At least "--config" or "--pkg" must be specified',