Sha256: 3ba6afd29201458c55c83e109906d9bbe1a9cc988a93f1c60dc28433f9e5dc95

Contents?: true

Size: 1.62 KB

Versions: 39

Compression:

Stored size: 1.62 KB

Contents

#!/usr/bin/env ruby

module Flapjack
  module Utility

    def time_period_in_words(period)
      period_mm, period_ss  = period.divmod(60)
      period_hh, period_mm  = period_mm.divmod(60)
      period_dd, period_hh  = period_hh.divmod(24)
      ["#{period_dd} days",
       "#{period_hh} hours",
       "#{period_mm} minutes",
       "#{period_ss} seconds"].reject {|s| s =~ /^0 /}.join(', ')
    end

    # Returns relative time in words referencing the given date
    # relative_time_ago(Time.now) => 'about a minute ago'
    def relative_time_ago(from_time)
      distance_in_minutes = (((Time.now - from_time.to_time).abs)/60).round
      case distance_in_minutes
        when 0..1 then 'about a minute'
        when 2..44 then "#{distance_in_minutes} minutes"
        when 45..89 then 'about 1 hour'
        when 90..1439 then "about #{(distance_in_minutes.to_f / 60.0).round} hours"
        when 1440..2439 then '1 day'
        when 2440..2879 then 'about 2 days'
        when 2880..43199 then "#{(distance_in_minutes / 1440).round} days"
        when 43200..86399 then 'about 1 month'
        when 86400..525599 then "#{(distance_in_minutes / 43200).round} months"
        when 525600..1051199 then 'about 1 year'
        else "over #{(distance_in_minutes / 525600).round} years"
      end
    end

    # returns a string showing the local timezone we're running in
    # eg "CST (UTC+09:30)"
    def local_timezone
      tzname = Time.new.zone
      q, r = Time.new.utc_offset.divmod(3600)
      sign = (q < 0) ? '-' : '+'
      tzoffset = sign + "%02d" % q.abs.to_s + ':' + r.to_f.div(60).to_s
      "#{tzname} (UTC#{tzoffset})"
    end

  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
flapjack-0.6.61 lib/flapjack/utility.rb
flapjack-0.6.60 lib/flapjack/utility.rb
flapjack-0.6.59 lib/flapjack/utility.rb
flapjack-0.6.58 lib/flapjack/utility.rb
flapjack-0.6.57 lib/flapjack/utility.rb
flapjack-0.6.56 lib/flapjack/utility.rb
flapjack-0.6.55 lib/flapjack/utility.rb
flapjack-0.6.54 lib/flapjack/utility.rb
flapjack-0.6.53 lib/flapjack/utility.rb
flapjack-0.6.52 lib/flapjack/utility.rb
flapjack-0.6.51 lib/flapjack/utility.rb
flapjack-0.6.50 lib/flapjack/utility.rb
flapjack-0.6.49 lib/flapjack/utility.rb
flapjack-0.6.48 lib/flapjack/utility.rb
flapjack-0.6.47 lib/flapjack/utility.rb
flapjack-0.6.46 lib/flapjack/utility.rb
flapjack-0.6.45 lib/flapjack/utility.rb
flapjack-0.6.44 lib/flapjack/utility.rb
flapjack-0.6.43 lib/flapjack/utility.rb
flapjack-0.6.42 lib/flapjack/utility.rb