-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathchronic.rb
More file actions
202 lines (177 loc) · 5.4 KB
/
chronic.rb
File metadata and controls
202 lines (177 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
require 'time'
require 'date'
require 'chronic/parser'
require 'chronic/handler'
require 'chronic/handlers'
require 'chronic/mini_date'
require 'chronic/tag'
require 'chronic/span'
require 'chronic/token'
require 'chronic/grabber'
require 'chronic/pointer'
require 'chronic/scalar'
require 'chronic/ordinal'
require 'chronic/separator'
require 'chronic/time_zone'
require 'chronic/numerizer'
require 'chronic/season'
require 'chronic/repeater'
require 'chronic/repeaters/repeater_year'
require 'chronic/repeaters/repeater_season'
require 'chronic/repeaters/repeater_season_name'
require 'chronic/repeaters/repeater_month'
require 'chronic/repeaters/repeater_month_name'
require 'chronic/repeaters/repeater_fortnight'
require 'chronic/repeaters/repeater_week'
require 'chronic/repeaters/repeater_weekend'
require 'chronic/repeaters/repeater_weekday'
require 'chronic/repeaters/repeater_day'
require 'chronic/repeaters/repeater_day_name'
require 'chronic/repeaters/repeater_day_portion'
require 'chronic/repeaters/repeater_hour'
require 'chronic/repeaters/repeater_minute'
require 'chronic/repeaters/repeater_second'
require 'chronic/repeaters/repeater_time'
# Parse natural language dates and times into Time or Chronic::Span objects.
#
# Examples:
#
# require 'chronic'
#
# Time.now #=> Sun Aug 27 23:18:25 PDT 2006
#
# Chronic.parse('tomorrow')
# #=> Mon Aug 28 12:00:00 PDT 2006
#
# Chronic.parse('monday', :context => :past)
# #=> Mon Aug 21 12:00:00 PDT 2006
module Chronic
VERSION = "0.9.1"
class << self
# Returns true when debug mode is enabled.
attr_accessor :debug
# Examples:
#
# require 'chronic'
# require 'active_support/time'
#
# Time.zone = 'UTC'
# Chronic.time_class = Time.zone
# Chronic.parse('June 15 2006 at 5:54 AM')
# # => Thu, 15 Jun 2006 05:45:00 UTC +00:00
#
# Returns The Time class Chronic uses internally.
attr_accessor :time_class
# Returns the available locales that Chronic can use
attr_accessor :locale_hashes
# The current locale Chronic is using to parse strings
#
# Examples:
#
# require 'chronic'
#
# Chronic.locale = :'pt-BR'
# Chronic.parse('15 de Junho de 2006 as 5:54 da manha ')
# # => Thu, 15 Jun 2006 05:45:00 UTC +00:00
#
# Returns the locale name Chronic uses internally
attr_accessor :locale
end
self.debug = false
self.time_class = Time
self.locale = :en
require 'chronic/locales/en'
self.locale_hashes = {
:en => Chronic::Locales::EN
}
# Parses a string containing a natural language date or time.
#
# If the parser can find a date or time, either a Time or Chronic::Span
# will be returned (depending on the value of `:guess`). If no
# date or time can be found, `nil` will be returned.
#
# text - The String text to parse.
# opts - An optional Hash of configuration options passed to Parser::new.
def self.parse(text, options = {})
# ensure current locale is available
raise ArgumentError, "#{locale} is not an available locale" unless has_locale(locale)
Parser.new(options).parse(text)
end
# Adds a locale to the locale hash
#
# name - Symbol locale name
# locale - Hash locale values
def self.add_locale(name, locale)
raise ArgumentError, "Locale shoud be a hash" unless locale.is_a?(Hash)
locale_hashes[name] = locale
end
# Checks if a locale is available
#
# name - Symbol locale name
#
# Returns true if the locale is available, false if not
def self.has_locale(name)
locale_hashes.include? name
end
# Returns the translations for the current locale
def self.translate(keys, loc=nil)
loc ||= locale
node = locale_hashes[loc]
keys.each do |key|
if node.include? key
node = node[key]
else
return translate(keys, :en)
end
end
node
end
# Construct a new time object determining possible month overflows
# and leap years.
#
# year - Integer year.
# month - Integer month.
# day - Integer day.
# hour - Integer hour.
# minute - Integer minute.
# second - Integer second.
#
# Returns a new Time object constructed from these params.
def self.construct(year, month = 1, day = 1, hour = 0, minute = 0, second = 0)
if second >= 60
minute += second / 60
second = second % 60
end
if minute >= 60
hour += minute / 60
minute = minute % 60
end
if hour >= 24
day += hour / 24
hour = hour % 24
end
# determine if there is a day overflow. this is complicated by our crappy calendar
# system (non-constant number of days per month)
day <= 56 || raise("day must be no more than 56 (makes month resolution easier)")
if day > 28
# no month ever has fewer than 28 days, so only do this if necessary
leap_year_month_days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
common_year_month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
days_this_month = Date.leap?(year) ? leap_year_month_days[month - 1] : common_year_month_days[month - 1]
if day > days_this_month
month += day / days_this_month
day = day % days_this_month
end
end
if month > 12
if month % 12 == 0
year += (month - 12) / 12
month = 12
else
year += month / 12
month = month % 12
end
end
Chronic.time_class.local(year, month, day, hour, minute, second)
end
end