chronic 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +27 -0
- data/Manifest.txt +16 -5
- data/README.md +14 -8
- data/Rakefile +2 -8
- data/chronic.gemspec +8 -11
- data/lib/chronic.rb +21 -14
- data/lib/chronic/chronic.rb +38 -130
- data/lib/chronic/grabber.rb +11 -15
- data/lib/chronic/handlers.rb +63 -40
- data/lib/chronic/mini_date.rb +27 -0
- data/lib/chronic/numerizer.rb +120 -0
- data/lib/chronic/ordinal.rb +5 -10
- data/lib/chronic/pointer.rb +8 -10
- data/lib/chronic/repeater.rb +106 -109
- data/lib/chronic/repeaters/repeater_day.rb +43 -41
- data/lib/chronic/repeaters/repeater_day_name.rb +38 -36
- data/lib/chronic/repeaters/repeater_day_portion.rb +74 -73
- data/lib/chronic/repeaters/repeater_fortnight.rb +57 -55
- data/lib/chronic/repeaters/repeater_hour.rb +46 -44
- data/lib/chronic/repeaters/repeater_minute.rb +46 -44
- data/lib/chronic/repeaters/repeater_month.rb +52 -50
- data/lib/chronic/repeaters/repeater_month_name.rb +84 -80
- data/lib/chronic/repeaters/repeater_season.rb +97 -119
- data/lib/chronic/repeaters/repeater_season_name.rb +39 -39
- data/lib/chronic/repeaters/repeater_second.rb +32 -30
- data/lib/chronic/repeaters/repeater_time.rb +106 -101
- data/lib/chronic/repeaters/repeater_week.rb +60 -58
- data/lib/chronic/repeaters/repeater_weekday.rb +67 -58
- data/lib/chronic/repeaters/repeater_weekend.rb +54 -52
- data/lib/chronic/repeaters/repeater_year.rb +50 -48
- data/lib/chronic/scalar.rb +24 -16
- data/lib/chronic/separator.rb +15 -33
- data/lib/chronic/span.rb +31 -0
- data/lib/chronic/tag.rb +26 -0
- data/lib/chronic/time_zone.rb +7 -9
- data/lib/chronic/token.rb +35 -0
- data/test/helper.rb +5 -6
- data/test/test_Chronic.rb +5 -0
- data/test/test_Numerizer.rb +60 -39
- data/test/test_RepeaterHour.rb +4 -0
- data/test/test_parsing.rb +104 -13
- metadata +14 -20
- data/lib/chronic/numerizer/numerizer.rb +0 -97
@@ -1,57 +1,59 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Chronic
|
2
|
+
class RepeaterHour < Repeater #:nodoc:
|
3
|
+
HOUR_SECONDS = 3600 # 60 * 60
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
def initialize(type)
|
6
|
+
super
|
7
|
+
@current_hour_start = nil
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
def next(pointer)
|
11
|
+
super
|
12
|
+
|
13
|
+
if !@current_hour_start
|
14
|
+
case pointer
|
15
|
+
when :future
|
16
|
+
@current_hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour + 1)
|
17
|
+
when :past
|
18
|
+
@current_hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour - 1)
|
19
|
+
end
|
20
|
+
else
|
21
|
+
direction = pointer == :future ? 1 : -1
|
22
|
+
@current_hour_start += direction * HOUR_SECONDS
|
23
|
+
end
|
24
|
+
|
25
|
+
Span.new(@current_hour_start, @current_hour_start + HOUR_SECONDS)
|
26
|
+
end
|
27
|
+
|
28
|
+
def this(pointer = :future)
|
29
|
+
super
|
11
30
|
|
12
|
-
if !@current_hour_start
|
13
31
|
case pointer
|
14
32
|
when :future
|
15
|
-
|
33
|
+
hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min + 1)
|
34
|
+
hour_end = Time.construct(@now.year, @now.month, @now.day, @now.hour + 1)
|
16
35
|
when :past
|
17
|
-
|
36
|
+
hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour)
|
37
|
+
hour_end = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
|
38
|
+
when :none
|
39
|
+
hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour)
|
40
|
+
hour_end = hour_start + HOUR_SECONDS
|
18
41
|
end
|
19
|
-
else
|
20
|
-
direction = pointer == :future ? 1 : -1
|
21
|
-
@current_hour_start += direction * HOUR_SECONDS
|
22
|
-
end
|
23
|
-
|
24
|
-
Chronic::Span.new(@current_hour_start, @current_hour_start + HOUR_SECONDS)
|
25
|
-
end
|
26
42
|
|
27
|
-
|
28
|
-
super
|
29
|
-
|
30
|
-
case pointer
|
31
|
-
when :future
|
32
|
-
hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min + 1)
|
33
|
-
hour_end = Time.construct(@now.year, @now.month, @now.day, @now.hour + 1)
|
34
|
-
when :past
|
35
|
-
hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour)
|
36
|
-
hour_end = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
|
37
|
-
when :none
|
38
|
-
hour_start = Time.construct(@now.year, @now.month, @now.day, @now.hour)
|
39
|
-
hour_end = hour_begin + HOUR_SECONDS
|
43
|
+
Span.new(hour_start, hour_end)
|
40
44
|
end
|
41
45
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
direction = pointer == :future ? 1 : -1
|
47
|
-
span + direction * amount * HOUR_SECONDS
|
48
|
-
end
|
46
|
+
def offset(span, amount, pointer)
|
47
|
+
direction = pointer == :future ? 1 : -1
|
48
|
+
span + direction * amount * HOUR_SECONDS
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
def width
|
52
|
+
HOUR_SECONDS
|
53
|
+
end
|
53
54
|
|
54
|
-
|
55
|
-
|
55
|
+
def to_s
|
56
|
+
super << '-hour'
|
57
|
+
end
|
56
58
|
end
|
57
|
-
end
|
59
|
+
end
|
@@ -1,57 +1,59 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Chronic
|
2
|
+
class RepeaterMinute < Repeater #:nodoc:
|
3
|
+
MINUTE_SECONDS = 60
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
def initialize(type)
|
6
|
+
super
|
7
|
+
@current_minute_start = nil
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
def next(pointer = :future)
|
11
|
+
super
|
12
|
+
|
13
|
+
if !@current_minute_start
|
14
|
+
case pointer
|
15
|
+
when :future
|
16
|
+
@current_minute_start = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min + 1)
|
17
|
+
when :past
|
18
|
+
@current_minute_start = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min - 1)
|
19
|
+
end
|
20
|
+
else
|
21
|
+
direction = pointer == :future ? 1 : -1
|
22
|
+
@current_minute_start += direction * MINUTE_SECONDS
|
23
|
+
end
|
24
|
+
|
25
|
+
Span.new(@current_minute_start, @current_minute_start + MINUTE_SECONDS)
|
26
|
+
end
|
27
|
+
|
28
|
+
def this(pointer = :future)
|
29
|
+
super
|
11
30
|
|
12
|
-
if !@current_minute_start
|
13
31
|
case pointer
|
14
32
|
when :future
|
15
|
-
|
33
|
+
minute_begin = @now
|
34
|
+
minute_end = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
|
16
35
|
when :past
|
17
|
-
|
36
|
+
minute_begin = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
|
37
|
+
minute_end = @now
|
38
|
+
when :none
|
39
|
+
minute_begin = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
|
40
|
+
minute_end = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min) + MINUTE_SECONDS
|
18
41
|
end
|
19
|
-
else
|
20
|
-
direction = pointer == :future ? 1 : -1
|
21
|
-
@current_minute_start += direction * MINUTE_SECONDS
|
22
|
-
end
|
23
|
-
|
24
|
-
Chronic::Span.new(@current_minute_start, @current_minute_start + MINUTE_SECONDS)
|
25
|
-
end
|
26
42
|
|
27
|
-
|
28
|
-
super
|
29
|
-
|
30
|
-
case pointer
|
31
|
-
when :future
|
32
|
-
minute_begin = @now
|
33
|
-
minute_end = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
|
34
|
-
when :past
|
35
|
-
minute_begin = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
|
36
|
-
minute_end = @now
|
37
|
-
when :none
|
38
|
-
minute_begin = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
|
39
|
-
minute_end = Time.construct(@now.year, @now.month, @now.day, @now.hour, @now.min) + MINUTE_SECONDS
|
43
|
+
Span.new(minute_begin, minute_end)
|
40
44
|
end
|
41
45
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
direction = pointer == :future ? 1 : -1
|
47
|
-
span + direction * amount * MINUTE_SECONDS
|
48
|
-
end
|
46
|
+
def offset(span, amount, pointer)
|
47
|
+
direction = pointer == :future ? 1 : -1
|
48
|
+
span + direction * amount * MINUTE_SECONDS
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
def width
|
52
|
+
MINUTE_SECONDS
|
53
|
+
end
|
53
54
|
|
54
|
-
|
55
|
-
|
55
|
+
def to_s
|
56
|
+
super << '-minute'
|
57
|
+
end
|
56
58
|
end
|
57
|
-
end
|
59
|
+
end
|
@@ -1,66 +1,68 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module Chronic
|
2
|
+
class RepeaterMonth < Repeater #:nodoc:
|
3
|
+
MONTH_SECONDS = 2_592_000 # 30 * 24 * 60 * 60
|
4
|
+
YEAR_MONTHS = 12
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def initialize(type)
|
7
|
+
super
|
8
|
+
@current_month_start = nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def next(pointer)
|
12
|
+
super
|
9
13
|
|
10
|
-
|
11
|
-
|
14
|
+
if !@current_month_start
|
15
|
+
@current_month_start = offset_by(Time.construct(@now.year, @now.month), 1, pointer)
|
16
|
+
else
|
17
|
+
@current_month_start = offset_by(Time.construct(@current_month_start.year, @current_month_start.month), 1, pointer)
|
18
|
+
end
|
12
19
|
|
13
|
-
|
14
|
-
@current_month_start = offset_by(Time.construct(@now.year, @now.month), 1, pointer)
|
15
|
-
else
|
16
|
-
@current_month_start = offset_by(Time.construct(@current_month_start.year, @current_month_start.month), 1, pointer)
|
20
|
+
Span.new(@current_month_start, Time.construct(@current_month_start.year, @current_month_start.month + 1))
|
17
21
|
end
|
18
22
|
|
19
|
-
|
20
|
-
|
23
|
+
def this(pointer = :future)
|
24
|
+
super
|
21
25
|
|
22
|
-
|
23
|
-
|
26
|
+
case pointer
|
27
|
+
when :future
|
28
|
+
month_start = Time.construct(@now.year, @now.month, @now.day + 1)
|
29
|
+
month_end = self.offset_by(Time.construct(@now.year, @now.month), 1, :future)
|
30
|
+
when :past
|
31
|
+
month_start = Time.construct(@now.year, @now.month)
|
32
|
+
month_end = Time.construct(@now.year, @now.month, @now.day)
|
33
|
+
when :none
|
34
|
+
month_start = Time.construct(@now.year, @now.month)
|
35
|
+
month_end = self.offset_by(Time.construct(@now.year, @now.month), 1, :future)
|
36
|
+
end
|
24
37
|
|
25
|
-
|
26
|
-
when :future
|
27
|
-
month_start = Time.construct(@now.year, @now.month, @now.day + 1)
|
28
|
-
month_end = self.offset_by(Time.construct(@now.year, @now.month), 1, :future)
|
29
|
-
when :past
|
30
|
-
month_start = Time.construct(@now.year, @now.month)
|
31
|
-
month_end = Time.construct(@now.year, @now.month, @now.day)
|
32
|
-
when :none
|
33
|
-
month_start = Time.construct(@now.year, @now.month)
|
34
|
-
month_end = self.offset_by(Time.construct(@now.year, @now.month), 1, :future)
|
38
|
+
Span.new(month_start, month_end)
|
35
39
|
end
|
36
40
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
def offset(span, amount, pointer)
|
41
|
-
Chronic::Span.new(offset_by(span.begin, amount, pointer), offset_by(span.end, amount, pointer))
|
42
|
-
end
|
41
|
+
def offset(span, amount, pointer)
|
42
|
+
Span.new(offset_by(span.begin, amount, pointer), offset_by(span.end, amount, pointer))
|
43
|
+
end
|
43
44
|
|
44
|
-
|
45
|
-
|
45
|
+
def offset_by(time, amount, pointer)
|
46
|
+
direction = pointer == :future ? 1 : -1
|
46
47
|
|
47
|
-
|
48
|
-
|
48
|
+
amount_years = direction * amount / YEAR_MONTHS
|
49
|
+
amount_months = direction * amount % YEAR_MONTHS
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
51
|
+
new_year = time.year + amount_years
|
52
|
+
new_month = time.month + amount_months
|
53
|
+
if new_month > YEAR_MONTHS
|
54
|
+
new_year += 1
|
55
|
+
new_month -= YEAR_MONTHS
|
56
|
+
end
|
57
|
+
Time.construct(new_year, new_month, time.day, time.hour, time.min, time.sec)
|
55
58
|
end
|
56
|
-
Time.construct(new_year, new_month, time.day, time.hour, time.min, time.sec)
|
57
|
-
end
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
60
|
+
def width
|
61
|
+
MONTH_SECONDS
|
62
|
+
end
|
62
63
|
|
63
|
-
|
64
|
-
|
64
|
+
def to_s
|
65
|
+
super << '-month'
|
66
|
+
end
|
65
67
|
end
|
66
|
-
end
|
68
|
+
end
|
@@ -1,98 +1,102 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Chronic
|
2
|
+
class RepeaterMonthName < Repeater #:nodoc:
|
3
|
+
MONTH_SECONDS = 2_592_000 # 30 * 24 * 60 * 60
|
4
|
+
MONTHS = {
|
5
|
+
:january => 1,
|
6
|
+
:february => 2,
|
7
|
+
:march => 3,
|
8
|
+
:april => 4,
|
9
|
+
:may => 5,
|
10
|
+
:june => 6,
|
11
|
+
:july => 7,
|
12
|
+
:august => 8,
|
13
|
+
:september => 9,
|
14
|
+
:october => 10,
|
15
|
+
:november => 11,
|
16
|
+
:december => 12
|
17
|
+
}
|
3
18
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
19
|
+
def initialize(type)
|
20
|
+
super
|
21
|
+
@current_month_begin = nil
|
22
|
+
end
|
8
23
|
|
9
|
-
|
10
|
-
|
24
|
+
def next(pointer)
|
25
|
+
super
|
11
26
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
27
|
+
if !@current_month_begin
|
28
|
+
target_month = symbol_to_number(@type)
|
29
|
+
case pointer
|
30
|
+
when :future
|
31
|
+
if @now.month < target_month
|
32
|
+
@current_month_begin = Time.construct(@now.year, target_month)
|
33
|
+
elsif @now.month > target_month
|
34
|
+
@current_month_begin = Time.construct(@now.year + 1, target_month)
|
35
|
+
end
|
36
|
+
when :none
|
37
|
+
if @now.month <= target_month
|
38
|
+
@current_month_begin = Time.construct(@now.year, target_month)
|
39
|
+
elsif @now.month > target_month
|
40
|
+
@current_month_begin = Time.construct(@now.year + 1, target_month)
|
41
|
+
end
|
42
|
+
when :past
|
43
|
+
if @now.month >= target_month
|
44
|
+
@current_month_begin = Time.construct(@now.year, target_month)
|
45
|
+
elsif @now.month < target_month
|
46
|
+
@current_month_begin = Time.construct(@now.year - 1, target_month)
|
47
|
+
end
|
20
48
|
end
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
@current_month_begin = Time.construct(@
|
49
|
+
@current_month_begin || raise("Current month should be set by now")
|
50
|
+
else
|
51
|
+
case pointer
|
52
|
+
when :future
|
53
|
+
@current_month_begin = Time.construct(@current_month_begin.year + 1, @current_month_begin.month)
|
54
|
+
when :past
|
55
|
+
@current_month_begin = Time.construct(@current_month_begin.year - 1, @current_month_begin.month)
|
26
56
|
end
|
27
|
-
when :past
|
28
|
-
if @now.month > target_month
|
29
|
-
@current_month_begin = Time.construct(@now.year, target_month)
|
30
|
-
elsif @now.month < target_month
|
31
|
-
@current_month_begin = Time.construct(@now.year - 1, target_month)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
@current_month_begin || raise("Current month should be set by now")
|
35
|
-
else
|
36
|
-
case pointer
|
37
|
-
when :future
|
38
|
-
@current_month_begin = Time.construct(@current_month_begin.year + 1, @current_month_begin.month)
|
39
|
-
when :past
|
40
|
-
@current_month_begin = Time.construct(@current_month_begin.year - 1, @current_month_begin.month)
|
41
57
|
end
|
42
|
-
end
|
43
58
|
|
44
|
-
|
45
|
-
|
59
|
+
cur_month_year = @current_month_begin.year
|
60
|
+
cur_month_month = @current_month_begin.month
|
46
61
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
62
|
+
if cur_month_month == 12
|
63
|
+
next_month_year = cur_month_year + 1
|
64
|
+
next_month_month = 1
|
65
|
+
else
|
66
|
+
next_month_year = cur_month_year
|
67
|
+
next_month_month = cur_month_month + 1
|
68
|
+
end
|
54
69
|
|
55
|
-
|
56
|
-
|
70
|
+
Span.new(@current_month_begin, Time.construct(next_month_year, next_month_month))
|
71
|
+
end
|
57
72
|
|
58
|
-
|
59
|
-
|
73
|
+
def this(pointer = :future)
|
74
|
+
super
|
60
75
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
76
|
+
case pointer
|
77
|
+
when :past
|
78
|
+
self.next(pointer)
|
79
|
+
when :future, :none
|
80
|
+
self.next(:none)
|
81
|
+
end
|
66
82
|
end
|
67
|
-
end
|
68
83
|
|
69
|
-
|
70
|
-
|
71
|
-
|
84
|
+
def width
|
85
|
+
MONTH_SECONDS
|
86
|
+
end
|
72
87
|
|
73
|
-
|
74
|
-
|
75
|
-
|
88
|
+
def index
|
89
|
+
symbol_to_number(@type)
|
90
|
+
end
|
76
91
|
|
77
|
-
|
78
|
-
|
79
|
-
|
92
|
+
def to_s
|
93
|
+
super << '-monthname-' << @type.to_s
|
94
|
+
end
|
80
95
|
|
81
|
-
|
96
|
+
private
|
82
97
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
:march => 3,
|
87
|
-
:april => 4,
|
88
|
-
:may => 5,
|
89
|
-
:june => 6,
|
90
|
-
:july => 7,
|
91
|
-
:august => 8,
|
92
|
-
:september => 9,
|
93
|
-
:october => 10,
|
94
|
-
:november => 11,
|
95
|
-
:december => 12}
|
96
|
-
lookup[sym] || raise("Invalid symbol specified")
|
98
|
+
def symbol_to_number(sym)
|
99
|
+
MONTHS[sym] || raise("Invalid symbol specified")
|
100
|
+
end
|
97
101
|
end
|
98
|
-
end
|
102
|
+
end
|