class TwitterCldr::Localized::LocalizedTimespan

Constants

APPROXIMATE_MULTIPLIER
DEFAULT_TYPE
TIME_IN_SECONDS

Public Class Methods

new(seconds, options = {}) click to toggle source
# File lib/twitter_cldr/localized/localized_timespan.rb, line 23
def initialize(seconds, options = {})
  super(seconds, options[:locale] || TwitterCldr.locale, options)
end

Public Instance Methods

to_s(options = {}) click to toggle source
# File lib/twitter_cldr/localized/localized_timespan.rb, line 27
def to_s(options = {})
  unit = options[:unit] || calculate_unit(base_obj.abs, options)
  direction = options[:direction] || (base_obj < 0 ? :ago : :until)
  type = options[:type] || DEFAULT_TYPE
  number = calculate_time(base_obj, unit)

  data_reader = TwitterCldr::DataReaders::TimespanDataReader.new(locale, number, {
    unit: unit,
    direction: direction,
    type: type
  })

  tokens = data_reader.tokenizer.tokenize(data_reader.pattern)
  data_reader.formatter.format(tokens, number, options)
end

Protected Instance Methods

calculate_time(seconds, unit) click to toggle source

0 <-> 29 secs # => seconds 30 secs <-> 44 mins, 29 secs # => minutes 44 mins, 30 secs <-> 23 hrs, 59 mins, 29 secs # => hours 23 hrs, 59 mins, 29 secs <-> 29 days, 23 hrs, 59 mins, 29 secs # => days 29 days, 23 hrs, 59 mins, 29 secs <-> 1 yr minus 1 sec # => months 1 yr <-> max time or date # => years

# File lib/twitter_cldr/localized/localized_timespan.rb, line 65
def calculate_time(seconds, unit)
  (seconds.to_f / TIME_IN_SECONDS[unit].to_f).abs.round.to_i
end
calculate_unit(seconds, options = {}) click to toggle source
# File lib/twitter_cldr/localized/localized_timespan.rb, line 45
def calculate_unit(seconds, options = {})
  approximate = options[:approximate]
  approximate = false if approximate.nil?
  multiplier = approximate ? APPROXIMATE_MULTIPLIER : 1

  if seconds < (TIME_IN_SECONDS[:minute] * multiplier) then :second
  elsif seconds < (TIME_IN_SECONDS[:hour] * multiplier) then :minute
  elsif seconds < (TIME_IN_SECONDS[:day] * multiplier) then :hour
  elsif seconds < (TIME_IN_SECONDS[:week] * multiplier) then :day
  elsif seconds < (TIME_IN_SECONDS[:month] * multiplier) then :week
  elsif seconds < (TIME_IN_SECONDS[:year] * multiplier) then :month
  else :year end
end