Repository navigation
Add Duration, and fix Time#shift at the end of a month - #355
Merged
Merged
Conversation
Duration is a standard library, loaded with require 'facets/duration': 2.hours, 3.days.ago, Time.now + 1.month. Months and years go by the calendar, days and weeks keep the time of day, and to_h and strftime break a duration down into units. The names and results are those of ActiveSupport's durations. Time#shift by months or years ran over into the next month when the new month was too short (a month after January 31 was March 3). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Duration
A standard library, loaded with
require 'facets/duration'and not byrequire 'facets'. It is Facets' own answer to ActiveSupport's2.hoursand3.days.ago.seconds,minutes,hours,days,weeks,fortnights,months,years, and their singulars, on Numeric.since,agoand their aliases (from_now,after,before,until) take a Time, Date or DateTime. A time keeps its zone or UTC offset.sleep 2.secondsworks. Arithmetic keeps the parts (1.month + 1.day), and2 * 3.hoursworks with the number first.to_h,to_a,in_hoursand friends, andstrftime, where the units named in the format are the ones the duration is broken down into.ActiveSupport
The names and results are ActiveSupport's. I ran 47 expressions through this and through ActiveSupport 8.1.4.1 (conversions, comparisons,
inspect, month-end and leap-day cases, clock changes in America/New_York, a fixed offset, dates, arithmetic): the results were the same.The Numeric methods are in a module that is included, so where ActiveSupport has defined them on Numeric itself, its own are the ones used, whichever loads first.
Time#+ and Time#-
These are extended with a prepended module that handles a Duration and passes anything else on. Without it,
Time.now + 1.monthwould work but add 2629746 seconds, where1.month.sincegoes by the calendar.Date#+is not extended; use3.days.since(date).Time#shift
Shifting by months or years ran over into the next month when the new month was too short: a month after January 31 was March 3, and a year after February 29 was March 1. It now gives the last day of the month.
Time#ago,#hence,#inand#lessuseshift, so they are fixed too.Tests
58 for Duration and 3 for
shift. The whole suite passes (1637 tests; the 4 pending were pending before).🤖 Generated with Claude Code