From c31de9e2043900a27ccfb130c57f801dc0d91e85 Mon Sep 17 00:00:00 2001 From: ax-6 Date: Thu, 8 Aug 2024 13:27:47 +0800 Subject: [PATCH] Updated the time library code regarding time zone offsets. Updated the formatting of the related one-line if statements. --- aqvm/base/io/io.c | 17 +++------- aqvm/base/logging/logging.c | 19 ++++------- aqvm/base/threading/mutex/mutex.c | 4 +++ aqvm/base/time/time.c | 55 +++++++++++++------------------ aqvm/base/time/windows/time.c | 4 +-- 5 files changed, 40 insertions(+), 59 deletions(-) diff --git a/aqvm/base/io/io.c b/aqvm/base/io/io.c index f94dbe4..2441423 100644 --- a/aqvm/base/io/io.c +++ b/aqvm/base/io/io.c @@ -75,34 +75,26 @@ int AqvmBaseIo_OutputLog(struct AqvmBaseFile_File* stream, const char* time, char* name = va_arg(system_info, char*); char* value = NULL; - if (name != NULL) { - value = va_arg(system_info, char*); - } + if (name != NULL) value = va_arg(system_info, char*); while (name != NULL && value != NULL) { if (fprintf(stream->file, ",\"%s\":\"%s\"", name, value) != 0) { // TODO return -4; } name = va_arg(system_info, char*); - if (name != NULL) { - value = va_arg(system_info, char*); - } + if (name != NULL) value = va_arg(system_info, char*); } name = va_arg(other_info, char*); value = NULL; - if (name != NULL) { - value = va_arg(system_info, char*); - } + if (name != NULL) value = va_arg(system_info, char*); while (name != NULL && value != NULL) { if (fprintf(stream->file, ",\"%s\":\"%s\"", name, value) != 0) { // TODO return -5; } name = va_arg(other_info, char*); - if (name != NULL) { - value = va_arg(system_info, char*); - } + if (name != NULL) value = va_arg(system_info, char*); } if (fprintf(stream->file, "}\n") < 0) { @@ -657,6 +649,7 @@ int AqvmBaseIo_vfprintf(struct AqvmBaseFile_File* stream, const char* format, int AqvmBaseIo_vprintf(const char* format, va_list arg) { if (format == NULL || AqvmBaseFile_CheckStream(AqvmBaseIo_stdout) != 0) { + // TODO return -1; } diff --git a/aqvm/base/logging/logging.c b/aqvm/base/logging/logging.c index 2c87499..5cb6d9d 100644 --- a/aqvm/base/logging/logging.c +++ b/aqvm/base/logging/logging.c @@ -17,7 +17,7 @@ void AqvmBaseLogging_OutputLog(const char* type, const char* code, const char* message, ...) { - char time_str[29]; + char time_str[31]; if (AqvmBaseTime_GetCurrentTimeString(time_str) != 0) { // TODO return; @@ -32,18 +32,10 @@ void AqvmBaseLogging_OutputLog(const char* type, const char* code, void AqvmBaseLogging_ProcessLog(const char* time, const char* type, const char* code, const char* message, va_list other_info, ...) { - if (time == NULL) { - time = "NULL"; - } - if (type == NULL) { - type = "NULL"; - } - if (code == NULL) { - code = "NULL"; - } - if (message == NULL) { - message = "NULL"; - } + if (time == NULL) time = "NULL"; + if (type == NULL) type = "NULL"; + if (code == NULL) code = "NULL"; + if (message == NULL) message = "NULL"; va_list system_info; va_start(system_info, other_info); @@ -84,6 +76,7 @@ int AqvmBaseLogging_OutputLogToFile(const char* time, const char* type, va_list system_info, va_list other_info) { struct AqvmBaseFile_File* log_ptr = AqvmBaseFile_fopen(".aqvm_log.log", "a"); if (log_ptr == NULL) { + // TODO return -1; } diff --git a/aqvm/base/threading/mutex/mutex.c b/aqvm/base/threading/mutex/mutex.c index eb73f32..611105f 100644 --- a/aqvm/base/threading/mutex/mutex.c +++ b/aqvm/base/threading/mutex/mutex.c @@ -13,11 +13,13 @@ int AqvmBaseThreadingMutex_InitializeMutex( #ifdef __unix__ if (AqvmBaseThreadingMutexUnix_InitializeMutex(mutex) != 0) { + // TODO return -2; } return 0; #elif _WIN32 if (AqvmBaseThreadingMutexWindows_InitializeMutex(mutex) != 0) { + // TODO return -2; } return 0; @@ -60,11 +62,13 @@ int AqvmBaseThreadingMutex_LockMutex(AqvmBaseThreadingMutex_Mutex* mutex) { #ifdef __unix__ if (AqvmBaseThreadingMutexUnix_LockMutex(mutex) != 0) { + // TODO return -2; } return 0; #elif _WIN32 if (AqvmBaseThreadingMutexWindows_LockMutex(mutex) != 0) { + // TODO return -2; } return 0; diff --git a/aqvm/base/time/time.c b/aqvm/base/time/time.c index 99bc0f8..53eb827 100644 --- a/aqvm/base/time/time.c +++ b/aqvm/base/time/time.c @@ -4,7 +4,6 @@ #include "aqvm/base/time/time.h" -#include #include #include "aqvm/base/io/io.h" @@ -50,25 +49,20 @@ int AqvmBaseTime_localtime(const time_t timestamp, // TODO return -5; } - - int offset_hour = result->hour - utc_time.hour; - int offset_minute = result->minute - utc_time.minute; - if (offset_hour > 0 || (offset_hour == 0 && offset_minute > 0)) { - result->offset_sign = 1; - } - if (offset_hour < 0 || (offset_hour == 0 && offset_minute < 0)) { - result->offset_sign = -1; - } - if (offset_hour > 0 && offset_minute < 0) { - offset_minute += 60; - --offset_hour; - } - if (offset_hour < 0 && offset_minute > 0) { - offset_minute -= 60; - ++offset_hour; + int timezone_offset = (result->hour - utc_time.hour) * 3600 + + (result->minute - utc_time.minute) * 60 + + (result->second - utc_time.second); + if (result->day != utc_time.day) + timezone_offset += result->day > utc_time.day ? 24 * 3600 : -24 * 3600; + if (timezone_offset == 0) { + result->offset_sign = 0; + } else { + int offset_hour = timezone_offset / 3600; + int offset_minute = timezone_offset % 3600 / 60; + result->offset_sign = timezone_offset > 0 ? 1 : -1; + result->offset_hour = offset_hour >= 0 ? offset_hour : -offset_hour; + result->offset_minute = offset_minute >= 0 ? offset_minute : -offset_minute; } - result->offset_hour = abs(offset_hour); - result->offset_minute = abs(offset_minute); return 0; } @@ -97,6 +91,7 @@ int AqvmBaseTime_gmtime(const time_t timestamp, "The gmtime function may cause thread unsafety.", NULL); struct tm* gm_time = gmtime(×tamp); if (gm_time == NULL) { + // TODO return -4; } *result = *gm_time; @@ -199,7 +194,7 @@ int AqvmBaseTime_GetCurrentTimeString(char* result) { // TODO return -3; } - char timezone_offset_string[6]; + char timezone_offset_string[7]; if (AqvmBaseTime_GetTimezoneOffsetString(¤t_time, timezone_offset_string) != 0) { // TODO @@ -207,7 +202,7 @@ int AqvmBaseTime_GetCurrentTimeString(char* result) { } if (current_time.year < 0) { if (AqvmBaseIo_snprintf( - result, 30, "-%04d-%02d-%02dT%02d:%02d:%02d.%03d%s", + result, 31, "-%04d-%02d-%02dT%02d:%02d:%02d.%03d%s", current_time.year, current_time.month, current_time.day, current_time.hour, current_time.minute, current_time.second, current_time.millisecond, timezone_offset_string) < 0) { @@ -216,7 +211,7 @@ int AqvmBaseTime_GetCurrentTimeString(char* result) { } } else { if (AqvmBaseIo_snprintf( - result, 29, "%04d-%02d-%02dT%02d:%02d:%02d.%03d%s", + result, 30, "%04d-%02d-%02dT%02d:%02d:%02d.%03d%s", current_time.year, current_time.month, current_time.day, current_time.hour, current_time.minute, current_time.second, current_time.millisecond, timezone_offset_string) < 0) { @@ -235,18 +230,18 @@ int AqvmBaseTime_GetTimezoneOffsetString( } if (time_info->offset_sign == 0) { - if (AqvmBaseIo_snprintf(result, 1, "Z") < 0) { + if (AqvmBaseIo_snprintf(result, 2, "Z") < 0) { // TODO return -2; } } else if (time_info->offset_sign > 0) { - if (AqvmBaseIo_snprintf(result, 6, "+%02d:%02d", time_info->offset_hour, + if (AqvmBaseIo_snprintf(result, 7, "+%02d:%02d", time_info->offset_hour, time_info->offset_minute) < 0) { // TODO return -3; } } else if (time_info->offset_sign < 0) { - if (AqvmBaseIo_snprintf(result, 6, "-%02d:%02d", time_info->offset_hour, + if (AqvmBaseIo_snprintf(result, 7, "-%02d:%02d", time_info->offset_hour, time_info->offset_minute) < 0) { // TODO return -4; @@ -376,9 +371,7 @@ int AqvmBaseTime_SetWeekday(struct AqvmBaseTime_Time* time_info) { return -2; } - if (y < 0) { - ++y; - } + if (y < 0) ++y; if (m < 3) { m += 12; --y; @@ -387,9 +380,8 @@ int AqvmBaseTime_SetWeekday(struct AqvmBaseTime_Time* time_info) { if (time_info->year < 1582 || (time_info->year == 1582 && time_info->month < 10) || (time_info->year == 1582 && time_info->month == 10 && - time_info->day <= 4)) { + time_info->day <= 4)) time_info->weekday = (-c + y + y / 4 + 13 * (m + 1) / 5 + d + 4) % 7; - } time_info->weekday = (c / 4 - 2 * c + y + y / 4 + 13 * (m + 1) / 5 + d - 1) % 7; @@ -405,8 +397,7 @@ int AqvmBaseTime_SetYearday(struct AqvmBaseTime_Time* time_info) { int days[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; time_info->yearday = days[time_info->month - 1] + time_info->day; - if (time_info->month > 2 && AqvmBaseTime_IsLeapYear(time_info)) { + if (time_info->month > 2 && AqvmBaseTime_IsLeapYear(time_info)) ++time_info->yearday; - } return 0; } \ No newline at end of file diff --git a/aqvm/base/time/windows/time.c b/aqvm/base/time/windows/time.c index 4c85379..05ae191 100644 --- a/aqvm/base/time/windows/time.c +++ b/aqvm/base/time/windows/time.c @@ -98,11 +98,11 @@ int AqvmBaseTimeWindows_GetCurrentTime(struct AqvmBaseTime_Time* result) { } if (AqvmBaseTime_SetIsdst(result) != 0) { // TODO - // return -5; + return -5; } if (AqvmBaseTime_SetTimezoneOffset(result) != 0) { // TODO - // return -6; + return -6; } return 0;