/usr/lib/ruby/vendor_ruby/timeliness/definitions.rb is in ruby-timeliness 0.3.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | module Timeliness
module Definitions
# Format tokens:
# y = year
# m = month
# d = day
# h = hour
# n = minute
# s = second
# u = micro-seconds
# ampm = meridian (am or pm) with or without dots (e.g. am, a.m, or a.m.)
# _ = optional space
# tz = Timezone abbreviation (e.g. UTC, GMT, PST, EST)
# zo = Timezone offset (e.g. +10:00, -08:00, +1000)
#
# All other characters are considered literal. You can embed regexp in the
# format but no guarantees that it will remain intact. If you don't use capture
# groups, dots or backslashes in the regexp, it may well work as expected.
# For special characters, use POSIX character classes for safety.
#
# Repeating tokens:
# x = 1 or 2 digits for unit (e.g. 'h' means an hour can be '9' or '09')
# xx = 2 digits exactly for unit (e.g. 'hh' means an hour can only be '09')
#
# Special Cases:
# yy = 2 or 4 digit year
# yyyy = exactly 4 digit year
# mmm = month long name (e.g. 'Jul' or 'July')
# ddd = Day name of 3 to 9 letters (e.g. Wed or Wednesday)
# u = microseconds matches 1 to 6 digits
@time_formats = [
'hh:nn:ss',
'hh-nn-ss',
'h:nn',
'h.nn',
'h nn',
'h-nn',
'h:nn_ampm',
'h.nn_ampm',
'h nn_ampm',
'h-nn_ampm',
'h_ampm'
]
@date_formats = [
'yyyy-mm-dd',
'yyyy/mm/dd',
'yyyy.mm.dd',
'm/d/yy',
'd/m/yy',
'm\d\yy',
'd\m\yy',
'd-m-yy',
'dd-mm-yyyy',
'd.m.yy',
'd mmm yy'
]
@datetime_formats = [
'yyyy-mm-dd hh:nn:ss.u',
'yyyy-mm-dd hh:nn:ss',
'yyyy-mm-dd h:nn',
'yyyy-mm-dd h:nn_ampm',
'm/d/yy h:nn:ss',
'm/d/yy h:nn_ampm',
'm/d/yy h:nn',
'd/m/yy hh:nn:ss',
'd/m/yy h:nn_ampm',
'd/m/yy h:nn',
'dd-mm-yyyy hh:nn:ss',
'dd-mm-yyyy h:nn_ampm',
'dd-mm-yyyy h:nn',
'ddd, dd mmm yyyy hh:nn:ss tz', # RFC 822
'ddd, dd mmm yyyy hh:nn:ss zo', # RFC 822
'ddd mmm d hh:nn:ss zo yyyy', # Ruby time string
'yyyy-mm-ddThh:nn:ssZ', # ISO 8601 without zone offset
'yyyy-mm-ddThh:nn:sszo', # ISO 8601 with zone offset
'yyyy-mm-ddThh:nn:ss.u', # ISO 8601 with usec
'yyyy-mm-ddThh:nn:ss.uzo', # ISO 8601 with usec and offset
'yyyy-mm-dd hh:nn:ss zo', # Ruby time string in later versions
'yyyy-mm-dd hh:nn:ss tz', # Ruby time string for UTC in later versions
]
# All tokens available for format construction. The token array is made of
# regexp and key for format component mapping, if any.
#
@format_tokens = {
'ddd' => [ '\w{3,9}' ],
'dd' => [ '\d{2}', :day ],
'd' => [ '\d{1,2}', :day ],
'mmm' => [ '\w{3,9}', :month ],
'mm' => [ '\d{2}', :month ],
'm' => [ '\d{1,2}', :month ],
'yyyy' => [ '\d{4}', :year ],
'yy' => [ '\d{4}|\d{2}', :year ],
'hh' => [ '\d{2}', :hour ],
'h' => [ '\d{1,2}', :hour ],
'nn' => [ '\d{2}', :min ],
'n' => [ '\d{1,2}', :min ],
'ss' => [ '\d{2}', :sec ],
's' => [ '\d{1,2}', :sec ],
'u' => [ '\d{1,6}', :usec ],
'ampm' => [ '[aApP]\.?[mM]\.?', :meridian ],
'zo' => [ '[+-]\d{2}:?\d{2}', :offset ],
'tz' => [ '[A-Z]{1,5}', :zone ],
'_' => [ '\s?' ]
}
# Component argument values will be passed to the format method if matched in
# the time string. The key should match the key defined in the format tokens.
#
# The array consists of the position the value should be inserted in
# the time array, and the code to place in the time array.
#
# If the position is nil, then the value won't be put in the time array. If the
# code is nil, then just the raw value is used.
#
@format_components = {
:year => [ 0, 'unambiguous_year(year)'],
:month => [ 1, 'month_index(month)'],
:day => [ 2 ],
:hour => [ 3, 'full_hour(hour, meridian ||= nil)'],
:min => [ 4 ],
:sec => [ 5 ],
:usec => [ 6, 'microseconds(usec)'],
:offset => [ 7, 'offset_in_seconds(offset)'],
:zone => [ 7, 'zone'],
:meridian => [ nil ]
}
# Mapping some common timezone abbreviations which are not mapped or
# mapped inconsistenly in ActiveSupport (TzInfo).
#
@timezone_mapping = {
'AEST' => 'Australia/Sydney',
'AEDT' => 'Australia/Sydney',
'ACST' => 'Australia/Adelaide',
'ACDT' => 'Australia/Adelaide',
'PST' => 'PST8PDT',
'PDT' => 'PST8PDT',
'CST' => 'CST6CDT',
'CDT' => 'CST6CDT',
'EDT' => 'EST5EDT',
'MDT' => 'MST7MDT'
}
US_FORMAT_REGEXP = /\Am{1,2}[^m]/
FormatNotFound = Class.new(StandardError)
DuplicateFormat = Class.new(StandardError)
class << self
attr_accessor :time_formats, :date_formats, :datetime_formats, :format_tokens, :format_components, :timezone_mapping
attr_reader :date_format_set, :time_format_set, :datetime_format_set
# Adds new formats. Must specify format type and can specify a :before
# option to nominate which format the new formats should be inserted in
# front on to take higher precedence.
#
# Error is raised if format already exists or if :before format is not found.
#
def add_formats(type, *add_formats)
formats = send("#{type}_formats")
options = add_formats.last.is_a?(Hash) ? add_formats.pop : {}
before = options[:before]
raise FormatNotFound, "Format for :before option #{before.inspect} was not found." if before && !formats.include?(before)
add_formats.each do |format|
raise DuplicateFormat, "Format #{format.inspect} is already included in #{type.inspect} formats" if formats.include?(format)
index = before ? formats.index(before) : -1
formats.insert(index, format)
end
compile_formats
end
# Delete formats of specified type. Error raised if format not found.
#
def remove_formats(type, *remove_formats)
remove_formats.each do |format|
unless send("#{type}_formats").delete(format)
raise FormatNotFound, "Format #{format.inspect} not found in #{type.inspect} formats"
end
end
compile_formats
end
# Removes US date formats so that ambiguous dates are parsed as European format
#
def use_euro_formats
@date_format_set = @euro_date_format_set
@datetime_format_set = @euro_datetime_format_set
end
# Restores default to parse ambiguous dates as US format
#
def use_us_formats
@date_format_set = @us_date_format_set
@datetime_format_set = @us_datetime_format_set
end
def compile_formats
@sorted_token_keys = nil
@time_format_set = FormatSet.compile(time_formats)
@us_date_format_set = FormatSet.compile(date_formats)
@us_datetime_format_set = FormatSet.compile(datetime_formats)
@euro_date_format_set = FormatSet.compile(date_formats.select { |format| US_FORMAT_REGEXP !~ format })
@euro_datetime_format_set = FormatSet.compile(datetime_formats.select { |format| US_FORMAT_REGEXP !~ format })
@date_format_set = @us_date_format_set
@datetime_format_set = @us_datetime_format_set
end
def sorted_token_keys
@sorted_token_keys ||= format_tokens.keys.sort {|a,b| a.size <=> b.size }.reverse
end
# Returns format for type and other possible matching format set based on type
# and value length. Gives minor speed-up by checking string length.
#
def format_sets(type, string)
case type
when :date
[ @date_format_set, @datetime_format_set ]
when :datetime
if string.length < 11
[ @date_format_set, @datetime_format_set ]
else
[ @datetime_format_set, @date_format_set ]
end
when :time
if string.length < 11
[ @time_format_set ]
else
[ @datetime_format_set, @time_format_set ]
end
else
if string.length < 11
[ @date_format_set, @time_format_set, @datetime_format_set ]
else
[ @datetime_format_set, @date_format_set, @time_format_set ]
end
end
end
end
end
end
|