distance_of_time_in_words,更智能的格式化?

时间:2014-10-30 01:03:28

标签: ruby-on-rails ruby time

目前我正在使用以下内容:

distance_of_time_in_words(tweet.created_at, Time.now + 30.minutes, include_seconds: false)

这在大多数情况下都可以正常工作,但我希望某些异常的格式不同,文档对于这种情况来说相当混乱。 (我也无法在这里找到任何关于它的问题)

例如,目前该函数在1天之前将时间转换为此值。

5 hours and 17 minutes ago

我将其用于发布的推文和时间,例如

Tweeted 4 days, 8 hours, and 3 minutes 

似乎有点长啰嗦和不必要的具体。

理想情况下,我希望能够展示:

less than a minute = the number of seconds ago

less than 1 hour = the amount of minutes

less than a day = the amount of hours

more than a day = the number of days

有没有办法做到这一点?

3 个答案:

答案 0 :(得分:1)

也许你想使用distance_of_time_in_words助手:

0 <-> 29 secs                                  # => less than a minute
30 secs <-> 1 min, 29 secs                     # => 1 minute
1 min, 30 secs <-> 44 mins, 29 secs            # => [2..44] minutes
44 mins, 30 secs <-> 89 mins, 29 secs          # => about 1 hour
89 mins, 30 secs <-> 23 hrs, 59 mins, 29 secs  # => about [2..24] hours
and so on...

您可以只使用一个参数来使用该帮助器。你认为这样的事情应该可以胜任:

 Tweeted <%= time_ago_in_words(tweet.created_at) ago

假设tweet.created_at约在29小时前,它将呈现给:

Tweeted 1 day ago

请参阅:http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words

答案 1 :(得分:1)

distance_of_time_in_words的输出(只是time_ago_in_words的别名)应如下所示:

0 <-> 29 secs                                                             # => less than a minute
30 secs <-> 1 min, 29 secs                                                # => 1 minute
1 min, 30 secs <-> 44 mins, 29 secs                                       # => [2..44] minutes
44 mins, 30 secs <-> 89 mins, 29 secs                                     # => about 1 hour
89 mins, 30 secs <-> 23 hrs, 59 mins, 29 secs                             # => about [2..24] hours
23 hrs, 59 mins, 30 secs <-> 41 hrs, 59 mins, 29 secs                     # => 1 day
41 hrs, 59 mins, 30 secs  <-> 29 days, 23 hrs, 59 mins, 29 secs           # => [2..29] days
29 days, 23 hrs, 59 mins, 30 secs <-> 44 days, 23 hrs, 59 mins, 29 secs   # => about 1 month
44 days, 23 hrs, 59 mins, 30 secs <-> 59 days, 23 hrs, 59 mins, 29 secs   # => about 2 months
59 days, 23 hrs, 59 mins, 30 secs <-> 1 yr minus 1 sec                    # => [2..12] months
1 yr <-> 1 yr, 3 months                                                   # => about 1 year
1 yr, 3 months <-> 1 yr, 9 months                                         # => over 1 year
1 yr, 9 months <-> 2 yr minus 1 sec                                       # => almost 2 years
2 yrs <-> max time or date                                                # => (same rules as 1 yr)

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words

这看起来像你想要的。有些东西必须覆盖distance_of_time_in_words?我不确定为什么会这样做,但事实似乎就是这样。

答案 2 :(得分:1)

这是一个非Rails解决方案,虽然看起来你最好使用Rails&#39; distance_of_time_in_words帮助者。通过文档看,后者似乎提供了极大的灵活性。

require 'time'

SECS_PER_MIN  =  60
SECS_PER_HOUR =  60 * SECS_PER_MIN
SECS_PER_DAY  =  24 * SECS_PER_HOUR
SECS_PER_WEEK =   7 * SECS_PER_DAY
SECS_PER_YEAR = 365 * SECS_PER_DAY
SECS_PER_CENT = 100 * SECS_PER_YEAR
SECS_PER_MLNM =  10 * SECS_PER_CENT
SECS_PER_EON  =  1_000_000 * SECS_PER_MLNM

def message(secs)
  case secs
  when (0...SECS_PER_MIN)
    "#{secs} seconds"
  when (SECS_PER_MIN...SECS_PER_HOUR)
    "#{secs/SECS_PER_MIN} minutes"
  when (SECS_PER_HOUR...SECS_PER_DAY)
    "#{secs/SECS_PER_HOUR} hours"
  when (SECS_PER_DAY...SECS_PER_WEEK)
    "#{secs/SECS_PER_DAY} days"
  when (SECS_PER_WEEK...SECS_PER_YEAR)
    "#{secs/SECS_PER_WEEK} weeks"
  when (SECS_PER_YEAR...SECS_PER_CENT)
      "#{secs/SECS_PER_YEAR} years"
  when (SECS_PER_CENT...SECS_PER_MLNM)
    "#{secs/SECS_PER_CENT} centuries"
  when (SECS_PER_MLNM...SECS_PER_EON)
    "#{secs/SECS_PER_MLNM} millenia"
  else
    "#{secs/SECS_PER_EON} eons"
  end << " ago"
end

您可以使用以下内容调用消息:

message(Time.now-tweet_time)

试试吧。

message(10)                      #=> "10 seconds ago"
message(1_000)                   #=> "16 minutes ago"
message(20_000)                  #=> "5 hours ago"
message(300_000)                 #=> "3 days ago"
message(10_000_000)              #=> "16 weeks ago"
message(1_000_000_000)           #=> "31 years ago"
message(25_000_000_000)          #=> "7 centuries ago"
message(1_300_000_000_000)       #=> "41 millenia ago"
message(100_000_000_000_000_000) #=> "3 eons ago"

你可以通过改变,例如,

来美化它
when (SECS_PER_DAY...SECS_PER_WEEK)
  "#{secs/SECS_PER_DAY} days"

when (SECS_PER_DAY...SECS_PER_WEEK)
  n = secs/SECS_PER_DAY
  n == 1 ? "1 day" : "#{n} days"