mruby-time: Fix mruby specific timegm() cannot return minus

This commit is contained in:
ken-mu
2018-01-20 13:09:58 +00:00
parent e5fb21b6d2
commit a1f36316d2
2 changed files with 24 additions and 2 deletions
+8 -2
View File
@@ -138,8 +138,14 @@ timegm(struct tm *tm)
int i;
unsigned int *nday = (unsigned int*) ndays[is_leapyear(tm->tm_year+1900)];
for (i = 70; i < tm->tm_year; ++i)
r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
static const int epoch_year = 70;
if(tm->tm_year >= epoch_year) {
for (i = epoch_year; i < tm->tm_year; ++i)
r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
} else {
for (i = tm->tm_year; i < epoch_year; ++i)
r -= is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
}
for (i = 0; i < tm->tm_mon; ++i)
r += nday[i] * 24 * 60 * 60;
r += (tm->tm_mday - 1) * 24 * 60 * 60;
+16
View File
@@ -226,3 +226,19 @@ assert('2000 times 500us make a second') do
end
t.usec == 0
end
assert('Time.new with Dec 31 23:59:59 1969 raise ArgumentError') do
assert_raise(ArgumentError) {Time.new(1969, 12, 31, 23, 59, 59)}
end
assert('Time.gm with Dec 31 23:59:59 1969 raise ArgumentError') do
assert_raise(ArgumentError) {Time.gm(1969, 12, 31, 23, 59, 59)}
end
assert('Time.new can make less than Dec 31 23:59:58 1969') do
Time.new(1969, 12, 31, 23, 59, 58).year == 1969
end
assert('Time.gm can make less than Dec 31 23:59:58 1969') do
Time.gm(1969, 12, 31, 23, 59, 58).year == 1969
end