Module: Chronic::Handlers

Included in:
Parser, SpanDefinitions
Defined in:
lib/chronic/handlers.rb

Class Method Summary collapse

Class Method Details

.day_or_time(day_start, time_tokens, options) ⇒ Object

support methods



520
521
522
523
524
525
526
527
528
529
# File 'lib/chronic/handlers.rb', line 520

def day_or_time(day_start, time_tokens, options)
  outer_span = Span.new(day_start, day_start + (24 * 60 * 60))

  unless time_tokens.empty?
    self.now = outer_span.begin
    get_anchor(dealias_and_disambiguate_times(time_tokens, options), options.merge(:context => :future))
  else
    outer_span
  end
end

.dealias_and_disambiguate_times(tokens, options) ⇒ Object



611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/chronic/handlers.rb', line 611

def dealias_and_disambiguate_times(tokens, options)
  # handle aliases of am/pm
  # 5:00 in the morning -> 5:00 am
  # 7:00 in the evening -> 7:00 pm

  day_portion_index = nil
  tokens.each_with_index do |t, i|
    if t.get_tag(RepeaterDayPortion)
      day_portion_index = i
      break
    end
  end

  time_index = nil
  tokens.each_with_index do |t, i|
    if t.get_tag(RepeaterTime)
      time_index = i
      break
    end
  end

  if day_portion_index && time_index
    t1 = tokens[day_portion_index]
    t1tag = t1.get_tag(RepeaterDayPortion)

    case t1tag.type
    when :morning
      puts '--morning->am' if Chronic.debug
      t1.untag(RepeaterDayPortion)
      t1.tag(RepeaterDayPortion.new(:am))
    when :afternoon, :evening, :night
      puts "--#{t1tag.type}->pm" if Chronic.debug
      t1.untag(RepeaterDayPortion)
      t1.tag(RepeaterDayPortion.new(:pm))
    end
  end

  # handle ambiguous times if :ambiguous_time_range is specified
  if options[:ambiguous_time_range] != :none
    ambiguous_tokens = []

    tokens.each_with_index do |token, i|
      ambiguous_tokens << token
      next_token = tokens[i + 1]

      if token.get_tag(RepeaterTime) && token.get_tag(RepeaterTime).type.ambiguous? && (!next_token || !next_token.get_tag(RepeaterDayPortion))
        distoken = Token.new('disambiguator')

        distoken.tag(RepeaterDayPortion.new(options[:ambiguous_time_range]))
        ambiguous_tokens << distoken
      end
    end

    tokens = ambiguous_tokens
  end

  tokens
end

.find_within(tags, span, pointer) ⇒ Object

Recursively finds repeaters within other repeaters. Returns a Span representing the innermost time span or nil if no repeater union could be found



584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/chronic/handlers.rb', line 584

def find_within(tags, span, pointer)
  puts "--#{span}" if Chronic.debug
  return span if tags.empty?

  head = tags.shift
  head.start = (pointer == :future ? span.begin : span.end)
  h = head.this(:none)

  if span.cover?(h.begin) || span.cover?(h.end)
    find_within(tags, h, pointer)
  end
end

.get_anchor(tokens, options) ⇒ Object



531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/chronic/handlers.rb', line 531

def get_anchor(tokens, options)
  grabber = Grabber.new(:this)
  pointer = :future
  repeaters = get_repeaters(tokens)
  repeaters.size.times { tokens.pop }

  if tokens.first && tokens.first.get_tag(Grabber)
    grabber = tokens.shift.get_tag(Grabber)
  end

  head = repeaters.shift
  head.start = self.now

  case grabber.type
  when :last
    outer_span = head.next(:past)
  when :this
    if options[:context] != :past and repeaters.size > 0
      outer_span = head.this(:none)
    else
      outer_span = head.this(options[:context])
    end
  when :next
    outer_span = head.next(:future)
  else
    raise 'Invalid grabber'
  end

  if Chronic.debug
    puts "Handler-class: #{head.class}"
    puts "--#{outer_span}"
  end

  find_within(repeaters, outer_span, pointer)
