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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9568f1e71186ac31a20d9a846213116cc8f84878
4
- data.tar.gz: 1bb0d2e0e60544311cd7f5e0f7358153edfd1ce1
3
+ metadata.gz: 133a42526128ad51856ce395e6c78d80b9bf055e
4
+ data.tar.gz: 275d7a0d7a3c2aa139b531258dba1381236e776e
5
5
  SHA512:
6
- metadata.gz: 8adf231887572db922d0ae3f562bc97388944a6c84a6473e225f987e81c275d71570c091fb2cc46bb135faa6c0b9ce010915a11c18533fc6f9550f5c4452d3b9
7
- data.tar.gz: 2a8fdd8fba6e101de2f0c859411eac3a457d275e91856b2e3c2d049d31ece2cd0e94aa29c2f26d00f3a84993ed353d9a9170fa463093f470079880d71fbd0b95
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
- ## Usage
68
-
69
- See examples above. Currently supported are:
70
-
71
- * years `YYYY`
72
- * months `YYYY-MM`
73
- * dates `YYYY-MM-DD`
74
- * shorcuts `today`, `yesterday` and `yday`, `current-month`, `previous-month`, `current-year`, `previous-year`
75
-
76
- It currently requires the year to have 4 digits.
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
 
@@ -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/ then parse_year(Date.today)
67
- when /\Aprevious-year\Z/ then parse_year(Date.today << 12)
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
@@ -1,3 +1,3 @@
1
1
  module DatePeriodParser
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  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
- end
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
 
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - hasclass