This gem adds a few methods to Time, both instance and class. It’s not great, but makes my life easier on a few cucumber based projects. There’s a method Time.strtotime which takes a date(time) string and turns it into a time object. Also method add and substract to the instance. (Important, these methods do not account for leap years and months, etc, one month is 30 days and 1 year is 365 days.)

~ to see if two Time objects are “close enough”

Time

DateTimeRegex a regex for verifying if a string might represent a date

Usage examples:

Time.strtotime date_string

| date string | resulting time object | | 24-12-2011 | Time.local(2011,12,24) | | 2011-12-24 | Time.local(2011,12,24) | | 24/12/2011 | Time.local(2011,12,24) | | 2011/12/24 | Time.local(2011,12,24) | | 24122011 | Time.local(2011,12,24) | | 20111224 | Time.local(2011,12,24) | | 30042012 | Time.local(2012,04,30) | | 20120430 | Time.local(2012,04,30) | | 01052012 | Time.local(2012,05,01) | | 20120501 | Time.local(2012,05,01) | | 24-12-2011 22:10:54 | Time.local(2011,12,24,22,10,54) | | 2011-12-24_12:10:54 | Time.local(2011,12,24,12,10,54) | | 24/12/2011-12:20:31 | Time.local(2011,12,24,12,20,31) | | 01052012122031 | Time.local(2012,05,01,12,20,31) | | 20120501122031 | Time.local(2012,05,01,12,20,31) |

using .add or .substract

|starting object | method | arguments | resulting object | | Time.local(2011,12,24,22,10,54) | add | {:year => 2, :month => 1 } | Time.local(2011,12,24,22,10,54) + (2*60*60*24*365 + 60*60*24*30) | | Time.local(2012,05,01,12,20,31) | substract | => 2, :day => 3 | Time.local(2012,05,01,12,20,31) - (60*60*24*30*2 + 60*60*24*3)| | Time.local(2011,12,24,22,10,54) | add | => 1, :hour => 3 |Time.local(2011,12,24,22,10,54) + (60*60*24 + 60*60*3) |

using =~ this method allows you to get “close enough” times, defaults to 300 seconds (5 minutes) of acceptable difference time_obj =~ other_time # true or false if the difference between the objects is less than 5 minutes time_obj.=~ other_time, 360 # true or false if the difference is less than 6 minutes

********************************************** *How is this diferent than just “Time.parse”?* **********************************************

Time.parse does not work well with non-American formats, if like me, you’re working with strings that have dates in a format like: DD/MM/YYYY Time.parse will not do. In fact, if you rewrite strtotime so it just calls parse a few of the tests included will fail.

New in 2.0.1 Fixed an issue/bug where a few non date number sequences would be evaluated as date by ::valid_datetime? due to the a liniency in the validating regex. The verification regex is now available for public as a constant: Time::DateTimeRegex

New in 2.0.0 Completelly changed =~ method, the second argument is now an acceptable difference in seconds - more reliable. Removed the require ‘time’ std lib (which should reduce memory of this gem ) and as such it no longers has the parse method included in the strtotime method, if you want to use parse, use it! As such, with these differences this version is not retro-compatible, if you were using a previous version it may fail on this one.

New in 1.3.1 Added =~ method which allows you to see if two time objects are close enough, by default it compares up till the hour, for instance, a =~ b will be true if a and b are both objects with the same year, month, day and hour. Removed all the aborts! Replaced be real errors inside the class! Sorry for the mess.

New in 1.3: Added support for time strings in the format you’d usualy pass to Time.parse Added utc_parse method for the lazy, it parses the date and converts it to utc

New in 1.2 Tomorrow and Yesterday methods

New in 1.1 I skiped this version.… because I suck at versioning