lmarburger (owner)

Revisions

gist: 210974 Download_button fork
public
Public Clone URL: git://gist.github.com/210974.git
Embed All Files: show embed
shortened_time.yml #
1
2
3
4
5
6
7
8
9
10
11
12
13
shortened_time:
  datetime:
    distance_in_words:
      less_than_x_seconds: '<{{count}}s'
      half_a_minute: '<1m'
      less_than_x_minutes: '<{{count}}m'
      x_minutes: '{{count}}m'
      about_x_hours: '{{count}}h'
      x_days: '{{count}}d'
      about_x_months: '{{count}}mon'
      x_months: '{{count}}mon'
      about_x_years: '{{count}}y'
      over_x_years: '{{count}}y'
shortened_time_ago_in_words_helper.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module ShortenedTimeAgoInWordsHelper
 
  # Use I18N to translate +distance_of_time_in_words+ into a condensed format.
  # Change the output in config/shortened_time.yml
  #
  # ==== Examples
  # from_time = Time.now
  # shortened_time_ago_in_words(from_time + 50.minutes) # => 1h
  # shortened_time_ago_in_words(50.minutes.from_now) # => 1h
  # shortened_time_ago_in_words(from_time + 15.seconds) # => <1m
  # shortened_time_ago_in_words(from_time + 15.seconds, true) # => <20s
  # shortened_time_ago_in_words(3.years.from_now) # => 3y
  # shortened_time_ago_in_words(from_time + 60.hours) # => 2d
  # shortened_time_ago_in_words(from_time + 45.seconds, true) # => <1m
  # shortened_time_ago_in_words(from_time - 45.seconds, true) # => <1m
  # shortened_time_ago_in_words(76.seconds.from_now) # => 1m
  # shortened_time_ago_in_words(from_time + 1.year + 3.days) # => 1y
  # shortened_time_ago_in_words(from_time + 4.years + 9.days + 30.minutes + 5.seconds) # => 4y
  #
  # to_time = Time.now + 6.years + 19.days
  # shortened_time_ago_in_words(to_time, true) # => 6y
  # shortened_time_ago_in_words(to_time, true) # => 6y
  # shortened_time_ago_in_words(Time.now) # => <1m
  #
  def shortened_time_ago_in_words(from_time, include_seconds = false)
    distance_of_time_in_words(from_time, Time.now, include_seconds, :locale => :shortened_time)
  end
 
end