Merge branch 'gem'
[fuzed.git] / helloworld / vendor / gems / chronic-0.2.2 / lib / chronic.rb
blobcc7548a540b8f6e7faa145828711dc9dddd87c8a
1 #=============================================================================
3 #  Name:       Chronic
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'
41 module Chronic
42   VERSION = "0.2.2"
43   
44   def self.debug; false; end
45 end
47 alias p_orig p
49 def p(val)
50   p_orig val
51   puts
52 end
54 # class Time
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 > 
60 #     
61 #     if month > 12
62 #       if month % 12 == 0
63 #         year += (month - 12) / 12
64 #         month = 12
65 #       else
66 #         year += month / 12
67 #         month = month % 12
68 #       end
69 #     end
70 #     
71 #     base = Time.local(year, month)
72 #     puts base
73 #     offset = ((day - 1) * 24 * 60 * 60) + (hour * 60 * 60) + (minute * 60) + second
74 #     puts offset.to_s
75 #     date = base + offset
76 #     puts date
77 #     date
78 #   end
79 # end
81 class Time
82   def self.construct(year, month = 1, day = 1, hour = 0, minute = 0, second = 0)
83     if second >= 60
84       minute += second / 60
85       second = second % 60
86     end
87     
88     if minute >= 60
89       hour += minute / 60
90       minute = minute % 60
91     end
92     
93     if hour >= 24
94       day += hour / 24
95       hour = hour % 24
96     end
97     
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)")
101     if day > 28
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
110       end
111     end
112     
113     if month > 12
114       if month % 12 == 0
115         year += (month - 12) / 12
116         month = 12
117       else
118         year += month / 12
119         month = month % 12
120       end
121     end
122     
123     Time.local(year, month, day, hour, minute, second)
124   end