Skip to content
Snippets Groups Projects
Verified Commit 5f098c89 authored by jD91mZM2's avatar jD91mZM2
Browse files

Add more tests for mktime and localtime

parent a9ea2ef6
No related branches found
No related tags found
No related merge requests found
......@@ -291,6 +291,8 @@ pub unsafe extern "C" fn mktime(t: *mut tm) -> time_t {
let mut month = (*t).tm_mon;
let mut day = (*t).tm_mday as i64 - 1;
let leap = if leap_year(year) { 1 } else { 0 };
if year < 1970 {
day = MONTH_DAYS[if leap_year(year) { 1 } else { 0 }][(*t).tm_mon as usize] as i64 - day;
......@@ -299,8 +301,6 @@ pub unsafe extern "C" fn mktime(t: *mut tm) -> time_t {
day += if leap_year(year) { 366 } else { 365 };
}
let leap = if leap_year(year) { 1 } else { 0 };
while month < 11 {
month += 1;
day += MONTH_DAYS[leap][month as usize] as i64;
......@@ -312,15 +312,13 @@ pub unsafe extern "C" fn mktime(t: *mut tm) -> time_t {
+ (*t).tm_sec as i64))
} else {
while year > 1970 {
day += if leap_year(year) { 366 } else { 365 };
year -= 1;
day += if leap_year(year) { 366 } else { 365 };
}
let leap = if leap_year(year) { 1 } else { 0 };
while month > 0 {
day += MONTH_DAYS[leap][month as usize] as i64;
month -= 1;
day += MONTH_DAYS[leap][month as usize] as i64;
}
(day * (60 * 60 * 24)
......
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int check(time_t input) {
struct tm* t = localtime(&input);
printf("%ld = %ld\n", input, mktime(t));
if (input != mktime(t)) {
printf(
"Year %d, Day of year: %d, Month %d, Day of month: %d, Day of week: %d, %d:%d:%d\n",
t->tm_year, t->tm_yday, t->tm_mon, t->tm_mday, t->tm_wday, t->tm_hour, t->tm_min, t->tm_sec
);
puts("Failed!");
return -1;
}
return 0;
}
int main() {
struct tm t = {};
......@@ -12,11 +29,20 @@ int main() {
int day = 60 * 60 * 24;
time_t inputs[] = { -(day * 33), -day, -500, 0, 1531454950 };
for (int i = 0; i < 5; i += 1) {
struct tm* t2 = localtime(&inputs[i]);
if (check(inputs[i])) {
return -1;
}
}
srand(time(NULL));
printf("%ld = %ld\n", inputs[i], mktime(t2));
if (inputs[i] != mktime(t2)) {
puts("Failed!");
for (int i = 0; i < 10; i += 1) {
time_t input = (time_t) rand();
struct tm* time = localtime(&input);
time_t output = mktime(time);
if (input != output) {
// asctime has newline
printf("Comparison %ld == %ld failed. Time: %s", input, output, asctime(time));
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment