Created
June 13, 2011 13:17
-
-
Save lucascaton/1022746 to your computer and use it in GitHub Desktop.
lib/extensions/string.rb
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
class String | |
def to_date_from_br_format | |
unless self =~ /(\d{2})\/(\d{2})\/(\d{4})/ | |
raise "String is not formatted as a brazilian date (#{self})" | |
end | |
"#{$3}-#{$2}-#{$1}".to_date | |
end | |
def to_datetime_from_br_format | |
unless self =~ /(\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2})/ | |
raise "String is not formatted as a brazilian datetime (#{self})" | |
end | |
"#{$3}-#{$2}-#{$1} #{$4}:#{$5}".to_datetime | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
config/initializers/lib_requires.rb:
require 'extensions/string'