gitlab-chronic 0.10.4 → 0.10.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/.travis.yml +4 -6
- data/HISTORY.md +4 -0
- data/README.md +7 -10
- data/chronic.gemspec +1 -1
- data/lib/chronic/date.rb +7 -6
- data/lib/chronic/{tags/grabber.rb → grabber.rb} +10 -6
- data/lib/chronic/handlers.rb +2 -27
- data/lib/chronic/mini_date.rb +2 -2
- data/lib/chronic/numerizer.rb +130 -0
- data/lib/chronic/{tags/ordinal.rb → ordinal.rb} +8 -11
- data/lib/chronic/parser.rb +80 -34
- data/lib/chronic/{tags/pointer.rb → pointer.rb} +9 -5
- data/lib/chronic/{tags/repeater.rb → repeater.rb} +11 -26
- data/lib/chronic/repeaters/repeater_day.rb +1 -1
- data/lib/chronic/repeaters/repeater_day_name.rb +2 -2
- data/lib/chronic/repeaters/repeater_day_portion.rb +3 -3
- data/lib/chronic/repeaters/repeater_fortnight.rb +1 -1
- data/lib/chronic/repeaters/repeater_hour.rb +1 -1
- data/lib/chronic/repeaters/repeater_minute.rb +1 -1
- data/lib/chronic/repeaters/repeater_month.rb +1 -1
- data/lib/chronic/repeaters/repeater_month_name.rb +2 -2
- data/lib/chronic/repeaters/repeater_season.rb +1 -1
- data/lib/chronic/repeaters/repeater_second.rb +1 -1
- data/lib/chronic/repeaters/repeater_time.rb +2 -2
- data/lib/chronic/repeaters/repeater_week.rb +22 -23
- data/lib/chronic/repeaters/repeater_weekday.rb +2 -2
- data/lib/chronic/repeaters/repeater_weekend.rb +1 -1
- data/lib/chronic/repeaters/repeater_year.rb +1 -1
- data/lib/chronic/{tags/scalar.rb → scalar.rb} +10 -18
- data/lib/chronic/separator.rb +207 -0
- data/lib/chronic/{tags/sign.rb → sign.rb} +16 -2
- data/lib/chronic/tag.rb +7 -59
- data/lib/chronic/time.rb +8 -8
- data/lib/chronic/{tags/time_zone.rb → time_zone.rb} +1 -1
- data/lib/chronic/token.rb +3 -13
- data/lib/chronic/version.rb +1 -1
- data/lib/gitlab-chronic.rb +14 -18
- data/test/test_chronic.rb +6 -26
- data/test/test_handler.rb +1 -1
- data/test/test_numerizer.rb +86 -0
- data/test/test_parsing.rb +8 -306
- data/test/test_repeater_week.rb +0 -53
- data/test/test_token.rb +0 -6
- metadata +13 -19
- data/lib/chronic/definition.rb +0 -128
- data/lib/chronic/dictionary.rb +0 -36
- data/lib/chronic/repeaters/repeater_quarter.rb +0 -59
- data/lib/chronic/repeaters/repeater_quarter_name.rb +0 -40
- data/lib/chronic/tags/separator.rb +0 -123
- data/lib/chronic/tokenizer.rb +0 -38
- data/test/test_repeater_quarter.rb +0 -70
- data/test/test_repeater_quarter_name.rb +0 -198
@@ -10,11 +10,25 @@ module Chronic
|
|
10
10
|
# Returns an Array of tokens.
|
11
11
|
def self.scan(tokens, options)
|
12
12
|
tokens.each do |token|
|
13
|
-
|
14
|
-
|
13
|
+
if t = scan_for_plus(token) then token.tag(t); next end
|
14
|
+
if t = scan_for_minus(token) then token.tag(t); next end
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
+
# token - The Token object we want to scan.
|
19
|
+
#
|
20
|
+
# Returns a new SignPlus object.
|
21
|
+
def self.scan_for_plus(token)
|
22
|
+
scan_for token, SignPlus, { /^\+$/ => :plus }
|
23
|
+
end
|
24
|
+
|
25
|
+
# token - The Token object we want to scan.
|
26
|
+
#
|
27
|
+
# Returns a new SignMinus object.
|
28
|
+
def self.scan_for_minus(token)
|
29
|
+
scan_for token, SignMinus, { /^-$/ => :minus }
|
30
|
+
end
|
31
|
+
|
18
32
|
def to_s
|
19
33
|
'sign'
|
20
34
|
end
|
data/lib/chronic/tag.rb
CHANGED
@@ -4,12 +4,10 @@ module Chronic
|
|
4
4
|
class Tag
|
5
5
|
|
6
6
|
attr_accessor :type
|
7
|
-
attr_accessor :width
|
8
7
|
|
9
8
|
# type - The Symbol type of this tag.
|
10
|
-
def initialize(type,
|
9
|
+
def initialize(type, options = {})
|
11
10
|
@type = type
|
12
|
-
@width = width
|
13
11
|
@options = options
|
14
12
|
end
|
15
13
|
|
@@ -19,70 +17,20 @@ module Chronic
|
|
19
17
|
end
|
20
18
|
|
21
19
|
class << self
|
22
|
-
# Public: Scan an Array of Token objects.
|
23
|
-
#
|
24
|
-
# tokens - An Array of tokens to scan.
|
25
|
-
# options - The Hash of options specified in Chronic::parse.
|
26
|
-
#
|
27
|
-
# Returns an Array of tokens.
|
28
|
-
def scan(tokens, options)
|
29
|
-
raise NotImplementedError, 'Subclasses must override scan!'
|
30
|
-
end
|
31
|
-
|
32
20
|
private
|
33
21
|
|
34
|
-
|
35
|
-
|
36
|
-
# When it's a String it will case-insesitively match partial token,
|
37
|
-
# but only if item's last char have different type than token text's next char.
|
38
|
-
# When item is a Regexp it will match by it.
|
39
|
-
#
|
40
|
-
# item - Item to match. It can be String, Symbol or Regexp.
|
41
|
-
# klass - Tag class to create.
|
42
|
-
# symbol - Tag type as symbol or string to pass to Tag class.
|
43
|
-
# token - Token to match against.
|
44
|
-
# options - Options as hash to pass to Tag class.
|
45
|
-
#
|
46
|
-
# Returns an instance of specified Tag klass or nil if item didn't match.
|
47
|
-
def match_item(item, klass, symbol, token, options)
|
48
|
-
match = false
|
49
|
-
case item
|
50
|
-
when String
|
51
|
-
item_type = Tokenizer.char_type(item.to_s[-1])
|
52
|
-
text_type = token.text[token.position+item.length]
|
53
|
-
text_type = Tokenizer.char_type(text_type) if text_type
|
54
|
-
compatible = true
|
55
|
-
compatible = item_type != text_type if text_type && (item_type == :letter || item_type == :digit)
|
56
|
-
match = compatible && token.text[token.position, item.length].casecmp(item).zero?
|
57
|
-
when Symbol
|
58
|
-
match = token.word == item.to_s
|
22
|
+
def scan_for(token, klass, items={}, options = {})
|
23
|
+
case items
|
59
24
|
when Regexp
|
60
|
-
|
61
|
-
|
62
|
-
return klass.new(symbol, nil, options) if match
|
63
|
-
nil
|
64
|
-
end
|
65
|
-
|
66
|
-
# Internal: Scan for specified items and create respective Tag class.
|
67
|
-
#
|
68
|
-
# token - Token to match against.
|
69
|
-
# klass - Tag class to create.
|
70
|
-
# items - Item(s) to match. It can be Hash, String, Symbol or Regexp.
|
71
|
-
# Hash keys can be String, Symbol or Regexp, but values much be Symbol.
|
72
|
-
# options - Options as hash to pass to Tag class.
|
73
|
-
#
|
74
|
-
# Returns an instance of specified Tag klass or nil if item(s) didn't match.
|
75
|
-
def scan_for(token, klass, items, options = {})
|
76
|
-
if items.kind_of?(Hash)
|
25
|
+
return klass.new(token.word, options) if items =~ token.word
|
26
|
+
when Hash
|
77
27
|
items.each do |item, symbol|
|
78
|
-
|
79
|
-
return scanned if scanned
|
28
|
+
return klass.new(symbol, options) if item =~ token.word
|
80
29
|
end
|
81
|
-
else
|
82
|
-
return match_item(items, klass, token.word, token, options)
|
83
30
|
end
|
84
31
|
nil
|
85
32
|
end
|
33
|
+
|
86
34
|
end
|
87
35
|
|
88
36
|
end
|
data/lib/chronic/time.rb
CHANGED
@@ -6,23 +6,23 @@ module Chronic
|
|
6
6
|
SUBSECOND_SECONDS = 0.001
|
7
7
|
|
8
8
|
# Checks if given number could be hour
|
9
|
-
def self.could_be_hour?(hour
|
10
|
-
hour >= 0 && hour <=
|
9
|
+
def self.could_be_hour?(hour)
|
10
|
+
hour >= 0 && hour <= 24
|
11
11
|
end
|
12
12
|
|
13
13
|
# Checks if given number could be minute
|
14
|
-
def self.could_be_minute?(minute
|
15
|
-
minute >= 0 && minute <= 60
|
14
|
+
def self.could_be_minute?(minute)
|
15
|
+
minute >= 0 && minute <= 60
|
16
16
|
end
|
17
17
|
|
18
18
|
# Checks if given number could be second
|
19
|
-
def self.could_be_second?(second
|
20
|
-
second >= 0 && second <= 60
|
19
|
+
def self.could_be_second?(second)
|
20
|
+
second >= 0 && second <= 60
|
21
21
|
end
|
22
22
|
|
23
23
|
# Checks if given number could be subsecond
|
24
|
-
def self.could_be_subsecond?(subsecond
|
25
|
-
subsecond >= 0 && subsecond <= 999999
|
24
|
+
def self.could_be_subsecond?(subsecond)
|
25
|
+
subsecond >= 0 && subsecond <= 999999
|
26
26
|
end
|
27
27
|
|
28
28
|
# normalize offset in seconds to offset as string +mm:ss or -mm:ss
|
data/lib/chronic/token.rb
CHANGED
@@ -4,18 +4,9 @@ module Chronic
|
|
4
4
|
attr_accessor :word
|
5
5
|
attr_accessor :tags
|
6
6
|
|
7
|
-
|
8
|
-
attr_reader :position
|
9
|
-
|
10
|
-
def initialize(word, text = nil, position = 0)
|
7
|
+
def initialize(word)
|
11
8
|
@word = word
|
12
9
|
@tags = []
|
13
|
-
@text = text
|
14
|
-
@position = position
|
15
|
-
end
|
16
|
-
|
17
|
-
def ==(token)
|
18
|
-
token.word == @word.downcase
|
19
10
|
end
|
20
11
|
|
21
12
|
# Tag this token with the specified tag.
|
@@ -24,7 +15,7 @@ module Chronic
|
|
24
15
|
#
|
25
16
|
# Returns nothing.
|
26
17
|
def tag(new_tag)
|
27
|
-
@tags << new_tag
|
18
|
+
@tags << new_tag
|
28
19
|
end
|
29
20
|
|
30
21
|
# Remove all tags of the given class.
|
@@ -50,12 +41,11 @@ module Chronic
|
|
50
41
|
|
51
42
|
# Print this Token in a pretty way
|
52
43
|
def to_s
|
53
|
-
@word
|
44
|
+
@word << '(' << @tags.join(', ') << ') '
|
54
45
|
end
|
55
46
|
|
56
47
|
def inspect
|
57
48
|
to_s
|
58
49
|
end
|
59
50
|
end
|
60
|
-
|
61
51
|
end
|
data/lib/chronic/version.rb
CHANGED
data/lib/gitlab-chronic.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'time'
|
2
2
|
require 'date'
|
3
|
-
require 'numerizer'
|
4
3
|
|
5
4
|
require 'chronic/version'
|
6
5
|
|
@@ -11,24 +10,21 @@ require 'chronic/time'
|
|
11
10
|
require 'chronic/handler'
|
12
11
|
require 'chronic/handlers'
|
13
12
|
require 'chronic/mini_date'
|
13
|
+
require 'chronic/tag'
|
14
14
|
require 'chronic/span'
|
15
15
|
require 'chronic/token'
|
16
|
-
require 'chronic/
|
16
|
+
require 'chronic/grabber'
|
17
|
+
require 'chronic/pointer'
|
18
|
+
require 'chronic/scalar'
|
19
|
+
require 'chronic/ordinal'
|
20
|
+
require 'chronic/separator'
|
21
|
+
require 'chronic/sign'
|
22
|
+
require 'chronic/time_zone'
|
23
|
+
require 'chronic/numerizer'
|
17
24
|
require 'chronic/season'
|
18
25
|
|
19
|
-
require 'chronic/
|
20
|
-
require 'chronic/tags/grabber'
|
21
|
-
require 'chronic/tags/ordinal'
|
22
|
-
require 'chronic/tags/pointer'
|
23
|
-
require 'chronic/tags/scalar'
|
24
|
-
require 'chronic/tags/separator'
|
25
|
-
require 'chronic/tags/sign'
|
26
|
-
require 'chronic/tags/time_zone'
|
27
|
-
|
28
|
-
require 'chronic/tags/repeater'
|
26
|
+
require 'chronic/repeater'
|
29
27
|
require 'chronic/repeaters/repeater_year'
|
30
|
-
require 'chronic/repeaters/repeater_quarter'
|
31
|
-
require 'chronic/repeaters/repeater_quarter_name'
|
32
28
|
require 'chronic/repeaters/repeater_season'
|
33
29
|
require 'chronic/repeaters/repeater_season_name'
|
34
30
|
require 'chronic/repeaters/repeater_month'
|
@@ -124,7 +120,7 @@ module Chronic
|
|
124
120
|
|
125
121
|
# determine if there is a day overflow. this is complicated by our crappy calendar
|
126
122
|
# system (non-constant number of days per month)
|
127
|
-
day <= 56 || raise(
|
123
|
+
day <= 56 || raise("day must be no more than 56 (makes month resolution easier)")
|
128
124
|
if day > 28 # no month ever has fewer than 28 days, so only do this if necessary
|
129
125
|
days_this_month = ::Date.leap?(year) ? Date::MONTH_DAYS_LEAP[month] : Date::MONTH_DAYS[month]
|
130
126
|
if day > days_this_month
|
@@ -142,12 +138,12 @@ module Chronic
|
|
142
138
|
month = month % 12
|
143
139
|
end
|
144
140
|
end
|
145
|
-
if Chronic.time_class.name ==
|
141
|
+
if Chronic.time_class.name == "Date"
|
146
142
|
Chronic.time_class.new(year, month, day)
|
147
|
-
elsif not Chronic.time_class.respond_to?(:new) or (RUBY_VERSION.to_f < 1.9 and Chronic.time_class.name ==
|
143
|
+
elsif not Chronic.time_class.respond_to?(:new) or (RUBY_VERSION.to_f < 1.9 and Chronic.time_class.name == "Time")
|
148
144
|
Chronic.time_class.local(year, month, day, hour, minute, second)
|
149
145
|
else
|
150
|
-
offset = Time::normalize_offset(offset) if Chronic.time_class.name ==
|
146
|
+
offset = Time::normalize_offset(offset) if Chronic.time_class.name == "DateTime"
|
151
147
|
Chronic.time_class.new(year, month, day, hour, minute, second, offset)
|
152
148
|
end
|
153
149
|
end
|
data/test/test_chronic.rb
CHANGED
@@ -13,7 +13,7 @@ class TestChronic < TestCase
|
|
13
13
|
|
14
14
|
def test_pre_normalize_numerized_string
|
15
15
|
string = 'two and a half years'
|
16
|
-
assert_equal Numerizer.numerize(string), Chronic::Parser.new.pre_normalize(string)
|
16
|
+
assert_equal Chronic::Numerizer.numerize(string), Chronic::Parser.new.pre_normalize(string)
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_post_normalize_am_pm_aliases
|
@@ -61,20 +61,19 @@ class TestChronic < TestCase
|
|
61
61
|
Chronic::Handler.new([:scalar_month, [:separator_slash, :separator_dash], :scalar_day, [:separator_slash, :separator_dash], :scalar_year, :separator_at?, 'time?'], :handle_sm_sd_sy),
|
62
62
|
Chronic::Handler.new([:scalar_month, [:separator_slash, :separator_dash], :scalar_day, :separator_at?, 'time?'], :handle_sm_sd),
|
63
63
|
Chronic::Handler.new([:scalar_day, [:separator_slash, :separator_dash], :scalar_month, :separator_at?, 'time?'], :handle_sd_sm),
|
64
|
-
Chronic::Handler.new([:scalar_day, [:separator_slash, :separator_dash], :scalar_month, [:separator_slash, :separator_dash], :scalar_year, :separator_at?, 'time?'], :handle_sd_sm_sy)
|
65
|
-
Chronic::Handler.new([:scalar_day, :repeater_month_name, :scalar_year, :separator_at?, 'time?'], :handle_sd_rmn_sy)
|
64
|
+
Chronic::Handler.new([:scalar_day, [:separator_slash, :separator_dash], :scalar_month, [:separator_slash, :separator_dash], :scalar_year, :separator_at?, 'time?'], :handle_sd_sm_sy)
|
66
65
|
]
|
67
66
|
|
68
|
-
assert_equal endians, Chronic::
|
67
|
+
assert_equal endians, Chronic::Parser.new.definitions[:endian]
|
69
68
|
|
70
|
-
defs = Chronic::
|
69
|
+
defs = Chronic::Parser.new.definitions(:endian_precedence => :little)
|
71
70
|
assert_equal endians.reverse, defs[:endian]
|
72
71
|
|
73
|
-
defs = Chronic::
|
72
|
+
defs = Chronic::Parser.new.definitions(:endian_precedence => [:little, :middle])
|
74
73
|
assert_equal endians.reverse, defs[:endian]
|
75
74
|
|
76
75
|
assert_raises(ArgumentError) do
|
77
|
-
Chronic::
|
76
|
+
Chronic::Parser.new.definitions(:endian_precedence => :invalid)
|
78
77
|
end
|
79
78
|
end
|
80
79
|
|
@@ -162,25 +161,6 @@ class TestChronic < TestCase
|
|
162
161
|
end
|
163
162
|
end
|
164
163
|
|
165
|
-
def test_valid_options
|
166
|
-
options = {
|
167
|
-
:context => :future,
|
168
|
-
:now => nil,
|
169
|
-
:hours24 => nil,
|
170
|
-
:week_start => :sunday,
|
171
|
-
:guess => true,
|
172
|
-
:ambiguous_time_range => 6,
|
173
|
-
:endian_precedence => [:middle, :little],
|
174
|
-
:ambiguous_year_future_bias => 50
|
175
|
-
}
|
176
|
-
refute_nil Chronic.parse('now', options)
|
177
|
-
end
|
178
|
-
|
179
|
-
def test_invalid_options
|
180
|
-
assert_raises(ArgumentError) { Chronic.parse('now', foo: 'boo') }
|
181
|
-
assert_raises(ArgumentError) { Chronic.parse('now', time_class: Time) }
|
182
|
-
end
|
183
|
-
|
184
164
|
def test_activesupport
|
185
165
|
=begin
|
186
166
|
# ActiveSupport needs MiniTest '~> 4.2' which conflicts with '~> 5.0'
|
data/test/test_handler.rb
CHANGED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class ParseNumbersTest < TestCase
|
4
|
+
|
5
|
+
def test_straight_parsing
|
6
|
+
strings = {
|
7
|
+
'one' => 1,
|
8
|
+
'five' => 5,
|
9
|
+
'ten' => 10,
|
10
|
+
'eleven' => 11,
|
11
|
+
'twelve' => 12,
|
12
|
+
'thirteen' => 13,
|
13
|
+
'fourteen' => 14,
|
14
|
+
'fifteen' => 15,
|
15
|
+
'sixteen' => 16,
|
16
|
+
'seventeen' => 17,
|
17
|
+
'eighteen' => 18,
|
18
|
+
'nineteen' => 19,
|
19
|
+
'twenty' => 20,
|
20
|
+
'twenty seven' => 27,
|
21
|
+
'thirty-one' => 31,
|
22
|
+
'thirty-seven' => 37,
|
23
|
+
'thirty seven' => 37,
|
24
|
+
'fifty nine' => 59,
|
25
|
+
'forty two' => 42,
|
26
|
+
'fourty two' => 42,
|
27
|
+
# 'a hundred' => 100,
|
28
|
+
'one hundred' => 100,
|
29
|
+
'one hundred and fifty' => 150,
|
30
|
+
# 'one fifty' => 150,
|
31
|
+
'two-hundred' => 200,
|
32
|
+
'5 hundred' => 500,
|
33
|
+
'nine hundred and ninety nine' => 999,
|
34
|
+
'one thousand' => 1000,
|
35
|
+
'twelve hundred' => 1200,
|
36
|
+
'one thousand two hundred' => 1_200,
|
37
|
+
'seventeen thousand' => 17_000,
|
38
|
+
'twentyone-thousand-four-hundred-and-seventy-three' => 21_473,
|
39
|
+
'seventy four thousand and two' => 74_002,
|
40
|
+
'ninety nine thousand nine hundred ninety nine' => 99_999,
|
41
|
+
'100 thousand' => 100_000,
|
42
|
+
'two hundred fifty thousand' => 250_000,
|
43
|
+
'one million' => 1_000_000,
|
44
|
+
'one million two hundred fifty thousand and seven' => 1_250_007,
|
45
|
+
'one billion' => 1_000_000_000,
|
46
|
+
'one billion and one' => 1_000_000_001}
|
47
|
+
|
48
|
+
strings.each do |key, val|
|
49
|
+
assert_equal val, Chronic::Numerizer.numerize(key).to_i
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_ordinal_strings
|
54
|
+
{
|
55
|
+
'first' => '1st',
|
56
|
+
'second' => 'second',
|
57
|
+
'second day' => '2nd day',
|
58
|
+
'second of may' => '2nd of may',
|
59
|
+
'fifth' => '5th',
|
60
|
+
'twelfth' => '12th',
|
61
|
+
'twentieth' => '20th',
|
62
|
+
'thirtieth' => '30th',
|
63
|
+
'fourtieth' => '40th',
|
64
|
+
'fiftieth' => '50th',
|
65
|
+
'sixtieth' => '60th',
|
66
|
+
'seventieth' => '70th',
|
67
|
+
'eightieth' => '80th',
|
68
|
+
'ninetieth' => '90th',
|
69
|
+
'hundredth' => '100th',
|
70
|
+
'thousandth' => '1000th',
|
71
|
+
'millionth' => '1000000th',
|
72
|
+
'billionth' => '1000000000th',
|
73
|
+
'trillionth' => '1000000000000th',
|
74
|
+
'twenty third' => '23rd',
|
75
|
+
'first day month two' => '1st day month 2'
|
76
|
+
}.each do |key, val|
|
77
|
+
# Use pre_normalize here instead of Numerizer directly because
|
78
|
+
# pre_normalize deals with parsing 'second' appropriately
|
79
|
+
assert_equal val, Chronic::Parser.new.pre_normalize(key)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_edges
|
84
|
+
assert_equal "27 Oct 2006 7:30am", Chronic::Numerizer.numerize("27 Oct 2006 7:30am")
|
85
|
+
end
|
86
|
+
end
|
data/test/test_parsing.rb
CHANGED
@@ -22,14 +22,6 @@ class TestParsing < TestCase
|
|
22
22
|
time2 = Time.parse("2013-08-01 019:30:00.345-07:00")
|
23
23
|
assert_in_delta time, time2, 0.001
|
24
24
|
|
25
|
-
time = Chronic.parse("2013-08-01T19:30:00.34-07:00")
|
26
|
-
time2 = Time.parse("2013-08-01T19:30:00.34-07:00")
|
27
|
-
assert_in_delta time, time2, 0.001
|
28
|
-
|
29
|
-
time = Chronic.parse("2013-08-01T19:30:00.3456789-07:00")
|
30
|
-
time2 = Time.parse("2013-08-01T19:30:00.3456789-07:00")
|
31
|
-
assert_in_delta time, time2, 0.001
|
32
|
-
|
33
25
|
time = Chronic.parse("2012-08-02T12:00:00Z")
|
34
26
|
assert_equal Time.utc(2012, 8, 2, 12), time
|
35
27
|
|
@@ -53,13 +45,13 @@ class TestParsing < TestCase
|
|
53
45
|
|
54
46
|
def test_handle_rmn_sd
|
55
47
|
time = parse_now("aug 3")
|
56
|
-
assert_equal Time.local(
|
48
|
+
assert_equal Time.local(2006, 8, 3, 12), time
|
57
49
|
|
58
50
|
time = parse_now("aug 3", :context => :past)
|
59
51
|
assert_equal Time.local(2006, 8, 3, 12), time
|
60
52
|
|
61
53
|
time = parse_now("aug. 3")
|
62
|
-
assert_equal Time.local(
|
54
|
+
assert_equal Time.local(2006, 8, 3, 12), time
|
63
55
|
|
64
56
|
time = parse_now("aug 20")
|
65
57
|
assert_equal Time.local(2006, 8, 20, 12), time
|
@@ -119,7 +111,7 @@ class TestParsing < TestCase
|
|
119
111
|
|
120
112
|
def test_handle_od_rm
|
121
113
|
time = parse_now("fifteenth of this month")
|
122
|
-
assert_equal Time.local(
|
114
|
+
assert_equal Time.local(2006, 8, 15, 12), time
|
123
115
|
end
|
124
116
|
|
125
117
|
def test_handle_od_rmn
|
@@ -178,9 +170,6 @@ class TestParsing < TestCase
|
|
178
170
|
|
179
171
|
time = parse_now("may 32")
|
180
172
|
assert_equal Time.local(2032, 5, 16, 12, 0, 0), time
|
181
|
-
|
182
|
-
time = parse_now("may '01")
|
183
|
-
assert_equal Time.local(2001, 5, 16, 12, 0, 0), time
|
184
173
|
end
|
185
174
|
|
186
175
|
def test_handle_rdn_rmn_sd_t_tz_sy
|
@@ -298,15 +287,6 @@ class TestParsing < TestCase
|
|
298
287
|
|
299
288
|
time = parse_now("27 Oct 2006 7:30pm")
|
300
289
|
assert_equal Time.local(2006, 10, 27, 19, 30), time
|
301
|
-
|
302
|
-
time = parse_now("3 jan 10")
|
303
|
-
assert_equal Time.local(2010, 1, 3, 12), time
|
304
|
-
|
305
|
-
time = parse_now("3 jan 10", :endian_precedence => :little)
|
306
|
-
assert_equal Time.local(2010, 1, 3, 12), time
|
307
|
-
|
308
|
-
time = parse_now("3 jan 10", :endian_precedence => :middle)
|
309
|
-
assert_equal Time.local(2010, 1, 3, 12), time
|
310
290
|
end
|
311
291
|
|
312
292
|
def test_handle_sm_sd_sy
|
@@ -349,9 +329,6 @@ class TestParsing < TestCase
|
|
349
329
|
time = parse_now("09.08.2013")
|
350
330
|
assert_equal Time.local(2013, 8, 9, 12), time
|
351
331
|
|
352
|
-
time = parse_now("9.8.2013")
|
353
|
-
assert_equal Time.local(2013, 8, 9, 12), time
|
354
|
-
|
355
332
|
time = parse_now("30-07-2013 21:53:49")
|
356
333
|
assert_equal Time.local(2013, 7, 30, 21, 53, 49), time
|
357
334
|
end
|
@@ -420,33 +397,6 @@ class TestParsing < TestCase
|
|
420
397
|
|
421
398
|
time = parse_now("3/13", :context => :none)
|
422
399
|
assert_equal Time.local(2006, 3, 13, 12), time
|
423
|
-
|
424
|
-
time = parse_now("12/1", :context => :past)
|
425
|
-
assert_equal Time.local(2005, 12, 1, 12), time
|
426
|
-
|
427
|
-
time = parse_now("12/1", :context => :future)
|
428
|
-
assert_equal Time.local(2006, 12, 1, 12), time
|
429
|
-
|
430
|
-
time = parse_now("12/1", :context => :none)
|
431
|
-
assert_equal Time.local(2006, 12, 1, 12), time
|
432
|
-
|
433
|
-
time = parse_now("8/1", :context => :past)
|
434
|
-
assert_equal Time.local(2006, 8, 1, 12), time
|
435
|
-
|
436
|
-
time = parse_now("8/1", :context => :future)
|
437
|
-
assert_equal Time.local(2007, 8, 1, 12), time
|
438
|
-
|
439
|
-
time = parse_now("8/1", :context => :none)
|
440
|
-
assert_equal Time.local(2006, 8, 1, 12), time
|
441
|
-
|
442
|
-
time = parse_now("1/1", :context => :past)
|
443
|
-
assert_equal Time.local(2006, 1, 1, 12), time
|
444
|
-
|
445
|
-
time = parse_now("1/1", :context => :future)
|
446
|
-
assert_equal Time.local(2007, 1, 1, 12), time
|
447
|
-
|
448
|
-
time = parse_now("1/1", :context => :none)
|
449
|
-
assert_equal Time.local(2006, 1, 1, 12), time
|
450
400
|
end
|
451
401
|
|
452
402
|
# def test_handle_sm_sy
|
@@ -457,7 +407,7 @@ class TestParsing < TestCase
|
|
457
407
|
# assert_equal Time.local(2006, 12, 16, 12), time
|
458
408
|
#
|
459
409
|
# time = parse_now("13/06")
|
460
|
-
#
|
410
|
+
# assert_equal nil, time
|
461
411
|
# end
|
462
412
|
|
463
413
|
def test_handle_sy_sm
|
@@ -520,7 +470,7 @@ class TestParsing < TestCase
|
|
520
470
|
assert_equal Time.local(2007, 01, 30, 12), time
|
521
471
|
|
522
472
|
time = parse_now("5th tuesday in february")
|
523
|
-
|
473
|
+
assert_equal nil, time
|
524
474
|
|
525
475
|
%W(jan feb march april may june july aug sep oct nov dec).each_with_index do |month, index|
|
526
476
|
time = parse_now("5th tuesday in #{month}")
|
@@ -536,7 +486,7 @@ class TestParsing < TestCase
|
|
536
486
|
assert_equal Time.local(2006, 11, 15, 12), time
|
537
487
|
|
538
488
|
time = parse_now("10th wednesday in november")
|
539
|
-
|
489
|
+
assert_equal nil, time
|
540
490
|
|
541
491
|
# time = parse_now("3rd wednesday in 2007")
|
542
492
|
# assert_equal Time.local(2007, 1, 20, 12), time
|
@@ -701,12 +651,6 @@ class TestParsing < TestCase
|
|
701
651
|
time = parse_now("this week", :context => :past)
|
702
652
|
assert_equal Time.local(2006, 8, 14, 19), time
|
703
653
|
|
704
|
-
time = parse_now("this week", :context => :past, :guess => :begin)
|
705
|
-
assert_equal Time.local(2006, 8, 13), time
|
706
|
-
|
707
|
-
time = parse_now("this week", :context => :past, :guess => :begin, :week_start => :monday)
|
708
|
-
assert_equal Time.local(2006, 8, 14), time
|
709
|
-
|
710
654
|
# weekend
|
711
655
|
|
712
656
|
time = parse_now("this weekend")
|
@@ -1042,11 +986,6 @@ class TestParsing < TestCase
|
|
1042
986
|
# future
|
1043
987
|
end
|
1044
988
|
|
1045
|
-
def test_parse_guess_rmn_s_r_p
|
1046
|
-
time = parse_now("september 3 years ago", :guess => :start)
|
1047
|
-
assert_equal Time.local(2003, 9), time
|
1048
|
-
end
|
1049
|
-
|
1050
989
|
def test_parse_guess_o_r_g_r
|
1051
990
|
time = parse_now("3rd month next year", :guess => false)
|
1052
991
|
assert_equal Time.local(2007, 3), time.begin
|
@@ -1067,13 +1006,10 @@ class TestParsing < TestCase
|
|
1067
1006
|
|
1068
1007
|
def test_parse_guess_nonsense
|
1069
1008
|
time = parse_now("some stupid nonsense")
|
1070
|
-
|
1009
|
+
assert_equal nil, time
|
1071
1010
|
|
1072
1011
|
time = parse_now("Ham Sandwich")
|
1073
|
-
|
1074
|
-
|
1075
|
-
time = parse_now("t")
|
1076
|
-
assert_nil time
|
1012
|
+
assert_equal nil, time
|
1077
1013
|
end
|
1078
1014
|
|
1079
1015
|
def test_parse_span
|
@@ -1170,236 +1106,6 @@ class TestParsing < TestCase
|
|
1170
1106
|
assert_equal Time.local(2007, 6, 20), t.end
|
1171
1107
|
end
|
1172
1108
|
|
1173
|
-
def test_quarters
|
1174
|
-
time = parse_now("this quarter", :guess => false)
|
1175
|
-
assert_equal Time.local(2006, 7, 1), time.begin
|
1176
|
-
assert_equal Time.local(2006, 10, 1), time.end
|
1177
|
-
|
1178
|
-
time = parse_now("next quarter", :guess => false)
|
1179
|
-
assert_equal Time.local(2006, 10, 1), time.begin
|
1180
|
-
assert_equal Time.local(2007, 1, 1), time.end
|
1181
|
-
|
1182
|
-
time = parse_now("last quarter", :guess => false)
|
1183
|
-
assert_equal Time.local(2006, 4, 1), time.begin
|
1184
|
-
assert_equal Time.local(2006, 7, 1), time.end
|
1185
|
-
end
|
1186
|
-
|
1187
|
-
def test_quarters_srp
|
1188
|
-
time = parse_now("1 quarter ago", :guess => false)
|
1189
|
-
assert_equal Time.local(2006, 4, 1), time.begin
|
1190
|
-
assert_equal Time.local(2006, 7, 1), time.end
|
1191
|
-
|
1192
|
-
time = parse_now("2 quarters ago", :guess => false)
|
1193
|
-
assert_equal Time.local(2006, 1, 1), time.begin
|
1194
|
-
assert_equal Time.local(2006, 4, 1), time.end
|
1195
|
-
|
1196
|
-
time = parse_now("1 quarter from now", :guess => false)
|
1197
|
-
assert_equal Time.local(2006, 10, 1), time.begin
|
1198
|
-
assert_equal Time.local(2007, 1, 1), time.end
|
1199
|
-
end
|
1200
|
-
|
1201
|
-
def test_quarters_named
|
1202
|
-
["Q1", "first quarter", "1st quarter"].each do |string|
|
1203
|
-
time = parse_now(string, :guess => false, :context => :none)
|
1204
|
-
assert_equal Time.local(2006, 1, 1), time.begin
|
1205
|
-
assert_equal Time.local(2006, 4, 1), time.end
|
1206
|
-
|
1207
|
-
time = parse_now(string, :guess => false, :context => :future)
|
1208
|
-
assert_equal Time.local(2007, 1, 1), time.begin
|
1209
|
-
assert_equal Time.local(2007, 4, 1), time.end
|
1210
|
-
|
1211
|
-
time = parse_now(string, :guess => false, :context => :past)
|
1212
|
-
assert_equal Time.local(2006, 1, 1), time.begin
|
1213
|
-
assert_equal Time.local(2006, 4, 1), time.end
|
1214
|
-
|
1215
|
-
time = parse_now("#{string} 2005", :guess => false)
|
1216
|
-
assert_equal Time.local(2005, 1, 1), time.begin
|
1217
|
-
assert_equal Time.local(2005, 4, 1), time.end
|
1218
|
-
|
1219
|
-
time = parse_now("2005 #{string}", :guess => false)
|
1220
|
-
assert_equal Time.local(2005, 1, 1), time.begin
|
1221
|
-
assert_equal Time.local(2005, 4, 1), time.end
|
1222
|
-
|
1223
|
-
time = parse_now("#{string} this year", :guess => false)
|
1224
|
-
assert_equal Time.local(2006, 1, 1), time.begin
|
1225
|
-
assert_equal Time.local(2006, 4, 1), time.end
|
1226
|
-
|
1227
|
-
time = parse_now("this year #{string}", :guess => false)
|
1228
|
-
assert_equal Time.local(2006, 1, 1), time.begin
|
1229
|
-
assert_equal Time.local(2006, 4, 1), time.end
|
1230
|
-
|
1231
|
-
time = parse_now("#{string} next year", :guess => false)
|
1232
|
-
assert_equal Time.local(2007, 1, 1), time.begin
|
1233
|
-
assert_equal Time.local(2007, 4, 1), time.end
|
1234
|
-
|
1235
|
-
time = parse_now("next year #{string}", :guess => false)
|
1236
|
-
assert_equal Time.local(2007, 1, 1), time.begin
|
1237
|
-
assert_equal Time.local(2007, 4, 1), time.end
|
1238
|
-
|
1239
|
-
time = parse_now("this #{string}", :guess => false, context: :none)
|
1240
|
-
assert_equal Time.local(2006, 1, 1), time.begin
|
1241
|
-
assert_equal Time.local(2006, 4, 1), time.end
|
1242
|
-
|
1243
|
-
time = parse_now("last #{string}", :guess => false, context: :none)
|
1244
|
-
assert_equal Time.local(2006, 1, 1), time.begin
|
1245
|
-
assert_equal Time.local(2006, 4, 1), time.end
|
1246
|
-
|
1247
|
-
time = parse_now("next #{string}", :guess => false, context: :none)
|
1248
|
-
assert_equal Time.local(2007, 1, 1), time.begin
|
1249
|
-
assert_equal Time.local(2007, 4, 1), time.end
|
1250
|
-
end
|
1251
|
-
|
1252
|
-
["Q2", "second quarter", "2nd quarter"].each do |string|
|
1253
|
-
time = parse_now(string, :guess => false, :context => :none)
|
1254
|
-
assert_equal Time.local(2006, 4, 1), time.begin
|
1255
|
-
assert_equal Time.local(2006, 7, 1), time.end
|
1256
|
-
|
1257
|
-
time = parse_now(string, :guess => false, :context => :future)
|
1258
|
-
assert_equal Time.local(2007, 4, 1), time.begin
|
1259
|
-
assert_equal Time.local(2007, 7, 1), time.end
|
1260
|
-
|
1261
|
-
time = parse_now(string, :guess => false, :context => :past)
|
1262
|
-
assert_equal Time.local(2006, 4, 1), time.begin
|
1263
|
-
assert_equal Time.local(2006, 7, 1), time.end
|
1264
|
-
|
1265
|
-
time = parse_now("#{string} 2005", :guess => false)
|
1266
|
-
assert_equal Time.local(2005, 4, 1), time.begin
|
1267
|
-
assert_equal Time.local(2005, 7, 1), time.end
|
1268
|
-
|
1269
|
-
time = parse_now("2005 #{string}", :guess => false)
|
1270
|
-
assert_equal Time.local(2005, 4, 1), time.begin
|
1271
|
-
assert_equal Time.local(2005, 7, 1), time.end
|
1272
|
-
|
1273
|
-
time = parse_now("#{string} this year", :guess => false)
|
1274
|
-
assert_equal Time.local(2006, 4, 1), time.begin
|
1275
|
-
assert_equal Time.local(2006, 7, 1), time.end
|
1276
|
-
|
1277
|
-
time = parse_now("this year #{string}", :guess => false)
|
1278
|
-
assert_equal Time.local(2006, 4, 1), time.begin
|
1279
|
-
assert_equal Time.local(2006, 7, 1), time.end
|
1280
|
-
|
1281
|
-
time = parse_now("#{string} next year", :guess => false)
|
1282
|
-
assert_equal Time.local(2007, 4, 1), time.begin
|
1283
|
-
assert_equal Time.local(2007, 7, 1), time.end
|
1284
|
-
|
1285
|
-
time = parse_now("next year #{string}", :guess => false)
|
1286
|
-
assert_equal Time.local(2007, 4, 1), time.begin
|
1287
|
-
assert_equal Time.local(2007, 7, 1), time.end
|
1288
|
-
|
1289
|
-
time = parse_now("this #{string}", :guess => false, context: :none)
|
1290
|
-
assert_equal Time.local(2006, 4, 1), time.begin
|
1291
|
-
assert_equal Time.local(2006, 7, 1), time.end
|
1292
|
-
|
1293
|
-
time = parse_now("last #{string}", :guess => false, context: :none)
|
1294
|
-
assert_equal Time.local(2006, 4, 1), time.begin
|
1295
|
-
assert_equal Time.local(2006, 7, 1), time.end
|
1296
|
-
|
1297
|
-
time = parse_now("next #{string}", :guess => false, context: :none)
|
1298
|
-
assert_equal Time.local(2007, 4, 1), time.begin
|
1299
|
-
assert_equal Time.local(2007, 7, 1), time.end
|
1300
|
-
end
|
1301
|
-
|
1302
|
-
["Q3", "third quarter", "3rd quarter"].each do |string|
|
1303
|
-
time = parse_now(string, :guess => false, :context => :none)
|
1304
|
-
assert_equal Time.local(2006, 7, 1), time.begin
|
1305
|
-
assert_equal Time.local(2006, 10, 1), time.end
|
1306
|
-
|
1307
|
-
time = parse_now(string, :guess => false, :context => :future)
|
1308
|
-
assert_equal Time.local(2007, 7, 1), time.begin
|
1309
|
-
assert_equal Time.local(2007, 10, 1), time.end
|
1310
|
-
|
1311
|
-
time = parse_now(string, :guess => false, :context => :past)
|
1312
|
-
assert_equal Time.local(2005, 7, 1), time.begin
|
1313
|
-
assert_equal Time.local(2005, 10, 1), time.end
|
1314
|
-
|
1315
|
-
time = parse_now("#{string} 2005", :guess => false)
|
1316
|
-
assert_equal Time.local(2005, 7, 1), time.begin
|
1317
|
-
assert_equal Time.local(2005, 10, 1), time.end
|
1318
|
-
|
1319
|
-
time = parse_now("2005 #{string}", :guess => false)
|
1320
|
-
assert_equal Time.local(2005, 7, 1), time.begin
|
1321
|
-
assert_equal Time.local(2005, 10, 1), time.end
|
1322
|
-
|
1323
|
-
time = parse_now("#{string} this year", :guess => false)
|
1324
|
-
assert_equal Time.local(2006, 7, 1), time.begin
|
1325
|
-
assert_equal Time.local(2006, 10, 1), time.end
|
1326
|
-
|
1327
|
-
time = parse_now("this year #{string}", :guess => false)
|
1328
|
-
assert_equal Time.local(2006, 7, 1), time.begin
|
1329
|
-
assert_equal Time.local(2006, 10, 1), time.end
|
1330
|
-
|
1331
|
-
time = parse_now("#{string} next year", :guess => false)
|
1332
|
-
assert_equal Time.local(2007, 7, 1), time.begin
|
1333
|
-
assert_equal Time.local(2007, 10, 1), time.end
|
1334
|
-
|
1335
|
-
time = parse_now("next year #{string}", :guess => false)
|
1336
|
-
assert_equal Time.local(2007, 7, 1), time.begin
|
1337
|
-
assert_equal Time.local(2007, 10, 1), time.end
|
1338
|
-
|
1339
|
-
time = parse_now("this #{string}", :guess => false, context: :none)
|
1340
|
-
assert_equal Time.local(2006, 7, 1), time.begin
|
1341
|
-
assert_equal Time.local(2006, 10, 1), time.end
|
1342
|
-
|
1343
|
-
time = parse_now("last #{string}", :guess => false, context: :none)
|
1344
|
-
assert_equal Time.local(2005, 7, 1), time.begin
|
1345
|
-
assert_equal Time.local(2005, 10, 1), time.end
|
1346
|
-
|
1347
|
-
time = parse_now("next #{string}", :guess => false, context: :none)
|
1348
|
-
assert_equal Time.local(2007, 7, 1), time.begin
|
1349
|
-
assert_equal Time.local(2007, 10, 1), time.end
|
1350
|
-
end
|
1351
|
-
|
1352
|
-
["Q4", "fourth quarter", "4th quarter"].each do |string|
|
1353
|
-
time = parse_now(string, :guess => false, :context => :none)
|
1354
|
-
assert_equal Time.local(2006, 10, 1), time.begin
|
1355
|
-
assert_equal Time.local(2007, 1, 1), time.end
|
1356
|
-
|
1357
|
-
time = parse_now(string, :guess => false, :context => :future)
|
1358
|
-
assert_equal Time.local(2006, 10, 1), time.begin
|
1359
|
-
assert_equal Time.local(2007, 1, 1), time.end
|
1360
|
-
|
1361
|
-
time = parse_now(string, :guess => false, :context => :past)
|
1362
|
-
assert_equal Time.local(2005, 10, 1), time.begin
|
1363
|
-
assert_equal Time.local(2006, 1, 1), time.end
|
1364
|
-
|
1365
|
-
time = parse_now("#{string} 2005", :guess => false)
|
1366
|
-
assert_equal Time.local(2005, 10, 1), time.begin
|
1367
|
-
assert_equal Time.local(2006, 1, 1), time.end
|
1368
|
-
|
1369
|
-
time = parse_now("2005 #{string}", :guess => false)
|
1370
|
-
assert_equal Time.local(2005, 10, 1), time.begin
|
1371
|
-
assert_equal Time.local(2006, 1, 1), time.end
|
1372
|
-
|
1373
|
-
time = parse_now("#{string} this year", :guess => false)
|
1374
|
-
assert_equal Time.local(2006, 10, 1), time.begin
|
1375
|
-
assert_equal Time.local(2007, 1, 1), time.end
|
1376
|
-
|
1377
|
-
time = parse_now("this year #{string}", :guess => false)
|
1378
|
-
assert_equal Time.local(2006, 10, 1), time.begin
|
1379
|
-
assert_equal Time.local(2007, 1, 1), time.end
|
1380
|
-
|
1381
|
-
time = parse_now("#{string} next year", :guess => false)
|
1382
|
-
assert_equal Time.local(2007, 10, 1), time.begin
|
1383
|
-
assert_equal Time.local(2008, 1, 1), time.end
|
1384
|
-
|
1385
|
-
time = parse_now("next year #{string}", :guess => false)
|
1386
|
-
assert_equal Time.local(2007, 10, 1), time.begin
|
1387
|
-
assert_equal Time.local(2008, 1, 1), time.end
|
1388
|
-
|
1389
|
-
time = parse_now("this #{string}", :guess => false, context: :none)
|
1390
|
-
assert_equal Time.local(2006, 10, 1), time.begin
|
1391
|
-
assert_equal Time.local(2007, 1, 1), time.end
|
1392
|
-
|
1393
|
-
time = parse_now("last #{string}", :guess => false, context: :none)
|
1394
|
-
assert_equal Time.local(2005, 10, 1), time.begin
|
1395
|
-
assert_equal Time.local(2006, 1, 1), time.end
|
1396
|
-
|
1397
|
-
time = parse_now("next #{string}", :guess => false, context: :none)
|
1398
|
-
assert_equal Time.local(2006, 10, 1), time.begin
|
1399
|
-
assert_equal Time.local(2007, 1, 1), time.end
|
1400
|
-
end
|
1401
|
-
end
|
1402
|
-
|
1403
1109
|
# regression
|
1404
1110
|
|
1405
1111
|
# def test_partial
|
@@ -1523,10 +1229,6 @@ class TestParsing < TestCase
|
|
1523
1229
|
assert_equal pre_normalize("8:00 pm February 11"), pre_normalize("8:00 p.m. February 11")
|
1524
1230
|
end
|
1525
1231
|
|
1526
|
-
def test_normalizing_time_of_day_phrases
|
1527
|
-
assert_equal pre_normalize("midday February 11"), pre_normalize("12:00 p.m. February 11")
|
1528
|
-
end
|
1529
|
-
|
1530
1232
|
private
|
1531
1233
|
def parse_now(string, options={})
|
1532
1234
|
Chronic.parse(string, {:now => TIME_2006_08_16_14_00_00 }.merge(options))
|