NumericPeriodFormat
Period of time (in seconds) formatted to Ruby on Rails.
Installation
Add this line to your application's Gemfile:
gem 'numeric_period_format'
And then execute:
$ bundle
Or install it yourself as:
$ gem install numeric_period_format
Usage
The directives begin with a percent (%) character. Any text not listed as a directive will be passed through to the output string.
The directive consists of a percent (%) character with one or more letters (y, m, d, H, M, S). The numbers of letters are right justified with zeros.
Format directives:
y - Year
m - Month
d - Day
H - Hour
M - Minute
S - Second
> 1.second.to_i.to_formatted_period('%S')
=> "1"
> 1.to_formatted_period('%S')
=> "1"
> 1.to_formatted_period('%SS')
=> "01"
> 1.to_formatted_period('%SSS')
=> "001"
> (1.minute + 1).to_i.to_formatted_period('%S')
=> "61"
> (1.minute + 1).to_i.to_formatted_period('%MM:%SS')
=> "01:01"
> (1.minute + 1).to_i.to_formatted_period('%HH:%MM:%SS')
=> "00:01:01"
> (1.day + 1).to_i.to_formatted_period('%d day - %HH:%MM:%SS')
=> "1 day - 06:00:01"
> (1.year + 1).to_i.to_formatted_period('%y year, %m month, %d day - %HH:%MM:%SS')
=> "1 year, 0 month, 0 day - 06:00:01"
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/numeric_period_format. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.