chronic-davispuh 0.10.2.v0.1da32066b3f46f2506b3471e39557497e34afa27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.travis.yml +10 -0
  4. data/Gemfile +3 -0
  5. data/HISTORY.md +243 -0
  6. data/LICENSE +21 -0
  7. data/README.md +185 -0
  8. data/Rakefile +68 -0
  9. data/chronic.gemspec +27 -0
  10. data/lib/chronic.rb +122 -0
  11. data/lib/chronic/arrow.rb +270 -0
  12. data/lib/chronic/date.rb +272 -0
  13. data/lib/chronic/definition.rb +208 -0
  14. data/lib/chronic/dictionary.rb +36 -0
  15. data/lib/chronic/handler.rb +44 -0
  16. data/lib/chronic/handlers/anchor.rb +65 -0
  17. data/lib/chronic/handlers/arrow.rb +84 -0
  18. data/lib/chronic/handlers/date.rb +270 -0
  19. data/lib/chronic/handlers/date_time.rb +72 -0
  20. data/lib/chronic/handlers/general.rb +130 -0
  21. data/lib/chronic/handlers/narrow.rb +54 -0
  22. data/lib/chronic/handlers/time.rb +167 -0
  23. data/lib/chronic/handlers/time_zone.rb +50 -0
  24. data/lib/chronic/objects/anchor_object.rb +263 -0
  25. data/lib/chronic/objects/arrow_object.rb +27 -0
  26. data/lib/chronic/objects/date_object.rb +164 -0
  27. data/lib/chronic/objects/date_time_object.rb +64 -0
  28. data/lib/chronic/objects/handler_object.rb +81 -0
  29. data/lib/chronic/objects/narrow_object.rb +85 -0
  30. data/lib/chronic/objects/time_object.rb +96 -0
  31. data/lib/chronic/objects/time_zone_object.rb +27 -0
  32. data/lib/chronic/parser.rb +154 -0
  33. data/lib/chronic/span.rb +32 -0
  34. data/lib/chronic/tag.rb +84 -0
  35. data/lib/chronic/tags/day_name.rb +34 -0
  36. data/lib/chronic/tags/day_portion.rb +33 -0
  37. data/lib/chronic/tags/day_special.rb +30 -0
  38. data/lib/chronic/tags/grabber.rb +29 -0
  39. data/lib/chronic/tags/keyword.rb +63 -0
  40. data/lib/chronic/tags/month_name.rb +39 -0
  41. data/lib/chronic/tags/ordinal.rb +52 -0
  42. data/lib/chronic/tags/pointer.rb +28 -0
  43. data/lib/chronic/tags/rational.rb +35 -0
  44. data/lib/chronic/tags/scalar.rb +101 -0
  45. data/lib/chronic/tags/season_name.rb +31 -0
  46. data/lib/chronic/tags/separator.rb +130 -0
  47. data/lib/chronic/tags/sign.rb +35 -0
  48. data/lib/chronic/tags/time_special.rb +34 -0
  49. data/lib/chronic/tags/time_zone.rb +56 -0
  50. data/lib/chronic/tags/unit.rb +174 -0
  51. data/lib/chronic/time.rb +141 -0
  52. data/lib/chronic/time_zone.rb +80 -0
  53. data/lib/chronic/token.rb +61 -0
  54. data/lib/chronic/token_group.rb +271 -0
  55. data/lib/chronic/tokenizer.rb +42 -0
  56. data/lib/chronic/version.rb +3 -0
  57. data/test/helper.rb +12 -0
  58. data/test/test_chronic.rb +190 -0
  59. data/test/test_daylight_savings.rb +98 -0
  60. data/test/test_handler.rb +113 -0
  61. data/test/test_parsing.rb +1520 -0
  62. data/test/test_span.rb +23 -0
  63. data/test/test_token.rb +31 -0
  64. metadata +218 -0