end

.get_repeaters(tokens) ⇒ Object



567
568
569
# File 'lib/chronic/handlers.rb', line 567

def get_repeaters(tokens)
  tokens.map { |token| token.get_tag(Repeater) }.compact.sort.reverse
end

.handle_generic(tokens, options) ⇒ Object

Handle generic timestamp (ruby 1.8)



148
149
150
151
152
153
# File 'lib/chronic/handlers.rb', line 148

def handle_generic(tokens, options)
  t = Chronic.time_class.parse(options[:text])
  Span.new(t, t + 1)
rescue ArgumentError => e
  raise e unless e.message =~ /out of range/
end

.handle_m_d(month, day, time_tokens, options) ⇒ Object

Handle month/day



6
7
8
9
10
11
12
13
14
# File 'lib/chronic/handlers.rb', line 6

def handle_m_d(month, day, time_tokens, options)
  month.start = self.now
  span = month.this(options[:context])
  year, month = span.begin.year, span.begin.month
  day_start = Chronic.time_class.local(year, month, day)
  day_start = Chronic.time_class.local(year + 1, month, day) if options[:context] == :future && day_start < now

  day_or_time(day_start, time_tokens, options)
end

.handle_o_r_g_r(tokens, options) ⇒ Object

Handle ordinal/repeater/grabber/repeater



513
514
515
516
# File 'lib/chronic/handlers.rb', line 513

def handle_o_r_g_r(tokens, options)
  outer_span = get_anchor(tokens[2..3], options)
  handle_orr(tokens[0..1], outer_span, options)
end

.handle_o_r_s_r(tokens, options) ⇒ Object

Handle ordinal/repeater/separator/repeater



507
508
509
510
# File 'lib/chronic/handlers.rb', line 507

def handle_o_r_s_r(tokens, options)
  outer_span = get_anchor([tokens[3]], options)
  handle_orr(tokens[0..1], outer_span, options)
end

.handle_od_rm(tokens, options) ⇒ Object

Handle ordinal this month



54
55
56
57
58
# File 'lib/chronic/handlers.rb', line 54

def handle_od_rm(tokens, options)
  day = tokens[0].get_tag(OrdinalDay).type
  month = tokens[2].get_tag(RepeaterMonth)
  handle_m_d(month, day, tokens[3..tokens.size], options)
end

.handle_od_rmn(tokens, options) ⇒ Object

Handle ordinal-day/repeater-month-name



61
62
63
64
65
66
67
68
# File 'lib/chronic/handlers.rb', line 61

def handle_od_rmn(tokens, options)
  month = tokens[1].get_tag(RepeaterMonthName)
  day = tokens[0].get_tag(OrdinalDay).type

  return if month_overflow?(self.now.year, month.index, day)

  handle_m_d(month, day, tokens[2..tokens.size], options)
end

.handle_od_rmn_sy(tokens, options) ⇒ Object

Handle oridinal-day/repeater-month-name/scalar-year



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/chronic/handlers.rb', line 190

def handle_od_rmn_sy(tokens, options)
  day = tokens[0].get_tag(OrdinalDay).type
  month = tokens[1].get_tag(RepeaterMonthName).index
  year = tokens[2].get_tag(ScalarYear).type
  time_tokens = tokens.last(tokens.size - 3)

  return if month_overflow?(year, month, day)

  begin
    day_start = Chronic.time_class.local(year, month, day)
    day_or_time(day_start, time_tokens, options)
  rescue ArgumentError
    nil
  end
end

.handle_orr(tokens, outer_span, options) ⇒ Object

Handle oridinal repeaters



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/chronic/handlers.rb', line 488

