diff options
| author | ken-mu <[email protected]> | 2018-01-20 13:09:58 +0000 |
|---|---|---|
| committer | ken-mu <[email protected]> | 2018-01-20 13:09:58 +0000 |
| commit | a1f36316d2ff0535d195412a74fae3f501cead41 (patch) | |
| tree | 87fb5382e75a606100b14e415402a96598a56df9 /mrbgems/mruby-time/src | |
| parent | e5fb21b6d281e76ed1aadf488706600739b7f787 (diff) | |
| download | mruby-a1f36316d2ff0535d195412a74fae3f501cead41.tar.gz mruby-a1f36316d2ff0535d195412a74fae3f501cead41.zip | |
mruby-time: Fix mruby specific timegm() cannot return minus
Diffstat (limited to 'mrbgems/mruby-time/src')
| -rw-r--r-- | mrbgems/mruby-time/src/time.c | 10 |
1 files changed, 8 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; |
