Skip to content

Instantly share code, notes, and snippets.

@lucascaton
Created June 13, 2011 13:17
Show Gist options
  • Save lucascaton/1022746 to your computer and use it in GitHub Desktop.
Save lucascaton/1022746 to your computer and use it in GitHub Desktop.
lib/extensions/string.rb
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
@lucascaton
Copy link
Author

config/initializers/lib_requires.rb:

require 'extensions/string'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment