Module: DatetimeFormatConverter

Defined in:
lib/datetime_format_converter.rb,
lib/datetime_format_converter/railtie.rb,
lib/datetime_format_converter/version.rb,
lib/datetime_format_converter/view_helpers.rb

Defined Under Namespace

Modules: ViewHelpers Classes: DatetimeNotSupportedError, Railtie

Constant Summary collapse

FORMAT_MAPPING =
[
  ["%C",       nil,      "year / 100 (round down.  20 in 2009)"],
  ["%N",       nil,      "Fractional seconds digits, default is 9 digits (nanosecond)"],
  ["%w",       nil,      "Day of the week (Sunday is 0, 0..6)"],
  ["%W",       nil,      "Week number of the year.  The week starts with Monday.  (00..53)"],
  ["%Y",       "YYYY",   "Year with century (can be negative, 4 digits at least)"],
  ["%y",       "YY",     "year % 100 (00..99)"],
  ["%m",       "MM",     "Month of the year, zero-padded (01..12)"],
  ["%_m",      "MM",     "blank-padded ( 1..12)"],
  ["%-m",      "M",      "no-padded (1..12)"],
  ["%B",       "MMMM",   "The full month name (``January'')"],
  ["%^B",      "MMMM",   "uppercased (``JANUARY'')"],
  ["%b",       "MMM",    "The abbreviated month name (``Jan'')"],
  ["%h",       "MMM",    "Equivalent to %b"],
  ["%^b",      "MMM",    "uppercased (``JAN'')"],
  ["%d",       "DD",     "Day of the month, zero-padded (01..31)"],
  ["%-d",      "D",      "no-padded (1..31)"],
  ["%e",       "DD",     "Day of the month, blank-padded ( 1..31)"],
  ["%j",       "DDDD",   "Day of the year (001..366)"],
  ["%H",       "HH",     "Hour of the day, 24-hour clock, zero-padded (00..23)"],
  ["%k",       "H",      "Hour of the day, 24-hour clock, blank-padded ( 0..23)"],
  ["%I",       "hh",     "Hour of the day, 12-hour clock, zero-padded (01..12)"],
  ["%l",       "h",      "Hour of the day, 12-hour clock, blank-padded ( 1..12)"],
  ["%P",       "a",      "Meridian indicator, lowercase (``am'' or ``pm'')"],
  ["%p",       "A",      "Meridian indicator, uppercase (``AM'' or ``PM'')"],
  ["%M",       "mm",     "Minute of the hour (00..59)"],
  ["%S",       "ss",     "Second of the minute (00..59)"],
  ["%L",       "SSS",    "Millisecond of the second (000..999)"],
  ["%z",       "Z",      "Time zone as hour and minute offset from UTC (e.g. +0900)"],
  ["%:z",      "ZZ",     "hour and minute offset from UTC with a colon (e.g. +09:00)"],
  ["%::z",     "ZZ:00",  "hour, minute and second offset from UTC (e.g. +09:00:00)"],
  ["%:::z",    "ZZ:00",  "hour, minute and second offset from UTC (e.g. +09, +09:30, +09:30:30)"],
  ["%Z",       "ZZ",     "Time zone abbreviation name"],
  ["%A",       "dddd",   "The full weekday name (``Sunday'')"],
  ["%^A",      "dddd",   "uppercased (``SUNDAY'')"],
  ["%a",       "ddd",    "The abbreviated name (``Sun'')"],
  ["%^a",      "ddd",    "uppercased (``SUN'')"],
  ["%u",       "E",      "Day of the week (Monday is 1, 1..7)"],
  ["%G",       "W",      "The week-based year"],
  ["%g",       "WW",     "The last 2 digits of the week-based year (00..99)"],
  ["%V",       "W",      "Week number of the week-based year (01..53)"],
  ["%U",       "WW",     "Week number of the year.  The week starts with Sunday.  (00..53)"],
  ["%s",       "x",      "Number of seconds since 1970-01-01 00:00:00 UTC."],
  ["%Q",       "x",      "Number of microseconds since 1970-01-01 00:00:00 UTC."],
  ["%n",       "\n",     "Newline character (\n)"],
  ["%t",       "\t",     "Tab character (\t)"],
  ["%%",       "%",      "Literal ``%'' character"],
  ["%3N",      "SSS",                             "millisecond (3 digits)"],
  ["%6N",      "SSS000",                          "microsecond (6 digits)"],
  ["%9N",      "SSS000000",                       "nanosecond (9 digits)"],
  ["%12N",     "SSS000000000",                    "picosecond (12 digits)"],
  ["%c",       "ddd MMM DD HH:mm:ss YYYY",        "date and time (%a %b %e %T %Y)"],
  ["%D",       "mm/DD/YY",                        "Date (%m/%d/%y)"],
  ["%x",       "mm/DD/YY",                        "Same as %D"],
  ["%F",       "YYYY-mm-DD",                      "The ISO 8601 date format (%Y-%m-%d)"],
  ["%v",       "DD-MMM-YYYY",                     "VMS date (%e-%b-%Y)"],
  ["%T",       "HH:mm:ss",                        "24-hour time (%H:%M:%S)"],
  ["%X",       "HH:mm:ss",                        "Same as %T"],
  ["%r",       "HH:mm:ss A",                      "12-hour time (%I:%M:%S %p)"],
  ["%R",       "HH:mm",                           "24-hour time (%H:%M)"],
  ["%+",       "ddd MMM DD HH:mm:ss ZZ YYYY",     "date(1) (%a %b %e %H:%M:%S %Z %Y)"]
]
TIMEZONE_MAPPING =
{
  "International Date Line West" => "Pacific/Midway",
  "Midway Island"                => "Pacific/Midway",
  "American Samoa"               => "Pacific/Pago_Pago",
  "Hawaii"                       => "Pacific/Honolulu",
  "Alaska"                       => "America/Juneau",
  "Pacific Time (US & Canada)"   => "America/Los_Angeles",
  "Tijuana"                      => "America/Tijuana",
  "Mountain Time (US & Canada)"  => "America/Denver",
  "Arizona"                      => "America/Phoenix",
  "Chihuahua"                    => "America/Chihuahua",
  "Mazatlan"                     => "America/Mazatlan",
  "Central Time (US & Canada)"   => "America/Chicago",
  "Saskatchewan"                 => "America/Regina",
  "Guadalajara"                  => "America/Mexico_City",
  "Mexico City"                  => "America/Mexico_City",
  "Monterrey"                    => "America/Monterrey",
  "Central America"              => "America/Guatemala",
  "Eastern Time (US & Canada)"   => "America/New_York",
  "Indiana (East)"               => "America/Indiana/Indianapolis",
  "Bogota"                       => "America/Bogota",
  "Lima"                         => "America/Lima",
  "Quito"                        => "America/Lima",
  "Atlantic Time (Canada)"       => "America/Halifax",
  "Caracas"                      => "America/Caracas",
  "La Paz"                       => "America/La_Paz",
  "Santiago"                     => "America/Santiago",
  "Newfoundland"                 => "America/St_Johns",
  "Brasilia"                     => "America/Sao_Paulo",
  "Buenos Aires"                 => "America/Argentina/Buenos_Aires",
  "Montevideo"                   => "America/Montevideo",
  "Georgetown"                   => "America/Guyana",
  "Greenland"                    => "America/Godthab",
  "Mid-Atlantic"                 => "Atlantic/South_Georgia",
  "Azores"                       => "Atlantic/Azores",
  "Cape Verde Is."               => "Atlantic/Cape_Verde",
  "Dublin"                       => "Europe/Dublin",
  "Edinburgh"                    => "Europe/London",
  "Lisbon"                       => "Europe/Lisbon",
  "London"                       => "Europe/London",
  "Casablanca"                   => "Africa/Casablanca",
  "Monrovia"                     => "Africa/Monrovia",
  "UTC"                          => "Etc/UTC",
  "Belgrade"                     => "Europe/Belgrade",
  "Bratislava"                   => "Europe/Bratislava",
  "Budapest"                     => "Europe/Budapest",
  "Ljubljana"                    => "Europe/Ljubljana",
  "Prague"                       => "Europe/Prague",
  "Sarajevo"                     => "Europe/Sarajevo",
  "Skopje"                       => "Europe/Skopje",
  "Warsaw"                       => "Europe/Warsaw",
  "Zagreb"                       => "Europe/Zagreb",
  "Brussels"                     => "Europe/Brussels",
  "Copenhagen"                   => "Europe/Copenhagen",
  "Madrid"                       => "Europe/Madrid",
  "Paris"                        => "Europe/Paris",
  "Amsterdam"                    => "Europe/Amsterdam",
  "Berlin"                       => "Europe/Berlin",
  "Bern"                         => "Europe/Zurich",
  "Zurich"                       => "Europe/Zurich",
  "Rome"                         => "Europe/Rome",
  "Stockholm"                    => "Europe/Stockholm",
  "Vienna"                       => "Europe/Vienna",
  "West Central Africa"          => "Africa/Algiers",
  "Bucharest"                    => "Europe/Bucharest",
  "Cairo"                        => "Africa/Cairo",
  "Helsinki"                     => "Europe/Helsinki",
  "Kyiv"                         => "Europe/Kiev",
  "Riga"                         => "Europe/Riga",
  "Sofia"                        => "Europe/Sofia",
  "Tallinn"                      => "Europe/Tallinn",
  "Vilnius"                      => "Europe/Vilnius",
  "Athens"                       => "Europe/Athens",
  "Istanbul"                     => "Europe/Istanbul",
  "Minsk"                        => "Europe/Minsk",
  "Jerusalem"                    => "Asia/Jerusalem",
  "Harare"                       => "Africa/Harare",
  "Pretoria"                     => "Africa/Johannesburg",
  "Kaliningrad"                  => "Europe/Kaliningrad",
  "Moscow"                       => "Europe/Moscow",
  "St. Petersburg"               => "Europe/Moscow",
  "Volgograd"                    => "Europe/Volgograd",
  "Samara"                       => "Europe/Samara",
  "Kuwait"                       => "Asia/Kuwait",
  "Riyadh"                       => "Asia/Riyadh",
  "Nairobi"                      => "Africa/Nairobi",
  "Baghdad"                      => "Asia/Baghdad",
  "Tehran"                       => "Asia/Tehran",
  "Abu Dhabi"                    => "Asia/Muscat",
  "Muscat"                       => "Asia/Muscat",
  "Baku"                         => "Asia/Baku",
  "Tbilisi"                      => "Asia/Tbilisi",
  "Yerevan"                      => "Asia/Yerevan",
  "Kabul"                        => "Asia/Kabul",
  "Ekaterinburg"                 => "Asia/Yekaterinburg",
  "Islamabad"                    => "Asia/Karachi",
  "Karachi"                      => "Asia/Karachi",
  "Tashkent"                     => "Asia/Tashkent",
  "Chennai"                      => "Asia/Kolkata",
  "Kolkata"                      => "Asia/Kolkata",
  "Mumbai"                       => "Asia/Kolkata",
  "New Delhi"                    => "Asia/Kolkata",
  "Kathmandu"                    => "Asia/Kathmandu",
  "Astana"                       => "Asia/Dhaka",
  "Dhaka"                        => "Asia/Dhaka",
  "Sri Jayawardenepura"          => "Asia/Colombo",
  "Almaty"                       => "Asia/Almaty",
  "Novosibirsk"                  => "Asia/Novosibirsk",
  "Rangoon"                      => "Asia/Rangoon",
  "Bangkok"                      => "Asia/Bangkok",
  "Hanoi"                        => "Asia/Bangkok",
  "Jakarta"                      => "Asia/Jakarta",
  "Krasnoyarsk"                  => "Asia/Krasnoyarsk",
  "Beijing"                      => "Asia/Shanghai",
  "Chongqing"                    => "Asia/Chongqing",
  "Hong Kong"                    => "Asia/Hong_Kong",
  "Urumqi"                       => "Asia/Urumqi",
  "Kuala Lumpur"                 => "Asia/Kuala_Lumpur",
  "Singapore"                    => "Asia/Singapore",
  "Taipei"                       => "Asia/Taipei",
  "Perth"                        => "Australia/Perth",
  "Irkutsk"                      => "Asia/Irkutsk",
  "Ulaanbaatar"                  => "Asia/Ulaanbaatar",
  "Seoul"                        => "Asia/Seoul",
  "Osaka"                        => "Asia/Tokyo",
  "Sapporo"                      => "Asia/Tokyo",
  "Tokyo"                        => "Asia/Tokyo",
  "Yakutsk"                      => "Asia/Yakutsk",
  "Darwin"                       => "Australia/Darwin",
  "Adelaide"                     => "Australia/Adelaide",
  "Canberra"                     => "Australia/Melbourne",
  "Melbourne"                    => "Australia/Melbourne",
  "Sydney"                       => "Australia/Sydney",
  "Brisbane"                     => "Australia/Brisbane",
  "Hobart"                       => "Australia/Hobart",
  "Vladivostok"                  => "Asia/Vladivostok",
  "Guam"                         => "Pacific/Guam",
  "Port Moresby"                 => "Pacific/Port_Moresby",
  "Magadan"                      => "Asia/Magadan",
  "Srednekolymsk"                => "Asia/Srednekolymsk",
  "Solomon Is."                  => "Pacific/Guadalcanal",
  "New Caledonia"                => "Pacific/Noumea",
  "Fiji"                         => "Pacific/Fiji",
  "Kamchatka"                    => "Asia/Kamchatka",
  "Marshall Is."                 => "Pacific/Majuro",
  "Auckland"                     => "Pacific/Auckland",
  "Wellington"                   => "Pacific/Auckland",
  "Nuku'alofa"                   => "Pacific/Tongatapu",
  "Tokelau Is."                  => "Pacific/Fakaofo",
  "Chatham Is."                  => "Pacific/Chatham",
  "Samoa"                        => "Pacific/Apia"
}
VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.datetime_format_to_js(source_format) ⇒ Object



