Skip to content

Add "only" option to accumulate time into single units#45

Merged
markets merged 7 commits intomasterfrom
copilot/fix-9b778b89-6ba5-4d67-ba70-f5ae942d8a43
Aug 11, 2025
Merged

Add "only" option to accumulate time into single units#45
markets merged 7 commits intomasterfrom
copilot/fix-9b778b89-6ba5-4d67-ba70-f5ae942d8a43

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Aug 11, 2025

This PR introduces a new only option that allows users to accumulate all time differences into a single specified unit, rather than using the default mixed-unit format.

New Functionality

The only option accepts four values: :years, :months, :weeks, and :days. When specified, it converts the entire time difference into that single unit:

# Before (default behavior)
timeago(Date.today.prev_day(365))
=> "1 year ago"

timeago(Date.today.prev_day(500))
=> "1 year and 4 months ago"

# After (with only option)
timeago(Date.today.prev_day(365), only: :weeks)
=> "52 weeks ago"

timeago(Date.today.prev_day(500), only: :weeks)
=> "71 weeks ago"

timeago(Date.today.prev_day(365), only: :months)
=> "12 months ago"

Integration with Existing Features

The only option works seamlessly with all existing functionality:

# With different styles
timeago(date, only: :weeks, style: :short)
=> "52w ago"

timeago(date, only: :months, style: :array)
=> ["12 months"]

# With localization
timeago(date, only: :weeks, locale: :es)
=> "hace 52 semanas"

# Command line interface (updated to use OptionParser)
timeago 2023-1-1 2024-1-1 --only weeks
=> "52 weeks ago"

Implementation Details

  • Added validation for the only option in the core timeago method
  • Implemented time unit conversion logic with proper rounding
  • Special handling for edge cases (today always returns "today" regardless of only option)
  • Small time periods are rounded up to minimum 1 of the specified unit
  • Updated CLI to use Ruby's OptionParser for improved argument handling
  • Full backward compatibility - all existing behavior is preserved

Testing

  • Added 8 comprehensive test cases covering all units and edge cases
  • All existing 21 tests continue to pass
  • CLI tests validate the new OptionParser integration
  • Total test suite: 29 tests, all passing

Documentation

  • Updated README with detailed examples and usage instructions
  • Added CLI documentation showing the new --only/-o flag with OptionParser
  • Included examples combining only with other options

This change addresses the need for users who want consistent time units in their output, particularly useful for data visualization, APIs, or when specific unit granularity is required.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits August 11, 2025 17:27
…rehensive tests

Co-authored-by: markets <576701+markets@users.noreply.github.com>
Co-authored-by: markets <576701+markets@users.noreply.github.com>
Copilot AI changed the title [WIP] Introduce a new option called "only" with the following allowed values: years, months, weeks, days. For example, when a user calls with "only: :weeks" accumulate all into weeks. Add "only" option to accumulate time into single units Aug 11, 2025
Copilot AI requested a review from markets August 11, 2025 17:29
@markets
Copy link
Copy Markdown
Owner

markets commented Aug 11, 2025

@copilot update from master please, the main change is that in master the CLI now uses OptParser, so update that part.

…only option

Co-authored-by: markets <576701+markets@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Aug 11, 2025

@copilot update from master please, the main change is that in master the CLI now uses OptParser, so update that part.

Updated the CLI to use OptionParser structure from master branch. The --only/-o option now integrates cleanly with the new implementation while maintaining all existing functionality. Commit: 3d6c616

@markets markets marked this pull request as ready for review August 11, 2025 18:47
@markets markets merged commit cf8315e into master Aug 11, 2025
12 checks passed
@markets markets deleted the copilot/fix-9b778b89-6ba5-4d67-ba70-f5ae942d8a43 branch August 11, 2025 19:00
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.

2 participants