Skip to content

Commit

Permalink
Updated the time library code regarding time zone offsets. Updated th…
Browse files Browse the repository at this point in the history
…e formatting of the related one-line if statements.
  • Loading branch information
ax-6 committed Aug 8, 2024
1 parent ab72e31 commit c31de9e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 59 deletions.
17 changes: 5 additions & 12 deletions aqvm/base/io/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
19 changes: 6 additions & 13 deletions aqvm/base/logging/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions aqvm/base/threading/mutex/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
55 changes: 23 additions & 32 deletions aqvm/base/time/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "aqvm/base/time/time.h"

#include <stdio.h>
#include <time.h>

#include "aqvm/base/io/io.h"
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -97,6 +91,7 @@ int AqvmBaseTime_gmtime(const time_t timestamp,
"The gmtime function may cause thread unsafety.", NULL);
struct tm* gm_time = gmtime(&timestamp);
if (gm_time == NULL) {
// TODO
return -4;
}
*result = *gm_time;
Expand Down Expand Up @@ -199,15 +194,15 @@ int AqvmBaseTime_GetCurrentTimeString(char* result) {
// TODO
return -3;
}
char timezone_offset_string[6];
char timezone_offset_string[7];
if (AqvmBaseTime_GetTimezoneOffsetString(&current_time,
timezone_offset_string) != 0) {
// TODO
return -4;
}
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) {
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions aqvm/base/time/windows/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c31de9e

Please sign in to comment.