date_period_parser 0.2.0 → 0.2.1
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 +29 -11
- data/lib/date_period_parser.rb +28 -2
- data/lib/date_period_parser/version.rb +1 -1
- data/spec/date_period_parser_spec.rb +15 -1
- 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: 133a42526128ad51856ce395e6c78d80b9bf055e
|
4
|
+
data.tar.gz: 275d7a0d7a3c2aa139b531258dba1381236e776e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 588749532d5a8686966fc13e4571ab2813b744c63955ffbfd70d63a39a2c6a2a65f7c6b99ba2c52b12143b83ed4788c610ba72f72bff938723052f2344b03fca
|
7
|
+
data.tar.gz: 77f86b17364e4d176d7d505b64ab7d4ec5157c3a3c49d2b91cfd00d2315b56358d8d260830f71e762518bc55eb4d962dfe3945bb817c4241e846ae9f73310ca9
|
data/README.md
CHANGED
@@ -21,7 +21,16 @@ class PostsController
|
|
21
21
|
end
|
22
22
|
```
|
23
23
|
|
24
|
-
It is **not** a natural language date parser like the [chronic gem](https://github.com/mojombo/chronic).
|
24
|
+
It is **not** a natural language date parser like the [chronic gem](https://github.com/mojombo/chronic). But intends to parse common formats like [ISO_8601](https://en.wikipedia.org/wiki/ISO_8601).
|
25
|
+
|
26
|
+
Examples:
|
27
|
+
|
28
|
+
* years `YYYY`
|
29
|
+
* months `YYYY-MM`
|
30
|
+
* dates `YYYY-MM-DD`
|
31
|
+
* beginning of year/month to now `ytd`, `mtd`
|
32
|
+
* shorcuts `today`, `yesterday` and `yday`, `current-month`, `previous-month`, `current-year`, `previous-year`
|
33
|
+
|
25
34
|
|
26
35
|
Tested with all common Rubies, 1.9.3 .. 2.2, JRuby (1.9 mode). For details check .travis.yml
|
27
36
|
|
@@ -64,16 +73,25 @@ Or install it yourself as:
|
|
64
73
|
|
65
74
|
$ gem install date_period_parser
|
66
75
|
|
67
|
-
##
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
76
|
+
## Recognized patterns
|
77
|
+
|
78
|
+
Values below for today: 2015-07-14 15:33
|
79
|
+
|
80
|
+
| Pattern | From | Until |
|
81
|
+
|--------------------|------------------|------------------|
|
82
|
+
| 2015 | 2015-01-01 00:00 | 2015-12-31 23:59 |
|
83
|
+
| ytd | 2015-01-01 00:00 | 2015-07-14 15:33 |
|
84
|
+
| current-year | 2015-01-01 00:00 | 2015-07-31 23:59 |
|
85
|
+
| previous-year | 2014-01-01 00:00 | 2014-12-31 23:59 |
|
86
|
+
| 2015-07 | 2015-07-01 00:00 | 2015-07-31 23:59 |
|
87
|
+
| mtd | 2015-07-01 00:00 | 2015-07-14 15:33 |
|
88
|
+
| current-month | 2015-07-01 00:00 | 2015-07-31 23:59 |
|
89
|
+
| previous-month | 2015-06-01 00:00 | 2015-06-30 23:59 |
|
90
|
+
| 2015-12-31 | 2015-12-31 00:00 | 2015-12-31 23:59 |
|
91
|
+
| today | 2015-07-14 00:00 | 2015-07-14 23:59 |
|
92
|
+
| yesterday | 2015-07-13 00:00 | 2015-07-13 23:59 |
|
93
|
+
|
94
|
+
Difference between `ytd` and `current-year` (`mtd` and `current-month` respectively) is that `ytd` will return DateTime.now vs. current-month spans from first to last day of the month.
|
77
95
|
|
78
96
|
### DatePeriodParser.parse
|
79
97
|
|
data/lib/date_period_parser.rb
CHANGED
@@ -63,8 +63,10 @@ module DatePeriodParser
|
|
63
63
|
when /\Ayday\Z/ then parse_date(Date.today - 1)
|
64
64
|
when /\Acurrent-month\Z/ then parse_month(Date.today)
|
65
65
|
when /\Aprevious-month\Z/ then parse_month(Date.today << 1)
|
66
|
-
when /\Acurrent-year\Z/
|
67
|
-
when /\Aprevious-year\Z/
|
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 /\Aytd\Z/ then ytd
|
68
70
|
when /\A\d\d\d\d\Z/ then parse_year
|
69
71
|
when /\A\d\d\d\d\-\d\d\Z/ then parse_month
|
70
72
|
when /\A\d\d\d\d\-\d\d\-\d\d\Z/ then parse_date
|
@@ -73,6 +75,26 @@ module DatePeriodParser
|
|
73
75
|
end
|
74
76
|
|
75
77
|
protected
|
78
|
+
def now_with_offset
|
79
|
+
d = DateTime.now
|
80
|
+
DateTime.new(d.year, d.month, d.day, d.hour, d.minute, d.second, offset)
|
81
|
+
end
|
82
|
+
|
83
|
+
def mtd
|
84
|
+
now = now_with_offset
|
85
|
+
[
|
86
|
+
DateTime.new(now.year, now.month, 1, 0, 0, 0, offset),
|
87
|
+
now
|
88
|
+
]
|
89
|
+
end
|
90
|
+
|
91
|
+
def ytd
|
92
|
+
now = now_with_offset
|
93
|
+
[
|
94
|
+
DateTime.new(now.year, 1, 1, 0, 0, 0, offset),
|
95
|
+
now
|
96
|
+
]
|
97
|
+
end
|
76
98
|
|
77
99
|
def parse_date(date = nil)
|
78
100
|
if date.nil?
|
@@ -126,6 +148,10 @@ module DatePeriodParser
|
|
126
148
|
end_of_date(d)
|
127
149
|
end
|
128
150
|
|
151
|
+
def start_of_date(dt)
|
152
|
+
DateTime.new(dt.year, dt.month, dt.day, 0, 0, 0, offset)
|
153
|
+
end
|
154
|
+
|
129
155
|
def end_of_date(dt)
|
130
156
|
DateTime.new(dt.year, dt.month, dt.day, 23, 59, 59.999, offset)
|
131
157
|
end
|
@@ -82,9 +82,23 @@ describe DatePeriodParser do
|
|
82
82
|
|
83
83
|
|
84
84
|
it 'ytd' do
|
85
|
+
t = DateTime.now
|
86
|
+
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
|
88
|
+
|
89
|
+
assert_equal DateTime.new(t.year, 1, 1, 0, 0, 0.000, "+0400"), parse("ytd", offset: "+0400").first
|
90
|
+
assert_equal DateTime.new(t.year, t.month, t.day, t.hour,t.minute,t.second, "+0400"), parse("ytd", offset: "+0400").last
|
85
91
|
end
|
92
|
+
|
86
93
|
it 'mtd' do
|
87
|
-
|
94
|
+
t = DateTime.now
|
95
|
+
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
|
97
|
+
|
98
|
+
assert_equal DateTime.new(t.year, t.month, 1, 0, 0, 0.000, "+0400"), parse("mtd", offset: "+0400").first
|
99
|
+
assert_equal DateTime.new(t.year, t.month, t.day, t.hour,t.minute,t.second, "+0400"), parse("mtd", offset: "+0400").last
|
100
|
+
end
|
101
|
+
|
88
102
|
it 'wtd' do
|
89
103
|
end
|
90
104
|
|