datemath 0.2.0 → 0.3.0
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/Gemfile.lock +1 -1
- data/README.md +6 -6
- data/lib/datemath/parser.rb +12 -9
- data/lib/datemath/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 23cef9f2a120a8bdb84d14e43802d3fb6c8d73f02426159bfc8f774a85c2f48e
|
4
|
+
data.tar.gz: 4a9952a60d8c4fc80d5f9e181e5ce6ac5dc8ed3666b41b4afde2593608c3712b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9babe3938ad79aacc5a241d11668f694b58abe033d56b8660282640df4e4ce36497259f5008a9963fc9c4ce6e343fbd9cd40259ef75e47ec844857100ff729f
|
7
|
+
data.tar.gz: '04488afd1884c1dccebd1ec914b3b92e2a55c8c35c6e01e020a8e96fe7116ecf53af2715d21290722d6ea07788897e9555441c8c82d2f38f70632f95e2cb63c6'
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -27,37 +27,37 @@ Here are a few examples:
|
|
27
27
|
Current date time:
|
28
28
|
|
29
29
|
```ruby
|
30
|
-
Datemath::Parser.new
|
30
|
+
Datemath::Parser.new("now").parse
|
31
31
|
```
|
32
32
|
|
33
33
|
Specific date time:
|
34
34
|
|
35
35
|
```ruby
|
36
|
-
Datemath::Parser.new
|
36
|
+
Datemath::Parser.new("2015-05-05T00:00:00").parse
|
37
37
|
```
|
38
38
|
|
39
39
|
Complex expression:
|
40
40
|
|
41
41
|
```ruby
|
42
|
-
Datemath::Parser.new
|
42
|
+
Datemath::Parser.new("now+1d").parse
|
43
43
|
```
|
44
44
|
|
45
45
|
Multiple operations:
|
46
46
|
|
47
47
|
```ruby
|
48
|
-
Datemath::Parser.new
|
48
|
+
Datemath::Parser.new("now+1d-1m").parse
|
49
49
|
```
|
50
50
|
|
51
51
|
Rounding:
|
52
52
|
|
53
53
|
```ruby
|
54
|
-
Datemath::Parser.new
|
54
|
+
Datemath::Parser.new("now+1d-1m/d").parse
|
55
55
|
```
|
56
56
|
|
57
57
|
Anchoring dates:
|
58
58
|
|
59
59
|
```ruby
|
60
|
-
Datemath::Parser.new
|
60
|
+
Datemath::Parser.new("2015-05-05T00:00:00||+1d-1m").parse
|
61
61
|
```
|
62
62
|
|
63
63
|
You can see more cases here: [here](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/date-math-expressions.html)
|
data/lib/datemath/parser.rb
CHANGED
@@ -3,7 +3,8 @@ module Datemath
|
|
3
3
|
|
4
4
|
# Initialize
|
5
5
|
#
|
6
|
-
def initialize
|
6
|
+
def initialize(text)
|
7
|
+
@text = text
|
7
8
|
@units = ['y', 'M', 'w', 'd', 'h', 'm', 's', 'ms']
|
8
9
|
end
|
9
10
|
|
@@ -12,25 +13,25 @@ module Datemath
|
|
12
13
|
# @param [String] text
|
13
14
|
# @param [Boolean] round_up
|
14
15
|
# @return [DateTime]
|
15
|
-
def parse(
|
16
|
-
return nil unless text
|
16
|
+
def parse(round_up = false)
|
17
|
+
return nil unless @text
|
17
18
|
|
18
19
|
time = nil
|
19
20
|
math_string = ''
|
20
21
|
index = nil
|
21
22
|
parse_string = nil
|
22
23
|
|
23
|
-
if (text[0, 3] == 'now')
|
24
|
+
if (@text[0, 3] == 'now')
|
24
25
|
time = DateTime.now
|
25
|
-
math_string = text['now'.length
|
26
|
+
math_string = @text['now'.length..@text.length]
|
26
27
|
else
|
27
|
-
index = text.index('||')
|
28
|
+
index = @text.index('||')
|
28
29
|
if index.nil?
|
29
|
-
parse_string = text
|
30
|
+
parse_string = @text
|
30
31
|
math_string = ''
|
31
32
|
else
|
32
|
-
parse_string = text[0, index]
|
33
|
-
math_string = text[(index + 2)
|
33
|
+
parse_string = @text[0, index]
|
34
|
+
math_string = @text[(index + 2)..@text.length]
|
34
35
|
end
|
35
36
|
time = DateTime.parse(parse_string) rescue nil
|
36
37
|
end
|
@@ -39,6 +40,8 @@ module Datemath
|
|
39
40
|
parse_date_math(math_string, time, round_up)
|
40
41
|
end
|
41
42
|
|
43
|
+
private
|
44
|
+
|
42
45
|
# Handles math_string to manipulate a given datetime
|
43
46
|
#
|
44
47
|
# @param [String] math_string
|
data/lib/datemath/version.rb
CHANGED