You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A date math (aka datemath) parser compatiable with the elasticsearch 'date math' format
8
-
9
-
## Why?
10
-
11
-
Working with date objects in python has always been interesting. Having a background in php, I have been looking for quite some time ( no pun intended ) for a way to do date time interpolation similar to php's ```strtotime()``` function. While the arrow module comes close, I needed something that could turn date math type strings into datetime objects for use in [tattle.io](http://tattle.io) and other projects I use in elasticsearch. I have found even more uses for it, including AWS cloudwatch and various other projects and hopefully you will too.
5
+
A date math (aka datemath) parser compatiable with the elasticsearch "date math" format
12
6
13
-
## What is date math?
7
+
## What is "date math"?
14
8
15
-
Date Math is the short hand arithmetic to find relative time to fixed moments in date and time. Similar to the SOLR date math format, Elasticsearch has its own built in format for short hand date math and this module aims to support that same coverage in python.
Date Math is the short hand arithmetic to find relative time to fixed moments in date and time. Similar to the SOLR date math format, we aim to support that same coverage in python.
19
10
20
11
> The date type supports using date math expression when using it in a query/filter (mainly makes sense in range query/filter).
21
12
>
@@ -31,6 +22,7 @@ Documentation from elasticsearch:
31
22
32
23
## Unit Maps
33
24
25
+
The "unit maps" here define the shorthand sytax for the dates/timeframes we are working with:
34
26
```yaml
35
27
y or Y = 'year'
36
28
M = 'month'
@@ -39,17 +31,14 @@ d or D = 'day'
39
31
w = 'week'
40
32
h or H = 'hour'
41
33
s or S = 'second'
42
-
```
43
-
44
-
## Install
45
-
46
-
```python
47
-
pip install python-datemath
34
+
now = <current_time_and_date>
48
35
```
49
36
50
37
## Examples
51
38
52
-
Assuming our datetime is currently: `2016-01-01T00:00:00-00:00`
39
+
Here are some examples of using date math to find dates both in the past and in the future
40
+
41
+
Assuming our "now" datetime is currently: `2016-01-01T00:00:00-00:00`
53
42
54
43
```yaml
55
44
Expression: Result:
@@ -73,7 +62,7 @@ now/Y 2016-12-31T23:59:59+00:00
73
62
74
63
## Usage
75
64
76
-
By default datemath return an arrow date object representing your timestamp.
65
+
If you use the `dm` function in the datemath module, we will return an arrow date object representing your timestamp.
77
66
78
67
```python
79
68
>>>from datemath import dm
@@ -118,8 +107,25 @@ If you would rather have a string, you can use arrow's ```.format()``` method.
118
107
u'2015.12.18'
119
108
```
120
109
121
-
Rather have a python datetime object instead? Just pass along the 'datetime' type
110
+
If you would rather have your time object come back in standard python `datetime`, use the `datemath` function instead:
If you want a Epoch timestamp back instead, we can do that.
138
+
If you want a Epoch timestamp back instead, we can do that too.
147
139
148
140
```python
149
141
>>> dm('now+2d-1m', type='timestamp')
@@ -188,6 +180,12 @@ Note - currently timestrings with a timezone offset and the usage of the ```tz``
188
180
<Arrow [2016-01-01T00:00:00-05:00]>
189
181
```
190
182
183
+
## Install
184
+
185
+
```python
186
+
pip install python-datemath
187
+
```
188
+
191
189
## Debugging
192
190
193
191
If you would like more verbose output to debug the process of what datemath is doing, simply set `export DATEMATH_DEBUG=true` in your shell then run some datemath tests. To stop debugging, run `unset DATEMATH_DEBUG`.
0 commit comments