Skip to content

Commit 331117e

Browse files
author
Nick MacCarthy
committed
README update
1 parent 7572d6c commit 331117e

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

README.md

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
[![Build Status](https://travis-ci.org/nickmaccarthy/python-datemath.svg?branch=master)](https://travis-ci.org/nickmaccarthy/python-datemath.svg?branch=master)
2-
31
# Python Datemath
42

53
## What?
64

7-
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
126

13-
## What is date math?
7+
## What is "date math"?
148

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.
16-
17-
Documentation from elasticsearch:
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)
9+
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.
1910

2011
> The date type supports using date math expression when using it in a query/filter (mainly makes sense in range query/filter).
2112
>
@@ -31,6 +22,7 @@ Documentation from elasticsearch:
3122
3223
## Unit Maps
3324

25+
The "unit maps" here define the shorthand sytax for the dates/timeframes we are working with:
3426
```yaml
3527
y or Y = 'year'
3628
M = 'month'
@@ -39,17 +31,14 @@ d or D = 'day'
3931
w = 'week'
4032
h or H = 'hour'
4133
s or S = 'second'
42-
```
43-
44-
## Install
45-
46-
```python
47-
pip install python-datemath
34+
now = <current_time_and_date>
4835
```
4936

5037
## Examples
5138

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`
5342

5443
```yaml
5544
Expression: Result:
@@ -73,7 +62,7 @@ now/Y 2016-12-31T23:59:59+00:00
7362

7463
## Usage
7564

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.
7766

7867
```python
7968
>>> from datemath import dm
@@ -118,8 +107,25 @@ If you would rather have a string, you can use arrow's ```.format()``` method.
118107
u'2015.12.18'
119108
```
120109

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:
111+
112+
```python
113+
>>> from datemath import datemath
114+
>>> ## Assuming "now" is 2016-01-01T00:00:00
115+
>>> datemath("now-1h")
116+
datetime.datetime(2015, 12, 31, 23, 0, tzinfo=tzutc())
117+
# Cast it as a str() get a string of the timestamp back too
118+
>>> str(datemath("now-1h"))
119+
'2015-12-31 23:00:00+00:00'
120+
>>> # roundDown=True is default and implied
121+
>>> datemath('2016-01-01T16:20:00||/d')
122+
datetime.datetime(2016, 1, 1, 0, 0, tzinfo=tzutc())
123+
>>> # Using the roundDown option
124+
>>> datemath('2016-01-01T16:20:00||/d', roundDown=False)
125+
datetime.datetime(2016, 1, 1, 23, 59, 59, 999999, tzinfo=tzutc())
126+
```
122127

128+
Or you can use the `dm` function and set its `type` to `datetime`:
123129
```python
124130
from datemath import dm
125131
>>> dm('now', type='datetime')
@@ -129,21 +135,7 @@ datetime.datetime(2016, 1, 22, 22, 58, 28, 338060, tzinfo=tzutc())
129135
datetime.datetime(2016, 1, 24, 22, 57, 45, 394470, tzinfo=tzutc())
130136
```
131137

132-
Or you can just import the `datemath` module, this will always give us a native `datetime` object
133-
134-
```python
135-
>>> from datemath import datemath
136-
>>>
137-
>>> datemath('2016-01-01T16:20:00||/d', roundDown=False)
138-
datetime.datetime(2016, 1, 1, 23, 59, 59, 999999, tzinfo=tzutc())
139-
>>>
140-
>>>
141-
>>> # roundDown=True is default and implied
142-
>>> datemath('2016-01-01T16:20:00||/d')
143-
datetime.datetime(2016, 1, 1, 0, 0, tzinfo=tzutc())
144-
```
145-
146-
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.
147139

148140
```python
149141
>>> dm('now+2d-1m', type='timestamp')
@@ -188,6 +180,12 @@ Note - currently timestrings with a timezone offset and the usage of the ```tz``
188180
<Arrow [2016-01-01T00:00:00-05:00]>
189181
```
190182

183+
## Install
184+
185+
```python
186+
pip install python-datemath
187+
```
188+
191189
## Debugging
192190

193191
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

Comments
 (0)