class

Cronic::Parser

Inherits Cronic::Handlers < Reference < Object

Constructors

new(context : PointerDir = PointerDir::Future, now : Time = Time.local, hours24 : Bool | Nil = nil, week_start : Time::DayOfWeek = Time::DayOfWeek::Sunday, ambiguous_time_range : Int32 | Nil = 6, endian_precedence : Array(DateEndian) = [DateEndian::MonthDay, DateEndian::DayMonth], ambiguous_year_future_bias : Int = 50)

Constructs and holds options for a Parser object.

Options include:

  • context - If your string represents a birthday, you can set this value to PointerDir::Past and if an ambiguous string is given, it will assume it is in the past.
  • now - Time, all computations will be based off of this time instead of Time.now.
  • hours24 - Time will be parsed as it would be 24 hour clock.
  • week_start - By default, the parser assesses weeks start on sunday but you can change this value to Time::DayOfWeek::Monday if needed.
  • ambiguous_time_range - If an Integer is given, ambiguous times (like 5:00) will be assumed to be within the range of that time in the AM to that time in the PM. For example, if you set it to 7, then the parser will look for the time between 7am and 7pm. In the case of 5:00, it would assume that means 5:00pm. If :none is given, no assumption will be made, and the first matching instance of that time will be used.
  • endian_precedence - By default, Cronic will parse "03/04/2011" as the fourth day of the third month. Alternatively you can tell Cronic to parse this as the third day of the fourth month by setting this to [DateEndian::DayMonth, DateEndian::MonthDay].
  • ambiguous_year_future_bias - When parsing two digit years (i.e. 79), Cronic will attempt to assume the full year using this figure. Cronic will look x amount of years into the future and past. If the two digit year is now + x years it's assumed to be the future, now - x years is assumed to be the past.
Source

Instance methods

guess(span : Timespan, mode : Guess = Guess::Middle) : Time

Guess a specific time within the given span. mode can be provided to select begin/middle/end of the span.

Source
now

The current reference Time. Defaults to Time.local

Source
now=(now : Time)

The current reference Time. Defaults to Time.local

Source
parse(text : String, guess = Cronic::Guess::Middle)

Parse text to a singular Time.

Setting guess to Guess::End will return last time from the Timespan, to Guess::Middle for the middle of the span and Guess::Begin for first time in the span.

Source
parse_span(text : String) : Timespan

Parse text into a Timespan

Source
pre_normalize(text : String) : String

Clean up the specified text ready for parsing.

Clean up the string by stripping unwanted characters, converting idioms to their canonical form, converting number words to numbers (three3), and converting ordinal words to numeric ordinals (third3rd)

Examples:

Cronic.pre_normalize('first day in May')
# => "1st day in may"

Cronic.pre_normalize('tomorrow after noon')
# => "next day future 12:00"

Cronic.pre_normalize('one hundred and thirty six days from now')
# => "136 days future this second"

Returns a new String ready for Cronic to parse.

Source
tokenize(text, **options) : Array(Token)

Process text into tagged tokens

Source