-
Notifications
You must be signed in to change notification settings - Fork 233
Plugin Parser
The plugin parser is not enabled by default. You need to change the following config setting:
config.use_plugin_parser = true
The parser has default formats included which can be easily added using the plugins format rules. Also formats can be easily removed without hacking the plugin at all.
For information on the default formats and how to create your own, please read the Timeliness gem documentation.
The perenial problem for non-US developers or applications not primarily for the US, is the US date format of m/d/yy. This is ambiguous with the European format of d/m/yy. By default the plugin uses the US formats as this is the Ruby default when it does date interpretation.
To switch to using the European (or Rest of The World) formats put this in the initializer.
config.parser.use_euro_formats
Now ‘01/02/2000’ will be parsed as 1st February 2000, instead of 2nd January 2000.
To switch back or to make it explicit, you can set it to US formats
config.parser.use_us_formats
Sometimes you may not want certain formats to be valid. You can remove formats for each type and the parser will then not consider that a valid format. To remove a format stick this in the initializer
config.parser.remove_formats(:date, 'm\d\yy')
Adding new formats is simple. Again stick this in the initializer file
config.parser.add_formats(:time, "d o'clock")
Now “10 o’clock” will be a valid value.
You can embed regular expressions in the format but no gurantees that it will remain intact. If you avoid the use of any token characters and regexp dots or backslashes as special characters in the regexp, it may well work as expected. For special characters use POSIX character classes for safety. See the ISO 8601 datetime for an example of an embedded regular expression.
Because formats are evaluated in order, adding a format which may be ambiguous with an existing format, will mean your format is ignored. If you need to make your new format higher precedence than an existing format, you can include the before option like so
config.parser.add_formats(:time, 'ss:nn:hh', :before => 'hh:nn:ss')
Now a time of ‘59:30:23’ will be interpreted as 11:30:59 pm. This option saves you adding a new one and deleting an old one to get it to work.
When dealing with 2 digit year values, by default a year is interpreted as being in the last century when at or above 30. You can customize this however
config.parser.ambiguous_year_threshold = 20
Now you get:
year of 19 is considered 2019 year of 20 is considered 1920