mruby-time: Fix mruby specific timegm() cannot return minus
MODIFY
mrbgems/mruby-time/src/time.c
mrbgems/mruby-time/src/time.c
@@ -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)
+ 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;

MODIFY
mrbgems/mruby-time/test/time.rb
mrbgems/mruby-time/test/time.rb
@@ -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
\ No newline at end of file

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