Skip to content

Commit 0967666

Browse files
committed
typo fixes, markdown fixes
1 parent 231f57d commit 0967666

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

README.md

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,35 @@
33
# Python Datemath
44

55
## What?
6+
67
A date math (aka datemath) parser compatiable with the elasticsearch 'date math' format
78

89
## Why?
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.
1010

11-
## What is date math ?
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.
12+
13+
## What is date math?
14+
1215
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.
1316

1417
Documentation from elasticsearch:
15-
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-date-format.html#date-math
18+
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-date-format.html#date-math](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-date-format.html#date-math)
1619

1720
> The date type supports using date math expression when using it in a query/filter (mainly makes sense in range query/filter).
18-
21+
>
1922
> The expression starts with an "anchor" date, which can be either now or a date string (in the applicable format) ending with `||`.
20-
23+
>
2124
> It can then follow by a math expression, supporting `+`, `-` and `/` (rounding).
22-
25+
>
2326
> The units supported are `y` (year), `M` (month), `w` (week), `d` (day), `h` (hour), `m` (minute), and `s` (second).
24-
27+
>
2528
> Here are some samples: `now+1h`, `now+1h+1m`, `now+1h/d`, `2012-01-01||+1M/d`.
26-
29+
>
2730
> 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.
2831
2932
## Unit Maps
30-
```
33+
34+
```yaml
3135
y or Y = 'year'
3236
M = 'month'
3337
m = 'minute'
@@ -38,12 +42,16 @@ s or S = 'second'
3842
```
3943

4044
## Install
45+
4146
```python
4247
pip install python-datemath
4348
```
49+
4450
## Examples
45-
Assuming our datetime is currently: '2016-01-01T00:00:00-00:00'
46-
```
51+
52+
Assuming our datetime is currently: `2016-01-01T00:00:00-00:00`
53+
54+
```yaml
4755
Expression: Result:
4856
now-1h 2015-12-31T23:00:00+00:00
4957
now-1y 2015-01-01T00:00:00+00:00
@@ -64,9 +72,10 @@ now/Y 2016-12-31T23:59:59+00:00
6472
```
6573

6674
## Usage
75+
6776
By default datemath return an arrow date object representing your timestamp.
6877

69-
```
78+
```python
7079
>>> from datemath import dm
7180
>>>
7281
>>> dm('now+1h')
@@ -93,7 +102,8 @@ By default datemath return an arrow date object representing your timestamp.
93102

94103
If you would rather have a string, you can use arrow's ```.format()``` method.
95104
> For for info on string formatting, check out arrows tokens section: http://crsmithdev.com/arrow/#tokens
96-
```
105+
106+
```python
97107
>>> from datemath import dm
98108
>>>
99109
>>> src_timestamp = dm('2016-01-01')
@@ -106,11 +116,11 @@ If you would rather have a string, you can use arrow's ```.format()``` method.
106116
>>>
107117
>>> new_timestamp.format('YYYY.MM.DD')
108118
u'2015.12.18'
109-
>>>
110119
```
111120

112121
Rather have a python datetime object instead? Just pass along the 'datetime' type
113-
```
122+
123+
```python
114124
from datemath import dm
115125
>>> dm('now', type='datetime')
116126
datetime.datetime(2016, 1, 22, 22, 58, 28, 338060, tzinfo=tzutc())
@@ -120,7 +130,8 @@ datetime.datetime(2016, 1, 24, 22, 57, 45, 394470, tzinfo=tzutc())
120130
```
121131

122132
Or you can just import the `datemath` module, this will always give us a native `datetime` object
123-
```
133+
134+
```python
124135
>>> from datemath import datemath
125136
>>>
126137
>>> datemath('2016-01-01T16:20:00||/d', roundDown=False)
@@ -130,24 +141,26 @@ datetime.datetime(2016, 1, 1, 23, 59, 59, 999999, tzinfo=tzutc())
130141
>>> # roundDown=True is default and implied
131142
>>> datemath('2016-01-01T16:20:00||/d')
132143
datetime.datetime(2016, 1, 1, 0, 0, tzinfo=tzutc())
133-
>>>
134144
```
135145

136146
If you want a Epoch timestamp back instead, we can do that.
147+
137148
```python
138149
>>> dm('now+2d-1m', type='timestamp')
139150
1453676321
140151
```
141152

142153
## What timezone are my objects in?
154+
143155
By default all object returned by datemath are in UTC.
144156

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
157+
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](https://gist.github.com/pamelafox/986163)
146158

147159
If you provide a timezone offset in your timestring, datemath will return your time object as that timezone offset in the string.
148160

149161
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
150-
```
162+
163+
```python
151164
>>> from datemath import dm
152165
>>>
153166
>>> dm('now')
@@ -166,20 +179,21 @@ Note - currently timestrings with a timezone offset and the usage of the ```tz``
166179
>>> dm('2016-01-01T00:00:00-05:00')
167180
<Arrow [2016-01-01T00:00:00-05:00]>
168181
>>>
169-
>>> # Timestring with TZooffset with datemath added (again, TS must be in ISO8601)
182+
>>> # Timestring with TZ offset with datemath added (again, TS must be in ISO8601)
170183
>>> dm('2016-01-01T00:00:00-05:00||+2d+3h+5m')
171184
<Arrow [2016-01-03T03:05:00-05:00]>
172185
>>>
173186
>>> # Note, timestrings with TZ offsets will be returned as the timezone of the offset in the string even if the "tz" option is used.
174187
>>> dm('2016-01-01T00:00:00-05:00', tz='US/Central')
175188
<Arrow [2016-01-01T00:00:00-05:00]>
176-
>>>
177189
```
178190

179191
## Debugging
192+
180193
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`.
181194

182195
## Changes
196+
183197
See CHANGELOG.md
184198

185199
# Happy date math'ing!

0 commit comments

Comments
 (0)