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
6
7
7
-
# Why?
8
+
##Why?
8
9
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 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.
9
10
10
-
# What is date math ?
11
+
##What is date math ?
11
12
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.
> Note, when doing range type searches, and the upper value is inclusive, the rounding will properly be rounded to the ceiling instead of flooring it.
27
28
28
-
# Unit Maps
29
+
##Unit Maps
29
30
```
30
31
y or Y = 'year'
31
32
M = 'month'
@@ -36,11 +37,11 @@ h or H = 'hour'
36
37
s or S = 'second'
37
38
```
38
39
39
-
# Install
40
+
##Install
40
41
```python
41
42
pip install python-datemath
42
43
```
43
-
# Examples
44
+
##Examples
44
45
Assuming our datetime is currently: '2016-01-01T00:00:00-00:00'
45
46
```
46
47
Expression: Result:
@@ -62,7 +63,7 @@ now/d 2016-01-01T23:59:59+00:00
62
63
now/Y 2016-12-31T23:59:59+00:00
63
64
```
64
65
65
-
# Usage
66
+
##Usage
66
67
By default datemath return an arrow date object representing your timestamp.
67
68
68
69
```
@@ -138,11 +139,17 @@ If you want a Epoch timestamp back instead, we can do that.
138
139
1453676321
139
140
```
140
141
141
-
# What timezone are my objects in?
142
-
By default all objects returned by datemath are in UTC. If you want them them in a different timezone, just pass along the ```tz``` argument.
143
-
Timezone list can be found here: https://gist.github.com/pamelafox/986163
142
+
## What timezone are my objects in?
143
+
By default all object returned by datemath are in UTC.
144
+
145
+
If you want them them back in a different timezone, just pass along the ```tz``` argument. Timezone list can be found here: https://gist.github.com/pamelafox/986163
146
+
147
+
If you provide a timezone offset in your timestring, datemath will return your time object as that timezone offset in the string.
148
+
149
+
Note - currently timestrings with a timezone offset and the usage of the ```tz``` argument will result in the time object being returned with the timezone of what was in the timezone offset in the original string
144
150
```
145
-
from datemath import dm
151
+
>>> from datemath import dm
152
+
>>>
146
153
>>> dm('now')
147
154
<Arrow [2016-01-26T01:00:53.601088+00:00]>
148
155
>>>
@@ -154,8 +161,24 @@ from datemath import dm
154
161
>>>
155
162
>>> dm('2017-10-20 09:15:20', tz='US/Pacific')
156
163
<Arrow [2017-10-20T09:15:20.000000-08:00]>
164
+
>>>
165
+
>>> # Timestring with TZ offset in the string (ISO8601 format only)
166
+
>>> dm('2016-01-01T00:00:00-05:00')
167
+
<Arrow [2016-01-01T00:00:00-05:00]>
168
+
>>>
169
+
>>> # Timestring with TZooffset with datemath added (again, TS must be in ISO8601)
170
+
>>> dm('2016-01-01T00:00:00-05:00||+2d+3h+5m')
171
+
<Arrow [2016-01-03T03:05:00-05:00]>
172
+
>>>
173
+
>>> # Note, timestrings with TZ offsets will be returned as the timezone of the offset in the string even if the "tz" option is used.
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