def handle_orr(tokens, outer_span, options)
  repeater = tokens[1].get_tag(Repeater)
  repeater.start = outer_span.begin - 1
  ordinal = tokens[0].get_tag(Ordinal).type
  span = nil

  ordinal.times do
    span = repeater.next(:future)

    if span.begin >= outer_span.end
      span = nil
      break
    end
  end

  span
end

.handle_p_s_r(tokens, options) ⇒ Object

Handle pointer/scalar/repeater



462
463
464
465
# File 'lib/chronic/handlers.rb', line 462

def handle_p_s_r(tokens, options)
  new_tokens = [tokens[1], tokens[2], tokens[0]]
  handle_s_r_p(new_tokens, options)
end

.handle_r(tokens, options) ⇒ Object

Handle repeaters



432
433
434
435
# File 'lib/chronic/handlers.rb', line 432

def handle_r(tokens, options)
  dd_tokens = dealias_and_disambiguate_times(tokens, options)
  get_anchor(dd_tokens, options)
end

.handle_r_g_r(tokens, options) ⇒ Object

Handle repeater/grabber/repeater



438
439
440
441
# File 'lib/chronic/handlers.rb', line 438

def handle_r_g_r(tokens, options)
  new_tokens = [tokens[1], tokens[0], tokens[2]]
  handle_r(new_tokens, options)
end

.handle_rdn_od(tokens, options) ⇒ Object

Handle RepeaterDayName OrdinalDay



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/chronic/handlers.rb', line 347

def handle_rdn_od(tokens, options)
  day = tokens[1].get_tag(OrdinalDay).type
  time_tokens = tokens.last(tokens.size - 2)
  year = self.now.year
  month = self.now.month
  if options[:context] == :future
    self.now.day > day ? month += 1 : month
  end

  return if month_overflow?(year, month, day)

  begin
    if time_tokens.empty?
      start_time = Chronic.time_class.local(year, month, day)
      end_time = time_with_rollover(year, month, day + 1)
      Span.new(start_time, end_time)
    else
      day_start = Chronic.time_class.local(year, month, day)
      day_or_time(day_start, time_tokens, options)
    end
  rescue ArgumentError
    nil
  end
end

.handle_rdn_rmn_od(tokens, options) ⇒ Object

Handle RepeaterDayName RepeaterMonthName OrdinalDay



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/chronic/handlers.rb', line 307

def handle_rdn_rmn_od(tokens, options)
  month = tokens[1].get_tag(RepeaterMonthName)
  day = tokens[2].get_tag(OrdinalDay).type
  time_tokens = tokens.last(tokens.size - 3)
  year = self.now.year

  return if month_overflow?(year, month.index, day)

  begin
    if time_tokens.empty?
      start_time = Chronic.time_class.local(year, month.index, day)
      end_time = time_with_rollover(year, month.index, day + 1)
      Span.new(start_time, end_time)
    else
      day_start = Chronic.time_class.local(year, month.index, day)
      day_or_time(day_start, time_tokens, options)
    end
  rescue ArgumentError
    nil
  end
end

.handle_rdn_rmn_od_sy(tokens, options) ⇒ Object

Handle RepeaterDayName RepeaterMonthName OrdinalDay ScalarYear



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/chronic/handlers.rb', line 330

def handle_rdn_rmn_od_sy(tokens, options)
  month = tokens[1].get_tag(RepeaterMonthName)
  day = tokens[2].get_tag(OrdinalDay).type
  year = tokens[3].get_tag(ScalarYear).type

  return if month_overflow?(year, month.index, day)

  begin
    start_time = Chronic.time_class.local(year, month.index, day)
    end_time = time_with_rollover(year, month.index, day + 1)
    Span.new(start_time, end_time)
  rescue ArgumentError
    nil
  end
end

.handle_rdn_rmn_sd(tokens, options) ⇒ Object

Handle RepeaterDayName RepeaterMonthName ScalarDay



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/chronic/handlers.rb', line 373

def handle_rdn_rmn_sd(tokens, options)
  month = tokens[1].get_tag(RepeaterMonthName)
  day = tokens[2].get_tag(ScalarDay).type
  time_tokens = tokens.last(tokens.size - 3)
  year = self.now.year

  return if month_overflow?(year, month.index, day)

  begin
    if time_tokens.empty?
      start_time = Chronic.time_class.local(year, month.index, day)
      end_time = time_with_rollover(year, month.index, day + 1)
      Span.new(start_time, end_time)
    else
      day_start = Chronic.time_class.local(year, month.index, day)
      day_or_time(day_start, time_tokens, options)
    end
  rescue ArgumentError
    nil
  end
end

.handle_rdn_rmn_sd_sy(tokens, options) ⇒ Object

Handle RepeaterDayName RepeaterMonthName ScalarDay ScalarYear



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/chronic/handlers.rb', line 396

def handle_rdn_rmn_sd_sy(tokens, options)
  month = tokens[1].get_tag(RepeaterMonthName)
  day = tokens[2].get_tag(ScalarDay).type
  year = tokens[3].get_tag(ScalarYear).type

  return if month_overflow?(year, month.index, day)

  begin
    start_time = Chronic.time_class.local(year, month.index, day)
    end_time = time_with_rollover(year, month.index, day + 1)
    Span.new(start_time, end_time)
  rescue ArgumentError
    nil
  end
end

.handle_rmn_od(tokens, options) ⇒ Object

Handle repeater-month-name/ordinal-day



44
45
46
47
48
49
50
51
# File 'lib/chronic/handlers.rb', line 44

def handle_rmn_od(tokens, options)
  month = tokens[0].get_tag(RepeaterMonthName)
  day = tokens[1].get_tag(OrdinalDay).type

  return if month_overflow?(self.now.year, month.index, day)

  handle_m_d(month, day, tokens[2..tokens.size], options)
end

.handle_rmn_od_on(tokens, options) ⇒ Object

Handle repeater-month-name/ordinal-day with separator-on



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chronic/handlers.rb', line 97

def handle_rmn_od_on(tokens, options)
  if tokens.size > 3
    month = tokens[2].get_tag(RepeaterMonthName)
    day = tokens[3].get_tag(OrdinalDay).type
    token_range = 0..1
  else
    month = tokens[1].get_tag(RepeaterMonthName)
    day = tokens[2].get_tag(OrdinalDay).type
    token_range = 0..0
  end

  return if month_overflow?(self.now.year, month.index, day)

  handle_m_d(month, day, tokens[token_range], options)
end

.handle_rmn_od_sy(tokens, options) ⇒ Object

Handle repeater-month-name/ordinal-day/scalar-year



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/chronic/handlers.rb', line 173

def handle_rmn_od_sy(tokens, options)
  month = tokens[0].get_tag(RepeaterMonthName).index
  day = tokens[1].get_tag(OrdinalDay).type
  year = tokens[2].get_tag(ScalarYear).type
  time_tokens = tokens.last(tokens.size - 3)

  return if month_overflow?(year, month, day)

  begin
    day_start = Chronic.time_class.local(year, month, day)
    day_or_time(day_start, time_tokens, options)
  rescue ArgumentError
    nil
  end
end

.handle_rmn_s_r_p(tokens, options) ⇒ Object

Handle repeater/scalar/repeater/pointer



474
475
476
# File 'lib/chronic/handlers.rb', line 474

def handle_rmn_s_r_p(tokens, options)
  handle_s_r_p_a(tokens[1..3] + tokens[0..0], options)
end

.handle_rmn_sd(tokens, options) ⇒ Object

Handle repeater-month-name/scalar-day



17
18
19
20
21
22
23
24
# File 'lib/chronic/handlers.rb', line 17

def handle_rmn_sd(tokens, options)
  month = tokens[0].get_tag(RepeaterMonthName)
  day = tokens[1].get_tag(ScalarDay).type

  return if month_overflow?(self.now.year, month.index, day)

  handle_m_d(month, day, tokens[2..tokens.size], options)
