summaryrefslogtreecommitdiff
path: root/time.c
diff options
context:
space:
mode:
authornagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-12-18 13:41:48 +0000
committernagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-12-18 13:41:48 +0000
commit650c45dcd70e791747c4b9084c100cb895e718cb (patch)
tree2de79112693d0cdb04be61443457fa5b66d69503 /time.c
parentfd3c731a31bfed559af8bd9e4ca394632bedf05a (diff)
merge revision(s) d6a2bce64a7fa1099e507e1d36b5f1533f42f60f,c687be4bc01c9ce52ea990945d9304d6fe59fe9b: [Backport #16159]
time.c (find_time_t): fix round-to-zero bug `find_time_t` did not work correctly for year older than the Epoch because it used C's integer division (which rounds negative to zero). For example, `TIme.new(1933)` returned a wrong time whose year is 1922 in Asia/Kuala_Lumpur because there is no 00:00:00 1st Jan. 1933 in the time zone. ``` $ TZ=Asia/Kuala_Lumpur ruby -e 'p Time.new(1933)' 1932-12-31 00:00:00 +0700 ``` This change fixes the issue by using `DIV` macro instead of `/`. Now `Time.new(1933)` returns a time in 1933. ``` $ TZ=Asia/Kuala_Lumpur ruby -e 'p Time.new(1933)' 1933-01-01 00:20:00 +0720 ``` [Bug #16159] Added a test for [Bug #16159] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_6@67836 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'time.c')
-rw-r--r--time.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/time.c b/time.c
index 3bbfce6b38..75db5540c9 100644
--- a/time.c
+++ b/time.c
@@ -3273,12 +3273,12 @@ find_time_t(struct tm *tptr, int utc_p, time_t *tp)
*tp = guess_lo +
((tptr->tm_year - tm_lo.tm_year) * 365 +
- ((tptr->tm_year-69)/4) -
- ((tptr->tm_year-1)/100) +
- ((tptr->tm_year+299)/400) -
- ((tm_lo.tm_year-69)/4) +
- ((tm_lo.tm_year-1)/100) -
- ((tm_lo.tm_year+299)/400) +
+ DIV((tptr->tm_year-69), 4) -
+ DIV((tptr->tm_year-1), 100) +
+ DIV((tptr->tm_year+299), 400) -
+ DIV((tm_lo.tm_year-69), 4) +
+ DIV((tm_lo.tm_year-1), 100) -
+ DIV((tm_lo.tm_year+299), 400) +
tptr_tm_yday -
tm_lo.tm_yday) * 86400 +
(tptr->tm_hour - tm_lo.tm_hour) * 3600 +