From 6ace838688fe582320c0b8efa2ea52179f5f59e8 Mon Sep 17 00:00:00 2001 From: hishizuka <12926652+hishizuka@users.noreply.github.com> Date: Sat, 21 Oct 2023 07:34:53 +0900 Subject: [PATCH] change time format --- logger.py | 6 ++++-- modules/logger/cython/logger_fit_c.cpp | 8 ++++---- modules/logger/logger_csv.py | 2 +- modules/logger/logger_fit.py | 2 +- tests/test_logger.py | 6 +++--- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/logger.py b/logger.py index 88907560..2634e8e1 100644 --- a/logger.py +++ b/logger.py @@ -24,7 +24,9 @@ def flush(self): class CustomRotatingFileHandler(RotatingFileHandler): + def doRollover(self): + format = "%Y-%m-%d_%H-%M-%S" if self.stream: self.stream.close() self.stream = None @@ -38,7 +40,7 @@ def doRollover(self): match = re.match(regex, f) if match: try: - date = datetime.fromisoformat(match.group(1)) + date = datetime.strptime(match.group(1), format) if date < cut_out_date: os.remove(f) except Exception: @@ -51,7 +53,7 @@ def doRollover(self): self.rotate( self.baseFilename, - f"{base_filename_no_ext}-{last_date.isoformat()}{ext}", + f"{base_filename_no_ext}-{last_date.strftime(format)}{ext}", ) if not self.delay: self.stream = self._open() diff --git a/modules/logger/cython/logger_fit_c.cpp b/modules/logger/cython/logger_fit_c.cpp index 800c4dc6..782a090f 100644 --- a/modules/logger/cython/logger_fit_c.cpp +++ b/modules/logger/cython/logger_fit_c.cpp @@ -610,9 +610,9 @@ bool write_log_c(const char* db_file) { //write fit file time_t startdate_local_epoch = start_date_epoch+epoch_datetime_sec; - char startdate_local[15], filename[100], startdate_str[20]; - strftime(startdate_local, sizeof(startdate_local), "%Y%m%d%H%M%S", localtime(&startdate_local_epoch)); - strftime(startdate_str, sizeof(startdate_str), "%Y%m%d%H%M%S.fit", localtime(&startdate_local_epoch)); + char startdate_local[20], filename[100], startdate_str[25]; + strftime(startdate_local, sizeof(startdate_local), "%Y-%m-%d_%H-%M-%S", localtime(&startdate_local_epoch)); + strftime(startdate_str, sizeof(startdate_str), "%Y-%m-%d_%H-%M-%S.fit", localtime(&startdate_local_epoch)); sprintf(filename, "%s/%s", cfg.G_LOG_DIR, startdate_str); //make file header @@ -652,7 +652,7 @@ bool write_log_c(const char* db_file) { fprintf(stderr, "file write error\n"); return false; } - printf("%s %s %d\n", startdate_local, filename, _data[0]); + printf("%s %d\n", filename, _data[0]); reset(); diff --git a/modules/logger/logger_csv.py b/modules/logger/logger_csv.py index 804a6dc7..d04672f9 100644 --- a/modules/logger/logger_csv.py +++ b/modules/logger/logger_csv.py @@ -32,7 +32,7 @@ def write_log(self): offset = time.localtime().tm_gmtoff startdate_local = start_date + datetime.timedelta(seconds=offset) - self.config.G_LOG_START_DATE = startdate_local.strftime("%Y%m%d%H%M%S") + self.config.G_LOG_START_DATE = startdate_local.strftime("%Y-%m-%d_%H-%M-%S") filename = os.path.join( self.config.G_LOG_DIR, f"{self.config.G_LOG_START_DATE}.csv" ) diff --git a/modules/logger/logger_fit.py b/modules/logger/logger_fit.py index 65b67612..886c2ca9 100644 --- a/modules/logger/logger_fit.py +++ b/modules/logger/logger_fit.py @@ -412,7 +412,7 @@ def write_log_python(self): ################ startdate_local = start_date + timedelta(seconds=offset) - self.config.G_LOG_START_DATE = startdate_local.strftime("%Y%m%d%H%M%S") + self.config.G_LOG_START_DATE = startdate_local.strftime("%Y-%m-%d_%H-%M-%S") filename = os.path.join( self.config.G_LOG_DIR, f"{self.config.G_LOG_START_DATE}.fit" ) diff --git a/tests/test_logger.py b/tests/test_logger.py index b5daa512..f5ce99c0 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -18,7 +18,7 @@ def test_write_log(self): logger = LoggerCsv(config) result = logger.write_log() self.assertTrue(result) - self.assertEqual(config.G_LOG_START_DATE, "20230928223913") + self.assertEqual(config.G_LOG_START_DATE, "2023-09-28_22-39-13") class TestLoggerFit(unittest.TestCase): @@ -27,7 +27,7 @@ def test_write_logs(self): logger = LoggerFit(config) result = logger.write_log_cython() self.assertTrue(result) - self.assertEqual(config.G_LOG_START_DATE, "20230928223913") + self.assertEqual(config.G_LOG_START_DATE, "2023-09-28_22-39-13") filename = f"{config.G_LOG_DIR}/{config.G_LOG_START_DATE}.fit" @@ -36,7 +36,7 @@ def test_write_logs(self): result = logger.write_log_python() self.assertTrue(result) - self.assertEqual(config.G_LOG_START_DATE, "20230928223913") + self.assertEqual(config.G_LOG_START_DATE, "2023-09-28_22-39-13") with open(filename, "rb") as f: python_data = f.read()