jackc / tod

Time of day and shift types for Ruby
MIT License
435 stars 56 forks source link

Proposal: Implement Tod::TimeOfDay#upto / Tod::TimeOfDay#step #89

Open asayamakk opened 3 years ago

asayamakk commented 3 years ago

Proposal for new instance methods upto and/or step

These can be used as following cases

Tod::TimeOfDay('10:00').upto(Tod::TimeOfDay('11::00')) do |tod|
  puts tod
end
#=> 10:00::00, 10:00:01, 10:00:02, ...10:59:59, 11:00:00
Tod::TimeOfDay('10:00').step(Tod::TimeOfDay('11::00'), 60) do |tod|
  puts tod
end
#=> 10:00, 10:01, 10:02, ...10:59, 11:00

c.f. https://rubydoc.info/stdlib/date/Date#step-instance_method https://rubydoc.info/stdlib/date/Date#upto-instance_method

before working on implementation, I would like to hear a comment about this methods. Thank you!

jackc commented 3 years ago

I don't have any objection in principle. But in practice you would need to think through what wrapping at midnight means. e.g. 23:59:58, 23:59:59, 00:00:00, 00:00:01. Does it violate any expected contracts of upto or step for the next step to be less than the previous?

I don't think it would be problem, but I haven't thought deeply about it either.