Skip to content

Add Duration, and fix Time#shift at the end of a month - #355

Merged
trans merged 1 commit into
mainfrom
feature/duration
Nov 29, 2026
Merged

trans merged 1 commit into
mainfrom
feature/duration

Conversation

@trans

@trans trans commented Nov 28, 2026

Copy link
Copy Markdown
Member

Duration

A standard library, loaded with require 'facets/duration' and not by require 'facets'. It is Facets' own answer to ActiveSupport's 2.hours and 3.days.ago.

2.hours                          #=> 2 hours
1.day + 12.hours                 #=> 1 day and 12 hours
3.days.ago
Time.now + 90.minutes
1.month.since(Time.utc(2026, 1, 31))   #=> 2026-02-28 00:00:00 UTC
100000.seconds.strftime('%h:%02m:%02s') #=> "27:46:40"
  • Number methods: seconds, minutes, hours, days, weeks, fortnights, months, years, and their singulars, on Numeric.
  • Calendar: months and years go by the calendar, clamped to the end of a short month. Days and weeks keep the time of day across a clock change. Hours, minutes and seconds are exact.
  • Times and dates: since, ago and their aliases (from_now, after, before, until) take a Time, Date or DateTime. A time keeps its zone or UTC offset.
  • As a number: a duration converts to and compares by its number of seconds; sleep 2.seconds works. Arithmetic keeps the parts (1.month + 1.day), and 2 * 3.hours works with the number first.
  • Breaking down: to_h, to_a, in_hours and friends, and strftime, 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.month would work but add 2629746 seconds, where 1.month.since goes by the calendar. Date#+ is not extended; use 3.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, #in and #less use shift, 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

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>
@trans
trans merged commit 9aba1f8 into main Nov 29, 2026
16 checks passed
@trans
trans deleted the feature/duration branch November 29, 2026 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant