All Projects → janko → As Duration

janko / As Duration

Licence: mit
Extraction of ActiveSupport::Duration from Rails

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to As Duration

duration-humanizer
361000 becomes "6 minutes, 1 second"
Stars: ✭ 61 (-51.59%)
Mutual labels:  duration, time
Ruby Duration
Immutable type that represents some amount of time with accuracy in seconds.
Stars: ✭ 122 (-3.17%)
Mutual labels:  time, duration
Durafmt
🕗 Better time duration formatting in Go!
Stars: ✭ 362 (+187.3%)
Mutual labels:  time, duration
Swift-ISO8601-DurationParser
Swift ISO8601 Parser
Stars: ✭ 24 (-80.95%)
Mutual labels:  duration, time
Time.dart
⏰ Type-safe DateTime and Duration calculations, powered by extensions.
Stars: ✭ 363 (+188.1%)
Mutual labels:  time, duration
Time
Windows tool for measuring command/program execution speed
Stars: ✭ 21 (-83.33%)
Mutual labels:  time, duration
Humanizeduration.js
361000 becomes "6 minutes, 1 second"
Stars: ✭ 1,234 (+879.37%)
Mutual labels:  time, duration
Time Stamp
Get a formatted timestamp. Used in gulp, assemble, generate, and many others.
Stars: ✭ 104 (-17.46%)
Mutual labels:  time
Hms
A simple class for storing time-of-day values
Stars: ✭ 117 (-7.14%)
Mutual labels:  time
Kairos
A non date-based time calculator
Stars: ✭ 100 (-20.63%)
Mutual labels:  time
Date And Time
A Minimalist DateTime utility for Node.js and the browser
Stars: ✭ 99 (-21.43%)
Mutual labels:  time
Dateutil
Useful extensions to the standard Python datetime features
Stars: ✭ 1,706 (+1253.97%)
Mutual labels:  time
Human time
Ruby time and date comparisons for humans
Stars: ✭ 113 (-10.32%)
Mutual labels:  time
Hyperapp Fx
Effects for use with Hyperapp
Stars: ✭ 105 (-16.67%)
Mutual labels:  time
React Timer Hook
React timer hook
Stars: ✭ 118 (-6.35%)
Mutual labels:  time
Timeoverflow
🏦 ⌛ A time banking system
Stars: ✭ 100 (-20.63%)
Mutual labels:  time
Lora Serialization
LoraWAN serialization/deserialization library for The Things Network
Stars: ✭ 120 (-4.76%)
Mutual labels:  time
Dateparse
GoLang Parse many date strings without knowing format in advance.
Stars: ✭ 1,365 (+983.33%)
Mutual labels:  time
Time
time是一个for typecho相册模板
Stars: ✭ 112 (-11.11%)
Mutual labels:  time
Chrono
Date and time library for Rust
Stars: ✭ 1,780 (+1312.7%)
Mutual labels:  time

AS::Duration

This gem is an extraction of ActiveSupport::Duration from Rails, along with the related core extensions.

Ruby 2.0 or greater is required.

Why not simply use ActiveSupport?

If you're in a Rails project, then you should use ActiveSupport::Duration. Otherwise there are several reason why you might prefer as-duration:

  • You simply don't want to have ActiveSupport as a dependency
  • You want to control what you require. You may think that requiring active_support/core_ext/integer/time will only require what you want, but in fact it will require a total of 5000 LOC (a lot of those are additional core extensions which you may not have wanted). as-duration has only under 200 LOC, and only gives you what you've asked for.

Is it well tested?

It sure is! I copied all the related tests from Rails, and modified them so that they work standalone. So, as-duration passes all of Rails' tests.

Installation

gem 'as-duration'

Features

NOTE: In most cases as-duration should work exactly like ActiveSupport::Duration. However, there are a few modifications made, mostly removing some of the magic, see Modifications.

Numeric methods

The following methods are added on Numeric (Float and Integer):

# plural versions
2.seconds
3.minutes
4.hours
5.days
6.weeks
7.fortnights
8.months
9.years

# singular versions
1.second
1.minute
1.hour
1.day
1.week
1.fortnight
1.month
1.year

The only exception is #months and #years which are only added to Integer (to maintain precision in calculations).

Duration/Time arithmetics

You can add and subtract durations from Time or Date objects.

Time.now + 2.hours
Date.today + 1.year

When you add seconds/minutes/hours to a Date, the Date is automatically converted to a Time object.

(Date.today + 1.minute).class #=> Time

As syntax sugar, you can also call time methods on the duration object:

# forward in time
1.year.from_now
2.months.since(Date.new(2015,4,27))
2.months.after(Date.new(2015,4,27))
2.months.from(Date.new(2015,4,27))

# back in time
2.hours.ago
20.minutes.until(Time.now)
20.minutes.before(Time.now)
20.minutes.to(Time.now)

Duration/Duration arithmetics

You can add and subtract durations:

1.week + 1.day
2.minutes - 1.second

Unlike ActiveSupport::Duration, you can't add durations to integers and vice versa. You either have to convert the integer to a duration, or the duration to an integer with AS::Duration#to_i. This is to help you not to mix different time units.

# Bad
10 + 1.minute  # TypeError
1.minute + 10  # TypeError

# Good
10.seconds + 1.minute  # AS::Duration
1.minute.to_i + 10     # Integer

Modifications to ActiveSupport::Duration

The behaviour of ActiveSupport::Duration has been slightly modified, mostly to remove some magic:

  • Added #from, #after, #before and #to to AS::Duration
  • #from_now and #ago cannot take any arguments, they always use the current time (passing an argument doesn't read well, better to use #from and #until)
  • Removed support for DateTime
    • DateTime was first introduced in Ruby so that you can represent time that the Time class at the moment wasn't able to. However, the Time class improved over time and removed those limitations, so there is no more need to use DateTime
  • Year lasts 365 days instead of 365.25
  • AS::Duration doesn't act like an Integer
    • to compare it with an integer you have to either convert the integer to a duration or convert the duration to an integer (with #to_i)
    • you can only add and subtract two duration objects
  • Removed hash equality

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].