Skip to content

feat: add select_relative_time_unit and format_relative_time_from_date#33

Draft
moss-bryophyta wants to merge 1 commit intogeneraltranslation:mainfrom
moss-bryophyta:moss/relative-time-from-date
Draft

feat: add select_relative_time_unit and format_relative_time_from_date#33
moss-bryophyta wants to merge 1 commit intogeneraltranslation:mainfrom
moss-bryophyta:moss/relative-time-from-date

Conversation

@moss-bryophyta
Copy link
Copy Markdown
Contributor

Summary

Adds two new functions to the generaltranslation formatting module, mirroring the JS core library's _selectRelativeTimeUnit and _formatRelativeTimeFromDate:

select_relative_time_unit(date)

Auto-selects the best relative time unit based on the difference between a datetime and now:

  • seconds < 60 → seconds
  • minutes < 60 → minutes
  • hours < 24 → hours
  • days < 7 → days
  • days < 28 → weeks
  • months < 12 → months
  • else → years

Returns a (value, unit) tuple where value is signed (negative = past).

format_relative_time_from_date(date, locales, options)

Combines unit selection with the existing format_relative_time to produce locale-aware strings like "3 hours ago" or "in 2 days" directly from a datetime.

Other changes

  • Version bump: 0.1.0 → 0.2.0
  • 19 new tests (all passing)

Companion to the JS PR on moss/relative-time-component.

Mirrors _selectRelativeTimeUnit and _formatRelativeTimeFromDate from the
JS core library. Auto-selects the best time unit (seconds → years) based
on the difference between a datetime and now, then formats it using Babel.

Bump generaltranslation package version to 0.2.0.
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