Created
January 1, 2013 00:33
-
-
Save ashirazi/4424177 to your computer and use it in GitHub Desktop.
String#strptime
Ruby needs symmetry for Time#strftime.
If a Time can be converted to a string (given a pattern),
then a String should be convertible to a Time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This really should belong in String, not Date... | |
class String | |
# Calculate the Time represented by this string, and the (optional) time pattern | |
# that describes this string (see Time#strftime). | |
# Returns a nil time if the String does not match the pattern, or the pattern is invalid. | |
def strptime(format=nil) | |
date = format ? Date.strptime(self, format) : Date.strptime(self) | |
date.to_time | |
rescue | |
nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, Arild. I wanted this a while back. 👍