date_period_parser 0.2.1 → 0.2.3
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.
- checksums.yaml +4 -4
- data/README.md +9 -8
- data/lib/date_period_parser.rb +106 -32
- data/lib/date_period_parser/version.rb +1 -1
- data/spec/date_period_parser_spec.rb +68 -10
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90516f6773fd43e04a5134ea3c2d067d885ea5f8
|
4
|
+
data.tar.gz: 4038d871c3a4339bb709ef6e7010a4947ad5b2a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4e833f6ad144dcf5c2883feb4f4f5d0fc31a245b1dce0b39fc22ff4310cb4f9d2f9405043a0bf0c404d50437a051bb00e974bdceb6b3c1e4d5ec79015e79c3a
|
7
|
+
data.tar.gz: c28fefc56507da2baa84c8dd79dece8e8cb0a5e3b09e6558724894d4d59946c5e07f0116d095b71dcdf5e9138242febbe116d048e9e2218ff5bc74120da77ec0
|
data/README.md
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
# DatePeriodParser
|
4
4
|
|
5
|
-
Parse a date-
|
5
|
+
Parse a date-period string like 2015-Q1 and returns the start and end DateTime.
|
6
6
|
|
7
|
-
|
7
|
+
Useful for reports to filterin records for a given time period.
|
8
8
|
|
9
9
|
```ruby
|
10
10
|
# Example useage in a rails controller
|
@@ -12,10 +12,7 @@ This can be used to pass date-periods for URL parameters or query strings, e.g.
|
|
12
12
|
class PostsController
|
13
13
|
# GET /posts?period=2015-08
|
14
14
|
def index
|
15
|
-
date_range = DatePeriodParser.range(params["period"])
|
16
|
-
# with user specific timezones
|
17
|
-
# date_range = DatePeriodParser.range(params["period"], offset: current_user.timezone)
|
18
|
-
date_range ||= DatePeriodParser.range("current-month") # default
|
15
|
+
date_range = DatePeriodParser.range(params["period"], default: "this-month")
|
19
16
|
@posts = Posts.where(created_at: date_range)
|
20
17
|
end
|
21
18
|
end
|
@@ -115,8 +112,12 @@ from ||= DateTime.yesterday
|
|
115
112
|
until ||= DateTime.now
|
116
113
|
|
117
114
|
# parse! raises ArgumentError for invalid periods
|
118
|
-
from,until = DatePeriodParser.parse("123213")
|
115
|
+
from,until = DatePeriodParser.parse!("123213")
|
119
116
|
#=> ArgumentError
|
117
|
+
|
118
|
+
# use :default option when period is optional
|
119
|
+
DatePeriodParser.parse(nil, default: "2014")
|
120
|
+
DatePeriodParser.parse("", default: "2014")
|
120
121
|
```
|
121
122
|
|
122
123
|
### DatePeriodParser.range
|
@@ -132,7 +133,7 @@ rng = DatePeriodParser.range("dsf89sfd")
|
|
132
133
|
# => nil
|
133
134
|
|
134
135
|
# range! raises ArgumentError for invalid periods
|
135
|
-
rng = DatePeriodParser.range("dsf89sfd")
|
136
|
+
rng = DatePeriodParser.range!("dsf89sfd")
|
136
137
|
#=> ArgumentError
|
137
138
|
```
|
138
139
|
|
data/lib/date_period_parser.rb
CHANGED
@@ -5,76 +5,144 @@ require 'date'
|
|
5
5
|
module DatePeriodParser
|
6
6
|
DEFAULT_OFFSET = "+00:00".freeze
|
7
7
|
|
8
|
-
#
|
8
|
+
# Returns array of start and end DateTime of given period string.
|
9
9
|
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
10
|
+
# @example Basic useage
|
11
|
+
# DatePeriodParser.parse("2014")
|
12
|
+
# # => [
|
13
|
+
# # #<DateTime 2014-01-01T00:00:00.000+0000">,
|
14
|
+
# # #<DateTime 2014-12-31T23:59:59.999+0000">
|
15
|
+
# # ]
|
13
16
|
#
|
14
|
-
#
|
17
|
+
# @example with timezone offsets:
|
15
18
|
# from,until = DatePeriodParser.parse("2014", offset: "+0700")
|
16
|
-
# from
|
17
|
-
# until # => #<DateTime 2014-12-31T23:59:59.999+0700"
|
19
|
+
# from # => #<DateTime 2014-01-01T00:00:00.000+0700">
|
20
|
+
# until # => #<DateTime 2014-12-31T23:59:59.999+0700">
|
18
21
|
#
|
19
|
-
#
|
22
|
+
# @example invalid periods
|
20
23
|
# DatePeriodParser.parse("123213") # => nil
|
21
24
|
# from,until = DatePeriodParser.parse("123213")
|
22
|
-
# from
|
23
|
-
# until
|
25
|
+
# from # => nil
|
26
|
+
# until # => nil
|
24
27
|
#
|
28
|
+
# @param [String] period date period string.
|
29
|
+
# @option options [String] :offset ("+0000") timezone offset, e.g. "+0700"
|
30
|
+
# @option options [String] :default (nil) use this default if `period` is nil
|
31
|
+
# @return [Array<DateTime, DateTime>] start and end DateTime
|
32
|
+
# @return [nil] if period string is invalid
|
25
33
|
#
|
26
|
-
|
27
|
-
|
28
|
-
parse!(str, options)
|
34
|
+
def parse(period, options = {})
|
35
|
+
parse!(period, options)
|
29
36
|
rescue ArgumentError => e
|
30
37
|
nil
|
31
38
|
end
|
32
39
|
|
33
|
-
|
34
|
-
|
40
|
+
# Same as #parse but raises an ArgumentError if period string is invalid
|
41
|
+
#
|
42
|
+
# @example Basic useage
|
43
|
+
# def my_method
|
44
|
+
# from,until = DatePeriodParser.parse!("FOOBAR")
|
45
|
+
# rescue ArgumentError => e
|
46
|
+
# # do something
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# @see #parse
|
50
|
+
# @param [String] period date period string.
|
51
|
+
# @raise [ArgumentError] if period string is invalid
|
52
|
+
# @option options [String] :offset ("+0000") timezone offset, e.g. "+0700"
|
53
|
+
# @option options [String] :default (nil) use this default if `period` is nil
|
54
|
+
# @return [Array<DateTime, DateTime>] start and end DateTime
|
55
|
+
#
|
56
|
+
def parse!(period, options = {})
|
57
|
+
period = options[:default] if period.nil? || period.empty?
|
58
|
+
Base.new(period, options).parse
|
35
59
|
end
|
36
60
|
|
37
|
-
|
38
|
-
|
61
|
+
# Same as #parse but returns a range instead
|
62
|
+
#
|
63
|
+
# @example Basic useage
|
64
|
+
# rng = DatePeriodParser.range("2014")
|
65
|
+
# rng.member? DateTime.new(2014,8,6)
|
66
|
+
#
|
67
|
+
# @see #parse
|
68
|
+
# @param [String] period date period string.
|
69
|
+
# @raise [ArgumentError] if period string is invalid
|
70
|
+
# @option options [String] :offset ("+0000") timezone offset, e.g. "+0700"
|
71
|
+
# @option options [String] :default (nil) use this default if `period` is nil
|
72
|
+
# @return [Range<DateTime, DateTime>] start and end DateTime as range
|
73
|
+
#
|
74
|
+
def range(period, options = {})
|
75
|
+
range!(period, options)
|
39
76
|
rescue ArgumentError => e
|
40
77
|
nil
|
41
78
|
end
|
42
79
|
|
43
|
-
|
44
|
-
|
80
|
+
# Same as #range but raises an ArgumentError if period string is invalid
|
81
|
+
#
|
82
|
+
# @example Basic useage
|
83
|
+
# def my_method
|
84
|
+
# rng = DatePeriodParser.range!("FOOBAR")
|
85
|
+
# rescue ArgumentError => e
|
86
|
+
# # do something
|
87
|
+
# end
|
88
|
+
#
|
89
|
+
# @see #parse
|
90
|
+
# @param [String] period date period string.
|
91
|
+
# @raise [ArgumentError] if period string is invalid
|
92
|
+
# @option options [String] :offset ("+0000") timezone offset, e.g. "+0700"
|
93
|
+
# @option options [String] :default (nil) use this default if `period` is nil
|
94
|
+
# @return [Range<DateTime, DateTime>] start and end DateTime as range
|
95
|
+
#
|
96
|
+
def range!(period, options = {})
|
97
|
+
period = options[:default] if period.nil? || period.empty?
|
98
|
+
first,last = Base.new(period, offset).parse
|
45
99
|
first..last
|
46
100
|
end
|
47
101
|
|
48
102
|
module_function :parse, :parse!, :range, :range!
|
49
103
|
|
104
|
+
# @api private
|
50
105
|
class Base
|
51
106
|
attr_reader :value, :offset
|
52
107
|
|
53
108
|
def initialize(value, options = nil)
|
54
109
|
options ||= {} # in case someone sends Base.new("", nil)
|
55
|
-
@value = value.freeze
|
110
|
+
@value = value.to_s.freeze
|
56
111
|
@offset = (options[:offset] || options['offset'] || DEFAULT_OFFSET).freeze
|
57
112
|
end
|
58
113
|
|
59
114
|
def parse
|
60
115
|
case @value
|
61
|
-
when /\Atoday\Z/ then parse_date(Date.today)
|
62
|
-
when /\Ayesterday\Z/ then parse_date(Date.today - 1)
|
63
|
-
when /\Ayday\Z/ then parse_date(Date.today - 1)
|
64
|
-
when /\Acurrent-month\Z/ then parse_month(Date.today)
|
65
|
-
when /\Aprevious-month\Z/ then parse_month(Date.today << 1)
|
66
|
-
when /\Acurrent-year\Z/ then parse_year(Date.today)
|
67
|
-
when /\Aprevious-year\Z/ then parse_year(Date.today << 12)
|
68
|
-
when /\Amtd\Z/ then mtd
|
69
|
-
when /\
|
70
|
-
when /\
|
71
|
-
when /\A\d\d\d\d
|
72
|
-
when /\A\d\d\d\d
|
116
|
+
when /\Atoday\Z/i then parse_date(Date.today)
|
117
|
+
when /\Ayesterday\Z/i then parse_date(Date.today - 1)
|
118
|
+
when /\Ayday\Z/i then parse_date(Date.today - 1)
|
119
|
+
when /\Acurrent-month\Z/i then parse_month(Date.today)
|
120
|
+
when /\Aprevious-month\Z/i then parse_month(Date.today << 1)
|
121
|
+
when /\Acurrent-year\Z/i then parse_year(Date.today)
|
122
|
+
when /\Aprevious-year\Z/i then parse_year(Date.today << 12)
|
123
|
+
when /\Amtd\Z/i then mtd
|
124
|
+
when /\Aqtd\Z/i then quarter_of(Date.today.year, Date.today.month)
|
125
|
+
when /\Aytd\Z/i then ytd
|
126
|
+
when /\A\d\d\d\d\-Q\d\Z/i then parse_quarter
|
127
|
+
when /\A\d\d\d\d\Z/i then parse_year
|
128
|
+
when /\A\d\d\d\d\-\d\d\Z/i then parse_month
|
129
|
+
when /\A\d\d\d\d\-\d\d\-\d\d\Z/i then parse_date
|
73
130
|
else raise ArgumentError.new("invalid date period")
|
74
131
|
end
|
75
132
|
end
|
76
133
|
|
77
134
|
protected
|
135
|
+
def quarter_of(year, month)
|
136
|
+
case month
|
137
|
+
when 1..3 then [start_of_date(Date.new(year, 1)), end_of_date(Date.new(year, 3, 31))]
|
138
|
+
when 4..6 then [start_of_date(Date.new(year, 4)), end_of_date(Date.new(year, 6, 30))]
|
139
|
+
when 7..9 then [start_of_date(Date.new(year, 7)), end_of_date(Date.new(year, 9, 30))]
|
140
|
+
when 10..12 then [start_of_date(Date.new(year, 10)), end_of_date(Date.new(year, 12, 31))]
|
141
|
+
else
|
142
|
+
raise ArgumentError.new("invalid date period")
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
78
146
|
def now_with_offset
|
79
147
|
d = DateTime.now
|
80
148
|
DateTime.new(d.year, d.month, d.day, d.hour, d.minute, d.second, offset)
|
@@ -125,6 +193,12 @@ module DatePeriodParser
|
|
125
193
|
]
|
126
194
|
end
|
127
195
|
|
196
|
+
def parse_quarter
|
197
|
+
year, quarter = @value.upcase.split("-Q").map(&:to_i)
|
198
|
+
|
199
|
+
quarter_of(year, (quarter - 1) * 3 + 1)
|
200
|
+
end
|
201
|
+
|
128
202
|
def parse_year(date = nil)
|
129
203
|
if date.nil?
|
130
204
|
year = @value.to_i
|
@@ -8,6 +8,7 @@ describe DatePeriodParser do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
it 'parse' do
|
11
|
+
assert parse(:today) != nil
|
11
12
|
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0000"), parse("2014").first
|
12
13
|
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0000"), parse("2014", nil).first
|
13
14
|
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0000"), parse("2014", {}).first
|
@@ -17,6 +18,13 @@ describe DatePeriodParser do
|
|
17
18
|
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0700"), parse("2014", {'offset' => "+0700"}).first
|
18
19
|
end
|
19
20
|
|
21
|
+
it 'parse with :default' do
|
22
|
+
assert parse(:today) != nil
|
23
|
+
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0000"), parse(nil, default: "2014").first
|
24
|
+
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0000"), parse("", default: "2014").first
|
25
|
+
assert_equal nil, parse("Invalid20", default: "2014")
|
26
|
+
end
|
27
|
+
|
20
28
|
it '2014' do
|
21
29
|
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0000"), parse("2014").first
|
22
30
|
assert_equal DateTime.new(2014,12,31, 23, 59, 59.999, "+0000"), parse("2014").last
|
@@ -58,7 +66,7 @@ describe DatePeriodParser do
|
|
58
66
|
it 'today' do
|
59
67
|
t = Date.today
|
60
68
|
assert_equal DateTime.new(t.year, t.month, t.day, 0, 0, 0.000, "+0000"), parse("today").first
|
61
|
-
assert_equal DateTime.new(t.year, t.month, t.day,23,59,59.999, "+0000"), parse("today").last
|
69
|
+
assert_equal DateTime.new(t.year, t.month, t.day,23,59,59.999, "+0000"), parse("today".upcase).last
|
62
70
|
end
|
63
71
|
|
64
72
|
# https://en.wikipedia.org/wiki/ISO_8601#Week_dates
|
@@ -66,25 +74,30 @@ describe DatePeriodParser do
|
|
66
74
|
end
|
67
75
|
|
68
76
|
it '2015-Q1' do
|
77
|
+
assert_equal DateTime.new(2015, 1, 1, 0, 0, 0.000, "+0000"), parse("2015-q1").first
|
78
|
+
assert_equal DateTime.new(2015, 3,31, 23, 59, 59.999, "+0000"), parse("2015-Q1").last
|
79
|
+
|
80
|
+
assert_equal DateTime.new(2015, 1, 1, 0, 0, 0.000, "-0300"), parse("2015-Q1", offset: "-0300").first
|
81
|
+
assert_equal DateTime.new(2015, 3,31, 23, 59, 59.999, "-0300"), parse("2015-Q1", offset: "-0300").last
|
69
82
|
end
|
70
83
|
|
71
84
|
it 'yesterday' do
|
72
85
|
t = Date.today - 1
|
73
86
|
assert_equal DateTime.new(t.year, t.month, t.day, 0, 0, 0.000, "+0000"), parse("yesterday").first
|
74
|
-
assert_equal DateTime.new(t.year, t.month, t.day,23,59,59.999, "+0000"), parse("yesterday").last
|
87
|
+
assert_equal DateTime.new(t.year, t.month, t.day,23,59,59.999, "+0000"), parse("yesterday".upcase).last
|
75
88
|
end
|
76
89
|
|
77
90
|
it 'yday' do
|
78
91
|
t = Date.today - 1
|
79
92
|
assert_equal DateTime.new(t.year, t.month, t.day, 0, 0, 0.000, "+0000"), parse("yday").first
|
80
|
-
assert_equal DateTime.new(t.year, t.month, t.day,23,59,59.999, "+0000"), parse("yday").last
|
93
|
+
assert_equal DateTime.new(t.year, t.month, t.day,23,59,59.999, "+0000"), parse("yday".upcase).last
|
81
94
|
end
|
82
95
|
|
83
96
|
|
84
97
|
it 'ytd' do
|
85
98
|
t = DateTime.now
|
86
99
|
assert_equal DateTime.new(t.year, 1, 1, 0, 0, 0.000, "+0000"), parse("ytd").first
|
87
|
-
assert_equal DateTime.new(t.year, t.month, t.day, t.hour,t.minute,t.second, "+0000"), parse("ytd").last
|
100
|
+
assert_equal DateTime.new(t.year, t.month, t.day, t.hour,t.minute,t.second, "+0000"), parse("ytd".upcase).last
|
88
101
|
|
89
102
|
assert_equal DateTime.new(t.year, 1, 1, 0, 0, 0.000, "+0400"), parse("ytd", offset: "+0400").first
|
90
103
|
assert_equal DateTime.new(t.year, t.month, t.day, t.hour,t.minute,t.second, "+0400"), parse("ytd", offset: "+0400").last
|
@@ -93,21 +106,66 @@ describe DatePeriodParser do
|
|
93
106
|
it 'mtd' do
|
94
107
|
t = DateTime.now
|
95
108
|
assert_equal DateTime.new(t.year, t.month, 1, 0, 0, 0.000, "+0000"), parse("mtd").first
|
96
|
-
assert_equal DateTime.new(t.year, t.month, t.day, t.hour,t.minute,t.second, "+0000"), parse("mtd").last
|
109
|
+
assert_equal DateTime.new(t.year, t.month, t.day, t.hour,t.minute,t.second, "+0000"), parse("mtd".upcase).last
|
97
110
|
|
98
111
|
assert_equal DateTime.new(t.year, t.month, 1, 0, 0, 0.000, "+0400"), parse("mtd", offset: "+0400").first
|
99
112
|
assert_equal DateTime.new(t.year, t.month, t.day, t.hour,t.minute,t.second, "+0400"), parse("mtd", offset: "+0400").last
|
100
|
-
|
113
|
+
end
|
101
114
|
|
102
115
|
it 'wtd' do
|
103
116
|
end
|
104
117
|
|
118
|
+
it "quarter_of" do
|
119
|
+
# private methods
|
120
|
+
from,to = DatePeriodParser::Base.new("foobar").send(:quarter_of, 2015, 1)
|
121
|
+
assert_equal DateTime.new(2015, 1, 1, 0, 0, 0, "+0000"), from
|
122
|
+
assert_equal DateTime.new(2015, 3,31, 23,59, 59.999, "+0000"), to
|
123
|
+
from,to = DatePeriodParser::Base.new("foobar").send(:quarter_of, 2015, 2)
|
124
|
+
assert_equal DateTime.new(2015, 1, 1, 0, 0, 0, "+0000"), from
|
125
|
+
assert_equal DateTime.new(2015, 3,31, 23,59, 59.999, "+0000"), to
|
126
|
+
from,to = DatePeriodParser::Base.new("foobar").send(:quarter_of, 2015, 3)
|
127
|
+
assert_equal DateTime.new(2015, 1, 1, 0, 0, 0, "+0000"), from
|
128
|
+
assert_equal DateTime.new(2015, 3,31, 23,59, 59.999, "+0000"), to
|
129
|
+
|
130
|
+
from,to = DatePeriodParser::Base.new("foobar").send(:quarter_of, 2015, 4)
|
131
|
+
assert_equal DateTime.new(2015, 4, 1, 0, 0, 0, "+0000"), from
|
132
|
+
assert_equal DateTime.new(2015, 6,30, 23,59, 59.999, "+0000"), to
|
133
|
+
from,to = DatePeriodParser::Base.new("foobar").send(:quarter_of, 2015, 6)
|
134
|
+
assert_equal DateTime.new(2015, 4, 1, 0, 0, 0, "+0000"), from
|
135
|
+
assert_equal DateTime.new(2015, 6,30, 23,59, 59.999, "+0000"), to
|
136
|
+
|
137
|
+
from,to = DatePeriodParser::Base.new("foobar").send(:quarter_of, 2015, 7)
|
138
|
+
assert_equal DateTime.new(2015, 7, 1, 0, 0, 0, "+0000"), from
|
139
|
+
assert_equal DateTime.new(2015, 9,30, 23,59, 59.999, "+0000"), to
|
140
|
+
from,to = DatePeriodParser::Base.new("foobar").send(:quarter_of, 2015, 9)
|
141
|
+
assert_equal DateTime.new(2015, 7, 1, 0, 0, 0, "+0000"), from
|
142
|
+
assert_equal DateTime.new(2015, 9,30, 23,59, 59.999, "+0000"), to
|
143
|
+
|
144
|
+
from,to = DatePeriodParser::Base.new("foobar").send(:quarter_of, 2015, 10)
|
145
|
+
assert_equal DateTime.new(2015, 10, 1, 0, 0, 0, "+0000"), from
|
146
|
+
assert_equal DateTime.new(2015, 12,31, 23,59, 59.999, "+0000"), to
|
147
|
+
from,to = DatePeriodParser::Base.new("foobar").send(:quarter_of, 2015, 12)
|
148
|
+
assert_equal DateTime.new(2015, 10, 1, 0, 0, 0, "+0000"), from
|
149
|
+
assert_equal DateTime.new(2015, 12,31, 23,59, 59.999, "+0000"), to
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'qtd' do
|
153
|
+
t = Date.today
|
154
|
+
from,to = DatePeriodParser::Base.new("foo").send(:quarter_of, t.year, t.month)
|
155
|
+
|
156
|
+
assert_equal DateTime.new(from.year, from.month, 1, 0, 0, 0.000, "+0000"), parse("qtd").first
|
157
|
+
assert_equal DateTime.new(to.year, to.month, to.day, to.hour,to.minute,to.second+0.999, "+0000"), parse("qtd".upcase).last
|
158
|
+
|
159
|
+
assert_equal DateTime.new(from.year, from.month, 1, 0, 0, 0.000, "+0400"), parse("qtd", offset: "+0400").first
|
160
|
+
assert_equal DateTime.new(to.year, to.month, to.day, to.hour,to.minute,to.second+0.999, "+0400"), parse("qtd", offset: "+0400").last
|
161
|
+
end
|
162
|
+
|
105
163
|
it 'current-month' do
|
106
164
|
t = Date.today
|
107
165
|
last = t >> 1 # same day, next month
|
108
166
|
last = Date.new(last.year, last.month, 1) - 1
|
109
167
|
assert_equal DateTime.new(t.year, t.month, 1, 0, 0, 0.000, "+0000"), parse("current-month").first
|
110
|
-
assert_equal DateTime.new(t.year, t.month, last.day,23,59,59.999, "+0000"), parse("current-month").last
|
168
|
+
assert_equal DateTime.new(t.year, t.month, last.day,23,59,59.999, "+0000"), parse("current-month".upcase).last
|
111
169
|
end
|
112
170
|
|
113
171
|
it 'previous-month' do
|
@@ -115,19 +173,19 @@ describe DatePeriodParser do
|
|
115
173
|
last = t >> 1 # same day, next month
|
116
174
|
last = Date.new(last.year, last.month, 1) - 1
|
117
175
|
assert_equal DateTime.new(t.year, t.month, 1, 0, 0, 0.000, "+0000"), parse("previous-month").first
|
118
|
-
assert_equal DateTime.new(t.year, t.month, last.day,23,59,59.999, "+0000"), parse("previous-month").last
|
176
|
+
assert_equal DateTime.new(t.year, t.month, last.day,23,59,59.999, "+0000"), parse("previous-month".upcase).last
|
119
177
|
end
|
120
178
|
|
121
179
|
it 'current-year' do
|
122
180
|
t = Date.today
|
123
181
|
assert_equal DateTime.new(t.year, 1, 1, 0, 0, 0.000, "+0000"), parse("current-year").first
|
124
|
-
assert_equal DateTime.new(t.year, 12, 31,23,59,59.999, "+0000"), parse("current-year").last
|
182
|
+
assert_equal DateTime.new(t.year, 12, 31,23,59,59.999, "+0000"), parse("current-year".upcase).last
|
125
183
|
end
|
126
184
|
|
127
185
|
it 'previous-year' do
|
128
186
|
t = Date.today << 12
|
129
187
|
assert_equal DateTime.new(t.year, 1, 1, 0, 0, 0.000, "+0000"), parse("previous-year").first
|
130
|
-
assert_equal DateTime.new(t.year, 12, 31,23,59,59.999, "+0000"), parse("previous-year").last
|
188
|
+
assert_equal DateTime.new(t.year, 12, 31,23,59,59.999, "+0000"), parse("previous-year".upcase).last
|
131
189
|
end
|
132
190
|
|
133
191
|
describe "with offsets" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: date_period_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hasclass
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -98,3 +98,4 @@ specification_version: 4
|
|
98
98
|
summary: Parse a date-like string and returns it's start and end DateTime.
|
99
99
|
test_files:
|
100
100
|
- spec/date_period_parser_spec.rb
|
101
|
+
has_rdoc:
|