tiny fixes

This commit is contained in:
bumbread 2023-02-16 06:38:31 +11:00
parent dc3bab6eb5
commit 7f97d4f729
1 changed files with 6 additions and 16 deletions

View File

@ -322,6 +322,7 @@ tm_t *gmtime(const time_t *timer) {
timestamp /= 24; timestamp /= 24;
int year = 1970; int year = 1970;
int days = timestamp; int days = timestamp;
int days_since_epoch = days;
// Start subtracting 400,100,4-year-stretches // Start subtracting 400,100,4-year-stretches
const int DAYS_IN_400_YEARS = 365 * 400 + 97; const int DAYS_IN_400_YEARS = 365 * 400 + 97;
const int DAYS_IN_100_YEARS = 365 * 100 + 24; const int DAYS_IN_100_YEARS = 365 * 100 + 24;
@ -332,8 +333,6 @@ tm_t *gmtime(const time_t *timer) {
days %= DAYS_IN_100_YEARS; days %= DAYS_IN_100_YEARS;
year += (days / DAYS_IN_4_YEARS) * 4; year += (days / DAYS_IN_4_YEARS) * 4;
days %= DAYS_IN_4_YEARS; days %= DAYS_IN_4_YEARS;
// Save
int days_since_epoch = days;
// Count remaining up-to 3 years in a loop // Count remaining up-to 3 years in a loop
for(;;) { for(;;) {
int this_year_days = 365 + _is_leap_year(year); int this_year_days = 365 + _is_leap_year(year);
@ -343,9 +342,8 @@ tm_t *gmtime(const time_t *timer) {
year += 1; year += 1;
days -= this_year_days; days -= this_year_days;
} }
// Save
int days_since_year = days;
// Find the current month // Find the current month
int days_since_year = days;
int month = 0; int month = 0;
for(;;) { for(;;) {
int days_in_month = _days_in_month[month] + _is_leap_year(year); int days_in_month = _days_in_month[month] + _is_leap_year(year);
@ -364,23 +362,15 @@ tm_t *gmtime(const time_t *timer) {
return time; return time;
} }
static bool _offset_utc_time_to_local(tm_t *time) { tm_t *localtime(const time_t *timer) {
TIME_ZONE_INFORMATION tz; TIME_ZONE_INFORMATION tz;
if(GetTimeZoneInformation(&tz) == TIME_ZONE_ID_INVALID) { if(GetTimeZoneInformation(&tz) == TIME_ZONE_ID_INVALID) {
return false; return NULL;
} }
int bias = tz.Bias; int bias = tz.Bias;
time->tm_min -= bias; time->tm_min -= bias;
_tm_adjust(time); tm_t *local_time = gmtime(timer);
return true; return local_time;
}
tm_t *localtime(const time_t *timer) {
tm_t *utc_time = gmtime(timer);
if(!_offset_utc_time_to_local(utc_time)) {
return NULL;
}
return utc_time;
} }
// String formatting // String formatting