230
231
232
233
234
235
# File 'lib/datetime_format_converter.rb', line 230

def datetime_format_to_js(source_format)
  raise DatetimeNotSupportedError.new("#{source_format} is not supported to convert it to js format") unless supported(source_format)
  supported_formats.inject(source_format) do |acc, h|
    acc.gsub(h[0], h[1])
  end
end

.not_supported_regexpObject



243
244
245
# File 'lib/datetime_format_converter.rb', line 243

def not_supported_regexp
  Regexp.new(FORMAT_MAPPING.reject{|i|i[1]}.map(&:first).map{|i|"(#{i})"}.join("|"))
end

.supported(source_format) ⇒ Object



238
239
240
# File 'lib/datetime_format_converter.rb', line 238

def supported(source_format)
  source_format.scan(not_supported_regexp).none?
end

.supported_formatsObject



248
249
250
# File 'lib/datetime_format_converter.rb', line 248

def supported_formats
  FORMAT_MAPPING.select{ |i| i[1] }
end

.timezone_format_to_js(source_format) ⇒ Object



225
226
227
# File 'lib/datetime_format_converter.rb', line 225

def timezone_format_to_js(source_format)
  TIMEZONE_MAPPING.fetch(source_format) { raise DatetimeNotSupportedError.new("#{source_format} is not supported to convert it to js format") }
end