2008-04-24
宝典:第五式 惊奇小发现之Time-ago-in-words
关键字: 时间差 小发现 rails
WEB2.0类的网站越来越多了,对于时间显示这个概念也都有不同的方法了,
类似:"刚刚","*分钟前","几天前"这些字眼也常常会见到了
于是近期老在想这个到底是怎么实现的,在一次看lovdbyless的时候不经意发现了
Time-ago-in-words 方法
用法也比较简单
看了下原码
小弟我不学习浅不知道有没有中文显示时间差的,于是就自己在application_helper里面写了下面的转换为中文
类似:"刚刚","*分钟前","几天前"这些字眼也常常会见到了
于是近期老在想这个到底是怎么实现的,在一次看lovdbyless的时候不经意发现了
Time-ago-in-words 方法
用法也比较简单
<%=time_ago_in_words(from_time, include_seconds = false) %> ##include_seconds是否显示秒
看了下原码
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_minutes = (((to_time - from_time).abs)/60).round
distance_in_seconds = ((to_time - from_time).abs).round
case distance_in_minutes
when 0..1
return (distance_in_minutes == 0) ? 'less than a minute' : '1 minute' unless include_seconds
case distance_in_seconds
when 0..4 then 'less than 5 seconds'
when 5..9 then 'less than 10 seconds'
when 10..19 then 'less than 20 seconds'
when 20..39 then 'half a minute'
when 40..59 then 'less than a minute'
else '1 minute'
end
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..2879 then '1 day'
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
def time_ago_in_words(from_time, include_seconds = false)
distance_of_time_in_words(from_time, Time.now, include_seconds)
end
小弟我不学习浅不知道有没有中文显示时间差的,于是就自己在application_helper里面写了下面的转换为中文
def ch_time(from_time) time = time-ago-in-words(from_time,include_seconds = true) time = time.sub(/about /,"") time = time.sub(/over /,"") if time.to_i == 0 case time.to_s when 'half a minute' then '半分钟前' when 'less than a minute' then '不到1分钟前' when 'less than 5 seconds' then '5秒前' when 'less than 10 seconds' then '10秒前' when 'less than 20 seconds' then '20秒前' end else mun = time.to_i case time[-3,3] when 'nds' then mun.to_s+'秒前' when 'ute' then mun.to_s+'分前' when 'tes' then mun.to_s+'分钟前' when 'urs','our' then mun.to_s+'小时前' when 'day','ays' then mun.to_s+'天前' when 'nth','ths' then mun.to_s+'个月前' when 'ear','ars' then mun.to_s+'年前' end end end
- 08:57
- 浏览 (313)
- 评论 (3)
- 分类: 自制Rails葵花宝典
- 相关推荐
评论
sayid2008
2008-04-24
收到Quake Wang的建议
先试下再说


先试下再说
Quake Wang
2008-04-24
你可以把代码放到lib目录下面,比如localized_message.rb:
然后在environment.rb里面 require一下:
这样在页面还是可以使用原先的api
module ActionView
module Helpers
module DateHelper
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
...
然后在environment.rb里面 require一下:
require 'localized_message'
这样在页面还是可以使用原先的api
jiyanliang
2008-04-24
一直想找这个方法,今天碰到了,收藏。
看来都流行读源码了!
看来都流行读源码了!
发表评论
- 浏览: 2534 次
- 性别:

- 来自: 长沙

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
我要导入
用Parseexcel吧,我用过,效果比较好 http://www.wehear ...
-- by weskycn -
宝典:第三式 FCKeditor插 ...
有点郁闷,其他人上传时能够看到这个文件夹里面的所有图片
-- by CaiDeHen -
宝典:第三式 FCKeditor插 ...
这个具体不是很清楚, 但是它的上传图片都放在public\uploads\Ima ...
-- by sayid2008 -
宝典:第三式 FCKeditor插 ...
为什么fckeditor上传的图片能够公用?
-- by CaiDeHen -
宝典:第五式 惊奇小发现 ...
收到Quake Wang的建议先试下再说
-- by sayid2008






评论排行榜