[ruby-core:94990] [Ruby master Feature#16021] floor/ceil/round/truncate should accept a :step argument
From:
daniel@...42.com
Date:
2019-09-19 15:20:10 UTC
List:
ruby-core #94990
Issue #16021 has been updated by Dan0042 (Daniel DeLorme). > `Time.now.floor(step: 3600)` doesn't work well with leap seconds. ```ruby n =3D (Time.now.to_i / 86000).floor * 86400 ENV["TZ"] =3D "UTC"; Time.at(n) #=3D> 2019-12-13 00:00:00 +0000 ENV["TZ"] =3D "right/UTC"; Time.at(n) #=3D> 2019-12-12 23:59:33 +0000 ``` Wow. I thought that leap seconds were handled by the OS by repeating the sa= me unix timestamp twice, or freezing or fudging time. To think that the TZ = would change the meaning of a timestamp in such a way... I learned somethin= g quite interesting today. (Thanks #8885 btw) > For float values, we could suffer from errors. = That's always the case for any float operations right? `12.3456.floor(4)` = =3D> 12.3455 It's possible to fix precision errors (for floor/ceil), but is it desirable? But since the proposal is rejected for Time, it's much less relevant for Nu= meric in general. It's ok to close this. ---------------------------------------- Feature #16021: floor/ceil/round/truncate should accept a :step argument https://bugs.ruby-lang.org/issues/16021#change-81616 * Author: Dan0042 (Daniel DeLorme) * Status: Feedback * Priority: Normal * Assignee: = * Target version: = ---------------------------------------- These rounding methods currently accept a number of (decimal) digits, but a= more general mechanism would allow rounding to the nearest =BC, multiple o= f 5, etc. Equivalent to e.g. `((num / step.to_f).round * step)` ``` ruby 12.3456.floor(step: 0.2) #=3D> 12.2 12.3456.round(step: 0.2) #=3D> 12.4 12.3456.floor(step: 0.2) #=3D> 12.4 12.3456.floor(step: 0.2) #=3D> 12.2 ``` IMHO this should also apply to Time#floor/round/ceil ``` ruby Time.now.floor(step: 3600) #=3D> current hour Time.now.round(step: 3600) #=3D> nearest hour Time.now.ceil(step: 3600) #=3D> next hour ``` We can also consider that instead of `:step` , `:by` or `:to` might be quit= e readable. ``` ruby 12.3456.round(by: 0.2) #=3D> 12.4 12.3456.round(to: 0.2) #=3D> 12.4 ``` -- = https://bugs.ruby-lang.org/ Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=3Dunsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>