Skip to content

Instantly share code, notes, and snippets.

@noniq
Created March 1, 2014 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noniq/9282811 to your computer and use it in GitHub Desktop.
Save noniq/9282811 to your computer and use it in GitHub Desktop.
Fun with dates.
gem "rails", "~> 3.2" # It’s fixed in Rails 4, btw :)
gem "timecop", "~> 0.7"
require "action_view"
require "active_support/core_ext"
require "timecop"
extend ActionView::Helpers::DateHelper
Time.zone = "CET"
Timecop.travel("2014-03-01 00:30 CET")
time_ago_in_words(1.year.ago) # => "about 1 year"
# So far so good.
time_ago_in_words(2.years.ago) # => "almost 2 years"
# Huh? Why “almost”??
# Let’s see:
1.year.ago # => Fri, 01 Mar 2013 00:30:00 CET +01:00
2.years.ago # => Thu, 01 Mar 2012 00:30:00 CET +01:00
# Maybe a timezone issue?
1.year.ago.utc # => 2013-02-28 23:30:00 UTC
2.years.ago.utc # => 2012-02-29 23:30:00 UTC
# Gotcha! Because of course:
distance_of_time_in_words("2012-02-29 23:30:00 UTC", "2014-02-28 23:30:00 UTC") # => "almost 2 years"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment