1 #=============================================================================
4 # Author: Tom Preston-Werner
5 # Purpose: Parse natural language dates and times into Time or
6 # Chronic::Span objects
8 #=============================================================================
10 $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
12 require 'chronic/chronic'
13 require 'chronic/handlers'
15 require 'chronic/repeater'
16 require 'chronic/repeaters/repeater_year'
17 require 'chronic/repeaters/repeater_season'
18 require 'chronic/repeaters/repeater_season_name'
19 require 'chronic/repeaters/repeater_month'
20 require 'chronic/repeaters/repeater_month_name'
21 require 'chronic/repeaters/repeater_fortnight'
22 require 'chronic/repeaters/repeater_week'
23 require 'chronic/repeaters/repeater_weekend'
24 require 'chronic/repeaters/repeater_day'
25 require 'chronic/repeaters/repeater_day_name'
26 require 'chronic/repeaters/repeater_day_portion'
27 require 'chronic/repeaters/repeater_hour'
28 require 'chronic/repeaters/repeater_minute'
29 require 'chronic/repeaters/repeater_second'
30 require 'chronic/repeaters/repeater_time'
32 require 'chronic/grabber'
33 require 'chronic/pointer'
34 require 'chronic/scalar'
35 require 'chronic/ordinal'
36 require 'chronic/separator'
37 require 'chronic/time_zone'
39 require 'numerizer/numerizer'
44 def self.debug; false; end
55 # def self.construct(year, month = 1, day = 1, hour = 0, minute = 0, second = 0)
56 # # extra_seconds = second > 60 ? second - 60 : 0
57 # # extra_minutes = minute > 59 ? minute - 59 : 0
58 # # extra_hours = hour > 23 ? hour - 23 : 0
59 # # extra_days = day >
63 # year += (month - 12) / 12
71 # base = Time.local(year, month)
73 # offset = ((day - 1) * 24 * 60 * 60) + (hour * 60 * 60) + (minute * 60) + second
75 # date = base + offset
82 def self.construct(year, month = 1, day = 1, hour = 0, minute = 0, second = 0)
98 # determine if there is a day overflow. this is complicated by our crappy calendar
99 # system (non-constant number of days per month)
100 day <= 56 || raise("day must be no more than 56 (makes month resolution easier)")
102 # no month ever has fewer than 28 days, so only do this if necessary
103 leap_year = (year % 4 == 0) && !(year % 100 == 0)
104 leap_year_month_days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
105 common_year_month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
106 days_this_month = leap_year ? leap_year_month_days[month - 1] : common_year_month_days[month - 1]
107 if day > days_this_month
108 month += day / days_this_month
109 day = day % days_this_month
115 year += (month - 12) / 12
123 Time.local(year, month, day, hour, minute, second)