TimeConstants

Time constants, in seconds, so you don’t have to use slow ActiveSupport helpers.

Usage

require 'time_constants'

You get toplevel constants from 0 to 100, except for days, which goes to 500.

T_1_SECOND
T_2_SECONDS

T_1_MINUTE
T_2_MINUTES

T_1_HOUR
T_2_HOURS

T_1_DAY
T_2_DAYS

T_1_WEEK
T_2_WEEKS

T_1_MONTH
T_2_MONTHS

T_1_YEAR
T_2_YEARS

T_1_SOLAR_YEAR
T_2_SOLAR_YEARS

Note that you do not need to refer to Time::.

Benchmark

require 'time_constants'
require 'active_support'

def benchmark(times = 100000, samples = 20)
  times *= samples
  cur = Time.now
  result = times.times { yield }
  print "#{cur = (Time.now - cur) / samples.to_f } seconds"
  puts " (#{(cur / $last_benchmark * 100).to_i - 100}% change)" rescue puts ""
  $last_benchmark = cur
  result
end

> benchmark { T_6_MONTHS }
0.03336355 seconds
> benchmark { 6.months.to_i }
1.65868245 seconds (4871% change)

License

Copyright 2011 Twitter, Inc. Licensed under the Apache 2 license. See the included LICENSE file.