class Cronic::Parser

Included Modules

Defined in:

cronic/parser.cr

Constructors

Instance Method Summary

Instance methods inherited from module Cronic::Handlers

day_or_time(day_start : Time, time_tokens, context : PointerDir = PointerDir::Future, **options) : Timespan | Nil day_or_time, find_within(tags, span : Nil, pointer : PointerDir)
find_within(tags, span : Timespan, pointer : PointerDir) : Timespan | Nil
find_within
, get_anchor(tokens, context = PointerDir::None, **options) get_anchor, get_repeaters(tokens) get_repeaters, handle_generic(tokens, text = "", **options) handle_generic, handle_o_r_g_r(tokens, **options) handle_o_r_g_r, handle_o_r_s_r(tokens, **options) handle_o_r_s_r, handle_od_rm(tokens, **options) handle_od_rm, handle_od_rmn(tokens, **options) : Timespan | Nil handle_od_rmn, handle_od_rmn_sy(tokens, **options) handle_od_rmn_sy, handle_ordday(tokens, text = "", **options) handle_ordday, handle_p_s_r(tokens, **options) handle_p_s_r, handle_r(tokens, **options) handle_r, handle_r_g_r(tokens, **options) handle_r_g_r, handle_rdn_od(tokens, context = PointerDir::Future, **options) handle_rdn_od, handle_rdn_rmn_od(tokens, **options) handle_rdn_rmn_od, handle_rdn_rmn_od_sy(tokens, **options) handle_rdn_rmn_od_sy, handle_rdn_rmn_sd(tokens, **options) handle_rdn_rmn_sd, handle_rdn_rmn_sd_sy(tokens, **options) handle_rdn_rmn_sd_sy, handle_rdn_rmn_sd_t_tz_sy(tokens, text = "", **opts) handle_rdn_rmn_sd_t_tz_sy, handle_rfc3339(tokens, text = "", **options) handle_rfc3339, handle_rfc3339_no_tz(tokens, text = "", **options) handle_rfc3339_no_tz, handle_rmn_od(tokens, **options) handle_rmn_od, handle_rmn_od_on(tokens, **options) handle_rmn_od_on, handle_rmn_od_sy(tokens, **options) handle_rmn_od_sy, handle_rmn_s_r_p(tokens, **options) handle_rmn_s_r_p, handle_rmn_sd(tokens, **options) handle_rmn_sd, handle_rmn_sd_on(tokens, **options) handle_rmn_sd_on, handle_rmn_sd_sy(tokens, **options) handle_rmn_sd_sy, handle_rmn_sy(tokens, **options) handle_rmn_sy, handle_rqn_sy(tokens, **options) handle_rqn_sy, handle_s_r_a_s_r_p_a(tokens, **options) handle_s_r_a_s_r_p_a, handle_s_r_p(tokens, **options) handle_s_r_p, handle_s_r_p_a(tokens, **options) handle_s_r_p_a, handle_sd_rmn(tokens, **options) handle_sd_rmn, handle_sd_rmn_sy(tokens, **options) handle_sd_rmn_sy, handle_sd_sm(tokens, **options) handle_sd_sm, handle_sd_sm_sy(tokens, **options) handle_sd_sm_sy, handle_sm_rmn_sy(tokens, **options) handle_sm_rmn_sy, handle_sm_sd(tokens, context = PointerDir::Future, **options) handle_sm_sd, handle_sm_sd_sy(tokens, **options) handle_sm_sd_sy, handle_sm_sy(tokens, **options) handle_sm_sy, handle_sy_rmn_od(tokens, **options) handle_sy_rmn_od, handle_sy_rqn(tokens, **options) handle_sy_rqn, handle_sy_sm(tokens, **options) handle_sy_sm, handle_sy_sm_sd(tokens, **options) handle_sy_sm_sd, handle_year_and_month(year : Int32, month : Int32) handle_year_and_month, subhandle_m_d(month, day, time_tokens, context = PointerDir::None, **options) subhandle_m_d, subhandle_orr(tokens, outer_span, **options) subhandle_orr, subhandle_srp(tokens, span : Timespan, **options) : Timespan | Nil subhandle_srp, xxhandle_rfc3339_no_tz(tokens, text = "", **options) xxhandle_rfc3339_no_tz

Class methods inherited from module Cronic::Handlers

dealias_and_disambiguate_times(tokens, ambiguous_time_range : Int32 | Nil = 6, **options) dealias_and_disambiguate_times

Constructor Detail

def self.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.

[View source]

Instance Method Detail

def 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.


[View source]
def now : Time #

The current reference Time. Defaults to Time.local


[View source]
def now=(now : Time) #

The current reference Time. Defaults to Time.local


[View source]
def 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.


[View source]
def parse_span(text : String) : Timespan #

Parse text into a Timespan


[View source]
def 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.


[View source]
def tokenize(text, **options) : Array(Token) #

Process text into tagged tokens


[View source]