end

.handle_rmn_sd_on(tokens, options) ⇒ Object

Handle repeater-month-name/scalar-day with separator-on



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chronic/handlers.rb', line 27

def handle_rmn_sd_on(tokens, options)
  if tokens.size > 3
    month = tokens[2].get_tag(RepeaterMonthName)
    day = tokens[3].get_tag(ScalarDay).type
    token_range = 0..1
  else
    month = tokens[1].get_tag(RepeaterMonthName)
    day = tokens[2].get_tag(ScalarDay).type
    token_range = 0..0
  end

  return if month_overflow?(self.now.year, month.index, day)

  handle_m_d(month, day, tokens[token_range], options)
end

.handle_rmn_sd_sy(tokens, options) ⇒ Object

Handle repeater-month-name/scalar-day/scalar-year



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/chronic/handlers.rb', line 156

def handle_rmn_sd_sy(tokens, options)
  month = tokens[0].get_tag(RepeaterMonthName).index
  day = tokens[1].get_tag(ScalarDay).type
  year = tokens[2].get_tag(ScalarYear).type
  time_tokens = tokens.last(tokens.size - 3)

  return if month_overflow?(year, month, day)

  begin
    day_start = Chronic.time_class.local(year, month, day)
    day_or_time(day_start, time_tokens, options)
  rescue ArgumentError
    nil
  end
end

.handle_rmn_sy(tokens, options) ⇒ Object

Handle repeater-month-name/scalar-year



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/chronic/handlers.rb', line 127

def handle_rmn_sy(tokens, options)
  month = tokens[0].get_tag(RepeaterMonthName).index
  year = tokens[1].get_tag(ScalarYear).type

  if month == 12
    next_month_year = year + 1
    next_month_month = 1
  else
    next_month_year = year
    next_month_month = month + 1
  end

  begin
    end_time = Chronic.time_class.local(next_month_year, next_month_month)
    Span.new(Chronic.time_class.local(year, month), end_time)
  rescue ArgumentError
    nil
  end
end

.handle_rqn_sy(tokens, options) ⇒ Object

Handle repeater-quarter-name/scalar-year



119
120
121
122
123
124
# File 'lib/chronic/handlers.rb', line 119

def handle_rqn_sy(tokens, options)
  year = tokens[1].get_tag(ScalarYear).type
  quarter_tag = tokens[0].get_tag(RepeaterQuarterName)
  quarter_tag.start = Chronic.construct(year)
  quarter_tag.this(:none)
end

.handle_s_r_a_s_r_p_a(tokens, options) ⇒ Object



478
479
480
481
482
483
# File 'lib/chronic/handlers.rb', line 478

def handle_s_r_a_s_r_p_a(tokens, options)
  anchor_span = get_anchor(tokens[4..tokens.size - 1], options)

  span = handle_srp(tokens[0..1]+tokens[4..6], anchor_span, options)
  handle_srp(tokens[2..3]+tokens[4..6], span, options)
end

.handle_s_r_p(tokens, options) ⇒ Object

Handle scalar/repeater/pointer



455
456
457
458
459
# File 'lib/chronic/handlers.rb', line 455

def handle_s_r_p(tokens, options)
  span = Span.new(self.now, self.now + 1)

  handle_srp(tokens, span, options)
end

.handle_s_r_p_a(tokens, options) ⇒ Object

Handle scalar/repeater/pointer/anchor



468
469
470
471
# File 'lib/chronic/handlers.rb', line 468

def handle_s_r_p_a(tokens, options)
  anchor_span = get_anchor(tokens[3..tokens.size - 1], options)
  handle_srp(tokens, anchor_span, options)
end

.handle_sd_rmn(tokens, options) ⇒ Object

Handle scalar-day/repeater-month-name



87
88
89
90
91
92
93
94
# File 'lib/chronic/handlers.rb', line 87

