Skip to content

Instantly share code, notes, and snippets.

@mphalliday
Last active October 12, 2025 20:34
Show Gist options
  • Select an option

  • Save mphalliday/6121728 to your computer and use it in GitHub Desktop.

Select an option

Save mphalliday/6121728 to your computer and use it in GitHub Desktop.

Revisions

  1. mphalliday revised this gist Jul 31, 2013. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  2. mphalliday renamed this gist Jul 31, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. mphalliday revised this gist Jul 31, 2013. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  4. mphalliday created this gist Jul 31, 2013.
    5 changes: 5 additions & 0 deletions DON'Ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    Time.now # => Returns system time and ignores your configured time zone.
    Time.parse("2012-03-02 16:05:37") # => Will assume time string given is in the system's time zone.
    Time.strptime(time_string, '%Y-%m-%dT%H:%M:%S%z') # Same problem as with Time#parse.
    Date.today # This could be yesterday or tomorrow depending on the machine's time zone.
    Date.today.to_time # => # Still not the configured time zone.
    10 changes: 10 additions & 0 deletions DOs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    2.hours.ago # => Fri, 02 Mar 2012 20:04:47 JST +09:00
    1.day.from_now # => Fri, 03 Mar 2012 22:04:47 JST +09:00
    Date.today.to_time_in_current_zone # => Fri, 02 Mar 2012 22:04:47 JST +09:00
    Date.current # => Fri, 02 Mar
    Time.zone.parse("2012-03-02 16:05:37") # => Fri, 02 Mar 2012 16:05:37 JST +09:00
    Time.zone.now # => Fri, 02 Mar 2012 22:04:47 JST +09:00
    Time.current # Same thing but shorter. (Thank you Lukas Sarnacki pointing this out.)
    Time.zone.today # If you really can't have a Time or DateTime for some reason
    Time.zone.now.utc.iso8601 # When supliyng an API (you can actually skip .zone here, but I find it better to always use it, than miss it when it's needed)
    Time.strptime(time_string, '%Y-%m-%dT%H:%M:%S%z').in_time_zone(Time.zone) # If you can't use Time#parse