lib/brdata/time_portuguese.rb in brdata-2.1.15 vs lib/brdata/time_portuguese.rb in brdata-3.0.0
- old
+ new
@@ -1,16 +1,17 @@
-module ActiveSupport::CoreExtensions::String::Conversions
+class String
# Cria a data com horĂ¡rio no padrao brasileiro e permanece aceitando no formato tradicional.
#
# Exemplo:
# "27/09/2007 01:23".to_date
-
+ alias_method :_original_to_time, :to_time
+
def to_time
- if /(\d{1,2})\W(\d{1,2})\W(\d{4})(\s((\d{1,2}):(\d{2})))?/ =~ self
- ::Time.mktime($3.to_i, $2.to_i, $1.to_i, $6.to_i, $7.to_i)
+ if /^(0?[1-9]|[12]\d|3[01])\W(0?[1-9]|1[012])\W(\d{4})(\W([01]?\d|2[0123])\W([0-5]?\d)\W?([0-5]\d)?)?$/ =~ self
+ ::Time.mktime($3.to_i, $2.to_i, $1.to_i, $5.to_i, $6.to_i, $7.to_i)
else
- ::Time.parse self
+ _original_to_time
end
end
end
@@ -23,11 +24,11 @@
# hora = Time.new
# hora.to_s_br ==> "27/09/2007 13:54"
def to_s_br
self.strftime("%d/%m/%Y %H:%M")
end
-
+
# Formata a hora usando nomes de dias e meses em Portugues
# Exemplo:
# hora = Time.new
# hora.strftime("%B") ==> "Janeiro"
# http://forum.rubyonbr.org/forums/1/topics/261
@@ -37,6 +38,7 @@
format.gsub!(/%A/, Date::DAYNAMES[self.wday])
format.gsub!(/%b/, Date::ABBR_MONTHNAMES[self.mon])
format.gsub!(/%B/, Date::MONTHNAMES[self.mon])
self.strftime_nolocale(format)
end
-end
\ No newline at end of file
+end
+