def handle_sd_rmn(tokens, options)
  month = tokens[1].get_tag(RepeaterMonthName)
  day = tokens[0].get_tag(ScalarDay).type

  return if month_overflow?(self.now.year, month.index, day)

  handle_m_d(month, day, tokens[2..tokens.size], options)
end

.handle_sd_rmn_sy(tokens, options) ⇒ Object

Handle scalar-day/repeater-month-name/scalar-year



207
208
209
210
211
# File 'lib/chronic/handlers.rb', line 207

def handle_sd_rmn_sy(tokens, options)
  new_tokens = [tokens[1], tokens[0], tokens[2]]
  time_tokens = tokens.last(tokens.size - 3)
  handle_rmn_sd_sy(new_tokens + time_tokens, options)
end

.handle_sd_sm(tokens, options) ⇒ Object

Handle scalar-day/scalar-month



269
270
271
272
273
# File 'lib/chronic/handlers.rb', line 269

def handle_sd_sm(tokens, options)
  new_tokens = [tokens[1], tokens[0]]
  time_tokens = tokens.last(tokens.size - 2)
  handle_sm_sd(new_tokens + time_tokens, options)
end

.handle_sd_sm_sy(tokens, options) ⇒ Object

Handle scalar-day/scalar-month/scalar-year (endian little)



231
232
233
234
235
# File 'lib/chronic/handlers.rb', line 231

def handle_sd_sm_sy(tokens, options)
  new_tokens = [tokens[1], tokens[0], tokens[2]]
  time_tokens = tokens.last(tokens.size - 3)
  handle_sm_sd_sy(new_tokens + time_tokens, options)
end

.handle_sm_rmn_sy(tokens, options) ⇒ Object



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/chronic/handlers.rb', line 412

def handle_sm_rmn_sy(tokens, options)
  day = tokens[0].get_tag(ScalarDay).type
  month = tokens[1].get_tag(RepeaterMonthName).index
  year = tokens[2].get_tag(ScalarYear).type
  if tokens.size > 3
    time = get_anchor([tokens.last], options).begin
    h, m, s = time.hour, time.min, time.sec
    time = Chronic.time_class.local(year, month, day, h, m, s)
    end_time = Chronic.time_class.local(year, month, day + 1, h, m, s)
  else
    time = Chronic.time_class.local(year, month, day)
    day += 1 unless day >= 31
    end_time = Chronic.time_class.local(year, month, day)
  end
  Span.new(time, end_time)
end

.handle_sm_sd(tokens, options) ⇒ Object

Handle scalar-month/scalar-day



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/chronic/handlers.rb', line 245

def handle_sm_sd(tokens, options)
  month = tokens[0].get_tag(ScalarMonth).type
  day = tokens[1].get_tag(ScalarDay).type
  year = self.now.year
  time_tokens = tokens.last(tokens.size - 2)

  return if month_overflow?(year, month, day)

  begin
    day_start = Chronic.time_class.local(year, month, day)

    if options[:context] == :future && day_start < now
      day_start = Chronic.time_class.local(year + 1, month, day)
    elsif options[:context] == :past && day_start > now
      day_start = Chronic.time_class.local(year - 1, month, day)
    end

    day_or_time(day_start, time_tokens, options)
  rescue ArgumentError
    nil
  end
end

.handle_sm_sd_sy(tokens, options) ⇒ Object

Handle scalar-month/scalar-day/scalar-year (endian middle)



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/chronic/handlers.rb', line 214

def handle_sm_sd_sy(tokens, options)
  month = tokens[0].get_tag(ScalarMonth).type
  day = tokens[1].get_tag(ScalarDay).type
  year = tokens[2].get_tag(ScalarYear).type
  time_tokens = tokens.last(tokens.size - 3)

  return if month_overflow?(year, month, day)

  begin
    day_start = Chronic.time_class.local(year, month, day)
    day_or_time(day_start, time_tokens, options)
  rescue ArgumentError
    nil
  end
