date_period_parser 0.1.1 → 0.1.2
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/.travis.yml +6 -0
- data/README.md +8 -1
- data/Rakefile +6 -3
- data/date_period_parser.gemspec +1 -0
- data/lib/date_period_parser/version.rb +1 -1
- data/spec/date_period_parser_spec.rb +28 -28
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2cc2b66070f88f331001dd3b95385080879170b
|
4
|
+
data.tar.gz: df96c926644370e4b15a5a340cc47e3bcefb50ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba8a5a733a4a683a4f4a3ede9ac33fe97c81350cd69ec5079bc74d36ebc1e5d5457b6cf06528c2ff9ccf6f097e0d46a8ecc4b28580a4b160440d349fb6a8e005
|
7
|
+
data.tar.gz: da0ed0c9deb1bd8290d27ba9582329ad30c1d590a1562f288fe0a11c6b9b474593651abad10781ec29321879ae2fe703537886119d0354a85746f0f6df3d4f74
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+

|
2
|
+
|
1
3
|
# DatePeriodParser
|
2
4
|
|
3
5
|
Parse a date-like string and returns it's start and end DateTime.
|
@@ -5,16 +7,21 @@ Parse a date-like string and returns it's start and end DateTime.
|
|
5
7
|
This can be used to pass date-periods for URL parameters or query strings, e.g. filtering records in a given period.
|
6
8
|
|
7
9
|
```ruby
|
10
|
+
# Example useage in a rails controller
|
11
|
+
|
8
12
|
class PostsController
|
9
13
|
# GET /posts?period=2015-08
|
10
14
|
def index
|
11
15
|
date_range = DatePeriodParser.range(params["period"])
|
16
|
+
date_range ||= DatePeriodParser.range("today") # default
|
12
17
|
@posts = Posts.where(created_at: date_range)
|
13
18
|
end
|
14
19
|
end
|
15
20
|
```
|
16
21
|
|
17
|
-
It is
|
22
|
+
It is **not** a natural language date parser like the [chronic gem](https://github.com/mojombo/chronic).
|
23
|
+
|
24
|
+
Tested with all common Rubies, 1.9.3 .. 2.2, JRuby (1.9 mode). For details check .travis.yml
|
18
25
|
|
19
26
|
## Examples
|
20
27
|
|
data/Rakefile
CHANGED
data/date_period_parser.gemspec
CHANGED
@@ -8,69 +8,69 @@ describe DatePeriodParser do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
it '2014' do
|
11
|
-
assert_equal DateTime.
|
12
|
-
assert_equal DateTime.
|
11
|
+
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0000"), parse("2014").first
|
12
|
+
assert_equal DateTime.new(2014,12,31, 23, 59, 59.999, "+0000"), parse("2014").last
|
13
13
|
end
|
14
14
|
|
15
15
|
it '2014-01' do
|
16
|
-
assert_equal DateTime.
|
17
|
-
assert_equal DateTime.
|
16
|
+
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0000"), parse("2014-01").first
|
17
|
+
assert_equal DateTime.new(2014, 1,31, 23, 59, 59.999, "+0000"), parse("2014-01").last
|
18
18
|
end
|
19
19
|
|
20
20
|
it '2014-02' do
|
21
|
-
assert_equal DateTime.
|
22
|
-
assert_equal DateTime.
|
21
|
+
assert_equal DateTime.new(2014, 2, 1, 0, 0, 0.000, "+0000"), parse("2014-02").first
|
22
|
+
assert_equal DateTime.new(2014, 2,28, 23, 59, 59.999, "+0000"), parse("2014-02").last
|
23
23
|
|
24
|
-
assert_equal DateTime.
|
25
|
-
assert_equal DateTime.
|
24
|
+
assert_equal DateTime.new(2012, 2, 1, 0, 0, 0.000, "+0000"), parse("2012-02").first
|
25
|
+
assert_equal DateTime.new(2012, 2,29, 23, 59, 59.999, "+0000"), parse("2012-02").last
|
26
26
|
end
|
27
27
|
|
28
28
|
it '2014-12' do
|
29
|
-
assert_equal DateTime.
|
30
|
-
assert_equal DateTime.
|
29
|
+
assert_equal DateTime.new(2014,12, 1, 0, 0, 0.000, "+0000"), parse("2014-12").first
|
30
|
+
assert_equal DateTime.new(2014,12,31, 23, 59, 59.999, "+0000"), parse("2014-12").last
|
31
31
|
end
|
32
32
|
|
33
33
|
it '2014-08' do
|
34
|
-
assert_equal DateTime.
|
35
|
-
assert_equal DateTime.
|
34
|
+
assert_equal DateTime.new(2014, 8, 1, 0, 0, 0.000, "+0000"), parse("2014-08").first
|
35
|
+
assert_equal DateTime.new(2014, 8,31, 23, 59, 59.999, "+0000"), parse("2014-08").last
|
36
36
|
end
|
37
37
|
|
38
38
|
it '2014-01-01' do
|
39
|
-
assert_equal DateTime.
|
40
|
-
assert_equal DateTime.
|
39
|
+
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0000"), parse("2014-01-01").first
|
40
|
+
assert_equal DateTime.new(2014, 1, 1, 23, 59, 59.999, "+0000"), parse("2014-01-01").last
|
41
41
|
end
|
42
42
|
|
43
43
|
it '2014-12-31' do
|
44
|
-
assert_equal DateTime.
|
45
|
-
assert_equal DateTime.
|
44
|
+
assert_equal DateTime.new(2014,12,31, 0, 0, 0.000, "+0000"), parse("2014-12-31").first
|
45
|
+
assert_equal DateTime.new(2014,12,31, 23, 59, 59.999, "+0000"), parse("2014-12-31").last
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'today' do
|
49
|
-
|
50
|
-
assert_equal DateTime.
|
51
|
-
assert_equal DateTime.
|
49
|
+
t = Date.today
|
50
|
+
assert_equal DateTime.new(t.year, t.month, t.day, 0, 0, 0.000, "+0000"), parse("today").first
|
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
54
|
describe "with offsets" do
|
55
55
|
it '2014' do
|
56
|
-
assert_equal DateTime.
|
57
|
-
assert_equal DateTime.
|
56
|
+
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0700"), parse("2014", "+0700").first
|
57
|
+
assert_equal DateTime.new(2014,12,31, 23, 59, 59.999, "+0700"), parse("2014", "+0700").last
|
58
58
|
end
|
59
59
|
|
60
60
|
it '2014-01' do
|
61
|
-
assert_equal DateTime.
|
62
|
-
assert_equal DateTime.
|
61
|
+
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0700"), parse("2014-01", "+7").first
|
62
|
+
assert_equal DateTime.new(2014, 1,31, 23, 59, 59.999, "+0700"), parse("2014-01", "+7").last
|
63
63
|
end
|
64
64
|
|
65
65
|
it '2014-01-01' do
|
66
|
-
assert_equal DateTime.
|
67
|
-
assert_equal DateTime.
|
66
|
+
assert_equal DateTime.new(2014, 1, 1, 0, 0, 0.000, "+0700"), parse("2014-01-01", "+7").first
|
67
|
+
assert_equal DateTime.new(2014, 1, 1, 23, 59, 59.999, "+0700"), parse("2014-01-01", "+7").last
|
68
68
|
end
|
69
69
|
|
70
70
|
it 'today' do
|
71
|
-
|
72
|
-
assert_equal DateTime.
|
73
|
-
assert_equal DateTime.
|
71
|
+
t = Date.today
|
72
|
+
assert_equal DateTime.new(t.year, t.month, t.day, 0, 0, 0.000, "+0700"), parse("today", "+7").first
|
73
|
+
assert_equal DateTime.new(t.year, t.month, t.day, 23, 59, 59.999, "+0700"), parse("today", "+7").last
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: date_period_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hasclass
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: Parse a date-like string and returns it's start and end DateTime.
|
42
56
|
email:
|
43
57
|
- sebi.burkhard@gmail.com
|