chronic 0.3.0 → 0.4.0

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