end

.handle_sm_sy(tokens, options) ⇒ Object

Handle scalar-month/scalar-year



293
294
295
296
297
# File 'lib/chronic/handlers.rb', line 293

def handle_sm_sy(tokens, options)
  month = tokens[0].get_tag(ScalarMonth).type
  year = tokens[1].get_tag(ScalarYear).type
  handle_year_and_month(year, month)
end

.handle_srp(tokens, span, options) ⇒ Object

Handle scalar/repeater/pointer helper



446
447
448
449
450
451
452
# File 'lib/chronic/handlers.rb', line 446

def handle_srp(tokens, span, options)
  distance = tokens[0].get_tag(Scalar).type
  repeater = tokens[1].get_tag(Repeater)
  pointer = tokens[2].get_tag(Pointer).type

  repeater.offset(span, distance, pointer) if repeater.respond_to?(:offset)
end

.handle_sy_rmn_od(tokens, options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chronic/handlers.rb', line 70

def handle_sy_rmn_od(tokens, options)
  year = tokens[0].get_tag(ScalarYear).type
  month = tokens[1].get_tag(RepeaterMonthName).index
  day = tokens[2].get_tag(OrdinalDay).type
  time_tokens = tokens.last(tokens.size - 3)

  return if month_overflow?(year, month, day)

  begin
    day_start = Chronic.time_class.local(year, month, day)
    day_or_time(day_start, time_tokens, options)
  rescue ArgumentError
    nil
  end
end

.handle_sy_rqn(tokens, options) ⇒ Object

Handle scalar-year/repeater-quarter-name



114
115
116
# File 'lib/chronic/handlers.rb', line 114

def handle_sy_rqn(tokens, options)
  handle_rqn_sy(tokens[0..1].reverse, options)
end

.handle_sy_sm(tokens, options) ⇒ Object

Handle scalar-year/scalar-month



300
301
302
303
304
# File 'lib/chronic/handlers.rb', line 300

def handle_sy_sm(tokens, options)
  year = tokens[0].get_tag(ScalarYear).type
  month = tokens[1].get_tag(ScalarMonth).type
  handle_year_and_month(year, month)
end

.handle_sy_sm_sd(tokens, options) ⇒ Object

Handle scalar-year/scalar-month/scalar-day



238
239
240
241
242
# File 'lib/chronic/handlers.rb', line 238

def handle_sy_sm_sd(tokens, options)
  new_tokens = [tokens[1], tokens[2], tokens[0]]
  time_tokens = tokens.last(tokens.size - 3)
  handle_sm_sd_sy(new_tokens + time_tokens, options)
end

.handle_year_and_month(year, month) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/chronic/handlers.rb', line 275

def handle_year_and_month(year, month)
  if month == 12
    next_month_year = year + 1
    next_month_month = 1
  else
    next_month_year = year
    next_month_month = month + 1
  end

  begin
    end_time = Chronic.time_class.local(next_month_year, next_month_month)
    Span.new(Chronic.time_class.local(year, month), end_time)
  rescue ArgumentError
    nil
  end
end

.month_overflow?(year, month, day) ⇒ Boolean

Returns:

  • (Boolean)


571
572
573
574
575
576
577
578
579
# File 'lib/chronic/handlers.rb', line 571

def month_overflow?(year, month, day)
  if ::Date.leap?(year)
    day > RepeaterMonth::MONTH_DAYS_LEAP[month - 1]
  else
    day > RepeaterMonth::MONTH_DAYS[month - 1]
  end
rescue ArgumentError
  false
end

.time_with_rollover(year, month, day) ⇒ Object



597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/chronic/handlers.rb', line 597

def time_with_rollover(year, month, day)
  date_parts =
    if month_overflow?(year, month, day)
      if month == 12
        [year + 1, 1, 1]
      else
        [year, month + 1, 1]
      end
    else
      [year, month, day]
    end
  Chronic.time_class.local(*date_parts)
end