@@ -0,0 +1,72 @@
1
+ require 'chronic/handlers/date'
2
+ require 'chronic/handlers/time'
3
+ require 'chronic/handlers/time_zone'
4
+
5
+ module Chronic
6
+ module DateTimeHandlers
7
+ include DateHandlers
8
+ include TimeHandlers
9
+ include TimeZoneHandlers
10
+
11
+ # Handle scalar-year/scalar-month/scalar-day/scalar-hour/scalar-minute/scalar-second
12
+ # formats: yyyy-mm-ddTh:m:s
13
+ def handle_sy_sm_sd_h_m_s
14
+ handle_sy_sm_sd
15
+ next_tag # T
16
+ handle_h_m_s
17
+ end
18
+
19
+ # Handle scalar-year/scalar-month/scalar-day/scalar-hour/scalar-minute/scalar-second
20
+ # formats: yyyy-mm-ddTh:m:sZ
21
+ def handle_sy_sm_sd_h_m_s_tz
22
+ handle_sy_sm_sd
23
+ next_tag # T
24
+ handle_h_m_s
25
+ handle_generic
26
+ end
27
+
28
+ # Handle scalar-year/scalar-month/scalar-day/scalar-hour/scalar-minute/scalar-second/scalar-subsecond
29
+ # formats: yyyy-mm-ddTh:m:s.sss
30
+ def handle_sy_sm_sd_h_m_s_ss
31
+ handle_sy_sm_sd
32
+ next_tag
33
+ handle_h_m_s_ss
34
+ end
35
+
36
+ # Handle scalar-year/scalar-month/scalar-day/scalar-hour/scalar-minute/scalar-second/scalar-subsecond
37
+ # formats: yyyy-mm-ddTh:m:s.sssZ
38
+ def handle_sy_sm_sd_h_m_s_ss_tz
39
+ handle_sy_sm_sd
40
+ next_tag
41
+ handle_h_m_s_ss
42
+ handle_generic
43
+ end
44
+
45
+ # Handle scalar-year/scalar-month/scalar-day/scalar-hour/scalar-minute/scalar-second/sign/scalar-hour/scalar-minute
46
+ # formats: yyyy-mm-ddTh:m:s+hh:mm
47
+ def handle_sy_sm_sd_h_m_s_hh_mm
48
+ handle_sy_sm_sd_h_m_s
49
+ handle_hh_mm
50
+ end
51
+
52
+ # Handle scalar-year/scalar-month/scalar-day/scalar-hour/scalar-minute/scalar-second/scalar-subsecond/sign/scalar-hour/scalar-minute
53
+ # formats: yyyy-mm-ddTh:m:s.sss +hh:mm
54
+ def handle_sy_sm_sd_h_m_s_ss_hh_mm
55
+ handle_sy_sm_sd_h_m_s_ss
56
+ handle_hh_mm
57
+ end
58
+
59
+ # Handle day-name/month-name/scalar-day/scalar-hour/scalar-minute/scalar-second/abbr/scalar-year
60
+ # formats: dn month dd h:m:s abbr yyyy
61
+ def handle_dn_mn_sd_h_m_s_abbr_sy
62
+ handle_dn_mn_sd
63
+ next_tag
64
+ handle_h_m_s
65
+ next_tag
66
+ handle_abbr
67
+ next_tag
68
+ handle_sy
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,130 @@
1
+ module Chronic
2
+ module GeneralHandlers
3
+ def reset_handler
4
+ @index = @begin
5
+ @grabber = nil
6
+ @day_special = nil
7
+ @time_special = nil
8
+ @precision = nil
9
+ end
10
+
11
+ # Handle at
12
+ # formats: at
13
+ def handle_at
14
+ handle_possible(KeywordAt)
15
+ handle_possible(SeparatorSpace)
16
+ end
17
+
18
+ # Handle in
19
+ # formats: in
20
+ def handle_in
21
+ handle_possible(KeywordIn)
22
+ handle_possible(SeparatorSpace)
23
+ end
24
+
25
+ # Handle grabber
26
+ # formats: gr
27
+ def handle_gr
28
+ @grabber = @tokens[@index].get_tag(Grabber).type
29
+ @index += 1
30
+ end
31
+
32
+ # Handle ordinal
33
+ # formats: o
34
+ def handle_o
35
+ @number = @tokens[@index].get_tag(Ordinal).type
36
+ @count = @number
37
+ @index += 1
38
+ end
39
+
40
+ # Handle scalar
41
+ # formats: s
42
+ def handle_s
43
+ @count = @tokens[@index].get_tag(Scalar).type
44
+ @index += 1
45
+ end
46
+
47
+ # Handle scalar-year
48
+ # formats: yyyy
49
+ def handle_sy
50
+ handle_possible(SeparatorSpace)
51
+ handle_possible(SeparatorApostrophe)
52
+ @year = @tokens[@index].get_tag(ScalarYear).type
53
+ @have_year = true
54
+ @index += 1
55
+ @precision = :year
56
+ end
57
+
58
+ # Handle rational
59
+ # formats: r
60
+ def handle_r
61
+ @count = @tokens[@index].get_tag(Rational).type
62
+ @index += 1
63
+ end
64
+
65
+ # Handle unit
66
+ # formats: u
67
+ def handle_u
68
+ @unit = @tokens[@index].get_tag(Unit).type
69
+ @index += 1
70
+ @precision = :unit
71
+ end
72
+
73
+ # Handle time-special
74
+ # formats: ts
75
+ def handle_ts
76
+ handle_at
77
+ handle_in
78
+ @time_special = @tokens[@index].get_tag(TimeSpecial).type
79
+ @index += 1
80
+ @precision = :time_special
81
+ end
82
+
83
+ # Handle day-special
84
+ # formats: ds
85
+ def handle_ds
86
+ @day_special = @tokens[@index].get_tag(DaySpecial).type
87
+ @index += 1
88
+ @precision = :day_special
89
+ end
90
+
91
+ # Handle day-name
92
+ # formats: dn
93
+ def handle_dn
94
+ @wday = Date::DAYS[@tokens[@index].get_tag(DayName).type]
95
+ @index += 1
96
+ @precision = :day
97
+ end
98
+
99
+ # Handle month-name
100
+ # formats: month
101
+ def handle_mn
102
+ handle_possible(SeparatorSpace)
103
+ handle_possible(SeparatorSpace) if handle_possible(KeywordIn)
104
+ @month = Date::MONTHS[@tokens[@index].get_tag(MonthName).type]
105
+ @index += 1
106
+ @precision = :month
107
+ end
108
+
109
+ # Handle season-name
110
+ # formats: season
111
+ def handle_sn
112
+ handle_possible(SeparatorSpace)
113
+ @season = @tokens[@index].get_tag(SeasonName).type
114
+ @index += 1
115
+ @precision = :season
116
+ end
117
+
118
+ # Handle keyword-q/scalar
119
+ # formats: Qs
120
+ def handle_q_s
121
+ next_tag # Q
122
+ @quarter = @tokens[@index].get_tag(Scalar).type
123
+ @number = @quarter
124
+ @index += 1
125
+ @precision = :quarter
126
+ @unit = :quarter
127
+ end
128
+
129
+ end
130
+ end
@@ -0,0 +1,54 @@
1
+ require 'chronic/handlers/general'
2
+
3
+ module Chronic
4
+ module NarrowHandlers
5
+ include GeneralHandlers
6
+
7
+ # Handle ordinal/day-name
8
+ # formats: o u
9
+ def handle_o_dn
10
+ handle_o
11
+ handle_possible(SeparatorSpace)
12
+ handle_dn
13
+ end
14
+
15
+ # Handle ordinal/unit
16
+ # formats: o u
17
+ def handle_o_u
18
+ handle_o
19
+ handle_possible(SeparatorSpace)
20
+ handle_u
21
+ end
22
+
23
+ # Handle ordinal-day/grabber/unit-month
24
+ # formats: dd(st|nd|rd|th) of last/this/next month
25
+ def handle_od_gr
26
+ handle_od
27
+ next_tag
28
+ handle_gr
29
+ # ignore unit
30
+ @year, @month = Date::add_month(@local_date.year, @local_date.month, @modifier)
31
+ next_tag
32
+ end
33
+
34
+ # Handle ordinal-month/grabber/unit-year
35
+ # formats: mm(st|nd|rd|th) of last/this/next year
36
+ def handle_om_gr
37
+ handle_om
38
+ next_tag
39
+ handle_gr
40
+ # ignore unit
41
+ @year = @local_date.year + @modifier
42
+ next_tag
43
+ end
44
+
45
+ # Handle grabber/ordinal/unit
46
+ # formats: last/this/next o u
47
+ def handle_gr_o_u
48
+ handle_gr
49
+ next_tag
50
+ handle_o_u
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,167 @@
1
+ require 'chronic/handlers/general'
2
+
3
+ module Chronic
4
+ module TimeHandlers
5
+ include GeneralHandlers
6
+ # Handle scalar-hour/scalar-minute/scalar-second/scalar-subsecond
7
+ # formats: h:m:s.SSS, h:m:s:SSS
8
+ def handle_h_m_s_ss
9
+ handle_h_m_s
10
+ next_tag
11
+ subsecond = @tokens[@index].get_tag(ScalarSubsecond)
12
+ @subsecond_size = 10 ** subsecond.width
13
+ @subsecond = subsecond.type.to_f / subsecond_size
14
+ next_tag
15
+ @precision = :subsecond
16
+ end
17
+
18
+ # Handle scalar-hour/scalar-minute/scalar-second/day-portion
19
+ # formats: h:m:s dp
20
+ def handle_h_m_s_dp
21
+ handle_h_m_s
22
+ handle_dp
23
+ end
24
+
25
+ # Handle scalar-hour/scalar-minute/scalar-second
26
+ # formats: h:m:s
27
+ def handle_h_m_s
28
+ handle_h_m
29
+ next_tag
30
+ handle_s
31
+ end
32
+
33
+ # Handle scalar-wide/day-portion
34
+ # formats: hhmm dp
35
+ def handle_hhmm_dp
36
+ handle_hhmm
37
+ handle_dp
38
+ end
39
+
40
+ # Handle scalar-hour/scalar-minute/time-special
41
+ # formats: h:m in ts
42
+ def handle_h_m_ts
43
+ handle_h_m
44
+ next_tag
45
+ next_tag # in
46
+ next_tag
47
+ handle_ts
48
+ @precision = :minute
49
+ end
50
+
51
+ # Handle scalar-hour/scalar-minute/day-portion
52
+ # formats: h:m dp, h.m dp
53
+ def handle_h_m_dp
54
+ handle_h_m
55
+ handle_dp
56
+ end
57
+
58
+ # Handle time-special/scalar-hour/scalar-minute
59
+ # formats: ts at h:m
60
+ def handle_ts_h_m
61
+ handle_ts
62
+ next_tag
63
+ handle_h_m
64
+ end
65
+
66
+ # Handle time-special/scalar-hour
67
+ # formats: ts at h
68
+ def handle_ts_h
69
+ handle_ts
70
+ next_tag
71
+ handle_h
72
+ end
73
+
74
+ # Handle scalar-hour/scalar-minute
75
+ # formats: h:m, h.m
76
+ def handle_h_m
77
+ handle_h
78
+ next_tag
79
+ handle_m
80
+ end
81
+
82
+ # Handle scalar-wide
83
+ # formats: hhmm
84
+ def handle_hhmm
85
+ handle_at
86
+ wide = @tokens[@index].get_tag(ScalarWide).type
87
+ @hour = wide[0, 2].to_i
88
+ @minute = wide[2, 4].to_i
89
+ @ambiguous = false
90
+ next_tag
91
+ @precision = :minute
92
+ end
93
+
94
+ # Handle grabber/day-portion
95
+ # formats: gr dp
96
+ def handle_gr_dp
97
+ handle_gr
98
+ next_tag
99
+ handle_dp
100
+ end
101
+
102
+ # Handle scalar-hour/time-special
103
+ # formats: h at|in ts
104
+ def handle_h_ts
105
+ handle_h
106
+ next_tag
107
+ handle_ts
108
+ @precision = :hour
109
+ end
110
+
111
+ # Handle scalar-hour/day-portion
112
+ # formats: h dp
113
+ def handle_h_dp
114
+ handle_h
115
+ handle_possible(SeparatorSpace)
116
+ handle_dp
117
+ end
118
+
119
+ # Handle scalar-hour/keyword-to/scalar-hour
120
+ # formats: h to h
121
+ def handle_h_h
122
+ handle_h
123
+ next_tag # space
124
+ next_tag # to
125
+ next_tag # space
126
+ handle_h
127
+ end
128
+
129
+ # Handle scalar-hour
130
+ # formats: h
131
+ def handle_h
132
+ handle_at
133
+ handle_possible(SeparatorSpace)
134
+ tag = @tokens[@index].get_tag(ScalarHour)
135
+ @hour = tag.type
136
+ @ambiguous = false if @hour.zero? or @hour > 12 or (tag.width >= 2 and @hour < 10 and @options[:hours24] != false)
137
+ next_tag
138
+ @precision = :hour
139
+ end
140
+
141
+ # Handle scalar-minute
142
+ # formats: m
143
+ def handle_m
144
+ @minute = @tokens[@index].get_tag(ScalarMinute).type
145
+ next_tag
146
+ @precision = :minute
147
+ end
148
+
149
+ # Handle scalar-second
150
+ # formats: s
151
+ def handle_s
152
+ @second = @tokens[@index].get_tag(ScalarSecond).type
153
+ next_tag
154
+ @precision = :second
155
+ end
156
+
157
+ # Handle day-portion
158
+ # formats: dp
159
+ def handle_dp
160
+ handle_at
161
+ handle_in
162
+ @day_portion = @tokens[@index].get_tag(DayPortion).type
163
+ next_tag
164
+ end
165
+
166
+ end
167
+ end
@@ -0,0 +1,50 @@
1
+ module Chronic
2
+ module TimeZoneHandlers
3
+
4
+ # Handle offset
5
+ # formats: UTC, Z
6
+ def handle_generic
7
+ @offset = @tokens[@index].get_tag(TimeZoneGeneric).type
8
+ next_tag
9
+ end
10
+
11
+ # Handle abbr
12
+ # formats: UTC, GMT, etc
13
+ def handle_abbr
14
+ @abbr = @tokens[@index].get_tag(TimeZoneAbbreviation).type
15
+ next_tag
16
+ end
17
+
18
+ # Handle scalar-wide
19
+ # formats: hhmm
20
+ def handle_hhmm
21
+ handle_sign
22
+ wide = @tokens[@index].get_tag(ScalarWide).type
23
+ @tzhour = wide[0, 2].to_i
24
+ @tzminute = wide[2, 4].to_i
25
+ next_tag
26
+ end
27
+
28
+ # Handle sign/scalar-hour/scalar-minute
29
+ # formats: +hh:mm, -hh:mm
30
+ def handle_hh_mm
31
+ handle_sign
32
+ @tzhour = @tokens[@index].get_tag(ScalarHour).type
33
+ next_tag
34
+ next_tag
35
+ @tzminute = @tokens[@index].get_tag(ScalarMinute).type
36
+ next_tag
37
+ end
38
+
39
+ # Handle sign
40
+ # formats: +, -
41
+ def handle_sign
42
+ sign = @tokens[@index].get_tag(Sign)
43
+ if sign
44
+ @sign = sign
45
+ next_tag
46
+ end
47
+ end
48
+
49
+ end
50
+ end