date_period_parser 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -5
- data/lib/date_period_parser.rb +20 -6
- data/lib/date_period_parser/version.rb +1 -1
- data/spec/date_period_parser_spec.rb +57 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50f01a223cc9dd3cc0aa207a58574c52c1bf8d55
|
4
|
+
data.tar.gz: d377682cd50e84844206f31f71f2d12615e5a1ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b025ae4f53aa7a07d64f7f40d7490e7c88663096af413e8c686a18006ceabeccb8ab4924df79192b00d589974caf02eb55733fc980f0c6e801509cf5d6c62239
|
7
|
+
data.tar.gz: 92d85586e7ee87b2cdec6cf7c0d99e58f2fd323f07a40e9a91dd6f314908d8a7c1a6fed0c4566bafc02d3ef1f8b7121f0f7e9130389510d4f7bba5799a041efe
|
data/README.md
CHANGED
@@ -13,7 +13,7 @@ class PostsController
|
|
13
13
|
# GET /posts?period=2015-08
|
14
14
|
def index
|
15
15
|
date_range = DatePeriodParser.range(params["period"])
|
16
|
-
date_range ||= DatePeriodParser.range("
|
16
|
+
date_range ||= DatePeriodParser.range("current-month") # default
|
17
17
|
@posts = Posts.where(created_at: date_range)
|
18
18
|
end
|
19
19
|
end
|
@@ -66,10 +66,10 @@ Or install it yourself as:
|
|
66
66
|
|
67
67
|
See examples above. Currently supported are:
|
68
68
|
|
69
|
-
* years
|
70
|
-
* months
|
71
|
-
* dates
|
72
|
-
* shorcuts
|
69
|
+
* years `YYYY`
|
70
|
+
* months `YYYY-MM`
|
71
|
+
* dates `YYYY-MM-DD`
|
72
|
+
* shorcuts `today`, `yesterday` and `yday`, `current-month`, `previous-month`, `current-year`, `previous-year`
|
73
73
|
|
74
74
|
It currently requires the year to have 4 digits.
|
75
75
|
|
data/lib/date_period_parser.rb
CHANGED
@@ -58,6 +58,12 @@ module DatePeriodParser
|
|
58
58
|
def parse
|
59
59
|
case @value
|
60
60
|
when /\Atoday\Z/ then parse_date(Date.today)
|
61
|
+
when /\Ayesterday\Z/ then parse_date(Date.today - 1)
|
62
|
+
when /\Ayday\Z/ then parse_date(Date.today - 1)
|
63
|
+
when /\Acurrent-month\Z/ then parse_month(Date.today)
|
64
|
+
when /\Aprevious-month\Z/ then parse_month(Date.today << 1)
|
65
|
+
when /\Acurrent-year\Z/ then parse_year(Date.today)
|
66
|
+
when /\Aprevious-year\Z/ then parse_year(Date.today << 12)
|
61
67
|
when /\A\d\d\d\d\Z/ then parse_year
|
62
68
|
when /\A\d\d\d\d\-\d\d\Z/ then parse_month
|
63
69
|
when /\A\d\d\d\d\-\d\d\-\d\d\Z/ then parse_date
|
@@ -82,20 +88,28 @@ module DatePeriodParser
|
|
82
88
|
]
|
83
89
|
end
|
84
90
|
|
85
|
-
def parse_month
|
86
|
-
|
91
|
+
def parse_month(date = nil)
|
92
|
+
if date.nil?
|
93
|
+
year, month = @value.split("-").map(&:to_i)
|
94
|
+
else
|
95
|
+
year, month = date.year, date.month
|
96
|
+
end
|
87
97
|
|
88
|
-
first = DateTime.new(year
|
98
|
+
first = DateTime.new(year, month, 1, 0, 0, 0, offset)
|
89
99
|
[
|
90
100
|
first,
|
91
101
|
last_date_time_of_month(first)
|
92
102
|
]
|
93
103
|
end
|
94
104
|
|
95
|
-
def parse_year
|
96
|
-
|
105
|
+
def parse_year(date = nil)
|
106
|
+
if date.nil?
|
107
|
+
year = @value.to_i
|
108
|
+
else
|
109
|
+
year = date.year
|
110
|
+
end
|
97
111
|
|
98
|
-
first = DateTime.new(year
|
112
|
+
first = DateTime.new(year, 1,1,0,0,0,offset)
|
99
113
|
[
|
100
114
|
first,
|
101
115
|
last_date_time_of_month(first >> 11)
|
@@ -51,6 +51,61 @@ describe DatePeriodParser do
|
|
51
51
|
assert_equal DateTime.new(t.year, t.month, t.day,23,59,59.999, "+0000"), parse("today").last
|
52
52
|
end
|
53
53
|
|
54
|
+
# https://en.wikipedia.org/wiki/ISO_8601#Week_dates
|
55
|
+
it '2015-W01' do
|
56
|
+
end
|
57
|
+
|
58
|
+
it '2015-Q1' do
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'yesterday' do
|
62
|
+
t = Date.today - 1
|
63
|
+
assert_equal DateTime.new(t.year, t.month, t.day, 0, 0, 0.000, "+0000"), parse("yesterday").first
|
64
|
+
assert_equal DateTime.new(t.year, t.month, t.day,23,59,59.999, "+0000"), parse("yesterday").last
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'yday' do
|
68
|
+
t = Date.today - 1
|
69
|
+
assert_equal DateTime.new(t.year, t.month, t.day, 0, 0, 0.000, "+0000"), parse("yday").first
|
70
|
+
assert_equal DateTime.new(t.year, t.month, t.day,23,59,59.999, "+0000"), parse("yday").last
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
it 'ytd' do
|
75
|
+
end
|
76
|
+
it 'mtd' do
|
77
|
+
end
|
78
|
+
it 'wtd' do
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'current-month' do
|
82
|
+
t = Date.today
|
83
|
+
last = t >> 1 # same day, next month
|
84
|
+
last = Date.new(last.year, last.month, 1) - 1
|
85
|
+
assert_equal DateTime.new(t.year, t.month, 1, 0, 0, 0.000, "+0000"), parse("current-month").first
|
86
|
+
assert_equal DateTime.new(t.year, t.month, last.day,23,59,59.999, "+0000"), parse("current-month").last
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'previous-month' do
|
90
|
+
t = Date.today << 1
|
91
|
+
last = t >> 1 # same day, next month
|
92
|
+
last = Date.new(last.year, last.month, 1) - 1
|
93
|
+
assert_equal DateTime.new(t.year, t.month, 1, 0, 0, 0.000, "+0000"), parse("previous-month").first
|
94
|
+
assert_equal DateTime.new(t.year, t.month, last.day,23,59,59.999, "+0000"), parse("previous-month").last
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'current-year' do
|
98
|
+
t = Date.today
|
99
|
+
assert_equal DateTime.new(t.year, 1, 1, 0, 0, 0.000, "+0000"), parse("current-year").first
|
100
|
+
assert_equal DateTime.new(t.year, 12, 31,23,59,59.999, "+0000"), parse("current-year").last
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'previous-year' do
|
104
|
+
t = Date.today << 12
|
105
|
+
assert_equal DateTime.new(t.year, 1, 1, 0, 0, 0.000, "+0000"), parse("previous-year").first
|
106
|
+
assert_equal DateTime.new(t.year, 12, 31,23,59,59.999, "+0000"), parse("previous-year").last
|
107
|
+
end
|
108
|
+
|
54
109
|
describe "with offsets" do
|
55
110
|
it '2014' do
|
56
111
|
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0700"), parse("2014", "+0700").first
|
@@ -74,6 +129,8 @@ describe DatePeriodParser do
|
|
74
129
|
end
|
75
130
|
end
|
76
131
|
|
132
|
+
|
133
|
+
|
77
134
|
it 'invalid pattern 2014-01-01-01' do
|
78
135
|
from, to = parse("2014-01-01-01")
|
79
136
|
assert_equal nil, from
|