Class: Delocalize::LocalizedDateTimeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/delocalize/localized_date_time_parser.rb

Constant Summary collapse

REGEXPS =

extend/change this according to your needs by merging your custom regexps

{
  '%B' => "(#{Date::MONTHNAMES.compact.join('|')})",      # long month name
  '%b' => "(#{Date::ABBR_MONTHNAMES.compact.join('|')})", # short month name
  '%m' => "(\\d{2})",                                     # numeric month
  '%A' => "(#{Date::DAYNAMES.join('|')})",                # full day name
  '%a' => "(#{Date::ABBR_DAYNAMES.join('|')})",           # short day name
  '%Y' => "(\\d{4})",                                     # long year
  '%y' => "(\\d{2})",                                     # short year
  '%e' => "(\\s\\d|\\d{2})",                              # short day
  '%d' => "(\\d{2})",                                     # full day
  '%H' => "(\\d{2})",                                     # hour (24)
  '%M' => "(\\d{2})",                                     # minute
  '%S' => "(\\d{2})"                                      # second
}

Class Method Summary collapse

Class Method Details

.parse(datetime, type) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/delocalize/localized_date_time_parser.rb', line 23

def parse(datetime, type)
  return unless datetime
  return datetime if datetime.respond_to?(:strftime) # already a Date/Time object -> no need to parse it

  translate_month_and_day_names(datetime)
  input_formats(type).each do |original_format|
    next unless datetime =~ /^#{apply_regex(original_format)}$/

    datetime = DateTime.strptime(datetime, original_format)
    return Date == type ?
      datetime.to_date :
      Time.zone.local(datetime.year, datetime.mon, datetime.mday, datetime.hour, datetime.min, datetime.sec)
  end
  default_parse(datetime, type)
end