summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-time
diff options
context:
space:
mode:
authorken-mu <[email protected]>2018-01-20 13:09:58 +0000
committerken-mu <[email protected]>2018-01-20 13:09:58 +0000
commita1f36316d2ff0535d195412a74fae3f501cead41 (patch)
tree87fb5382e75a606100b14e415402a96598a56df9 /mrbgems/mruby-time
parente5fb21b6d281e76ed1aadf488706600739b7f787 (diff)
downloadmruby-a1f36316d2ff0535d195412a74fae3f501cead41.tar.gz
mruby-a1f36316d2ff0535d195412a74fae3f501cead41.zip
mruby-time: Fix mruby specific timegm() cannot return minus
Diffstat (limited to 'mrbgems/mruby-time')
-rw-r--r--mrbgems/mruby-time/src/time.c10
-rw-r--r--mrbgems/mruby-time/test/time.rb16
2 files changed, 24 insertions, 2 deletions
diff --git a/mrbgems/mruby-time/src/time.c b/mrbgems/mruby-time/src/time.c
index e6c6b9904..98198083b 100644
--- a/mrbgems/mruby-time/src/time.c
+++ b/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)
- 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;
diff --git a/mrbgems/mruby-time/test/time.rb b/mrbgems/mruby-time/test/time.rb
index 52b931177..14cd984c6 100644
--- a/mrbgems/mruby-time/test/time.rb
+++ b/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