본문 바로가기
삽질/Ruby

[Ruby] Time 비교하기

by 푸딩s 2014. 11. 26.
728x90
반응형


time1 = Time.now

time2 = Time.now + 120 # 2분 후 

timecheck = time2 - time1


case timecheck

when 0 then p 'just now'

when 1 then p 'a second ago'

when 2..59 then p timecheck.to_s + ' seconds ago' 

when 60..119 then p 'a minute ago'  # 120 = 2 minutes

when 120..3540 then p (timecheck/60).to_i.to_s + ' minutes ago'

when 3541..7100 then p 'an hour ago'  # 3600 = 1 hour

when 7101..82800 then p ((timecheck+99)/3600).to_i.to_s + ' hours ago' 

when 82801..172000 then p 'a day ago'  # 86400 = 1 day

when 172001..518400 then p ((timecheck+800)/(60*60*24)).to_i.to_s + ' days ago'

when 518400..1036800 then p 'a week ago'

else p ((timecheck+180000)/(60*60*24*7)).to_i.to_s + ' weeks ago'

end


===> "2 minutes ago"


728x90
반응형

'삽질 > Ruby' 카테고리의 다른 글

[Ruby] 빈 공간 텍스트 채우기  (0) 2014.11.28
[Ruby] File Time  (0) 2014.11.26
[Ruby] html 예제  (0) 2014.11.26

댓글