|
1 |
| -''' |
2 |
| -A basic utility module for parsing math like strings relating to dates |
3 |
| -
|
4 |
| -This is inspired by Date Math features in elasticsearch and aims to replicate the same functionality for python. |
5 |
| -
|
6 |
| -DateMath (datemath or dm) suppor addition, subtraction and rounding at various granularities of "units" (a map of units to their shorthand is below for reference). |
7 |
| -Expressions can be chanied together and are read left to right. '+' and '-' denote addition and subtraction while '/' denotes 'round', in this case is a 'round down' or floor. |
8 |
| -Round requires a unit (/d), while addition and subtraction require an integer value and a unit (+1d). Whitespace is not allowed in the expression. Absolute datetimes with datemath |
9 |
| -can be made as well, with the datetime and datemath expressions delinated by '||' - example '2015-01-01||+1d' == '2015-01-02' |
10 |
| -
|
11 |
| -
|
12 |
| -Maps: |
13 |
| -
|
14 |
| -y or Y = 'year' |
15 |
| -M = 'month' |
16 |
| -m = 'minute' |
17 |
| -d or D = 'day' |
18 |
| -w = 'week' |
19 |
| -h or H = 'hour' |
20 |
| -s or S = 'second' |
21 |
| -
|
22 |
| -Examples: |
23 |
| -
|
24 |
| -Assuming our datetime is currently: '2016-01-01T00:00:00-00:00' |
25 |
| -
|
26 |
| -Expression: Result: |
27 |
| -now-1h 2015-12-31T23:00:00+00:00 |
28 |
| -now-1y 2015-01-01T00:00:00+00:00 |
29 |
| -now+1y+2d 2017-01-03T00:00:00+00:00 |
30 |
| -now+12h 2016-01-01T12:00:00+00:00 |
31 |
| -now+1d/d 2016-01-03T00:00:00+00:00 |
32 |
| -+2h 2016-01-01T02:00:00+00:00 |
33 |
| -+1h/h 2016-01-01T02:00:00+00:00 |
34 |
| -now+1w/w 2016-01-11T00:00:00+00:00 |
35 |
| -now/d+7d+12h 2016-01-08T12:00:00+00:00 |
36 |
| -2016-01-01||+1d 2016-01-02T00:00:00+00:00 |
37 |
| -2015-01-01||+2w 2015-01-15T00:00:00+00:00 |
38 |
| -
|
39 |
| -''' |
40 |
| - |
41 | 1 | from __future__ import annotations
|
42 | 2 |
|
43 | 3 | import os
|
@@ -250,8 +210,6 @@ def evaluate(expression: str, now: Arrow, timeZone: str = 'UTC', roundDown: bool
|
250 | 210 | if debug: print('\n\n')
|
251 | 211 | return now
|
252 | 212 |
|
253 |
| - |
254 |
| - |
255 | 213 | if __name__ == "__main__":
|
256 | 214 | if debug: print('NOW: {0}'.format(arrow.utcnow()))
|
257 | 215 | if debug: print('\n\n')
|
|
0 commit comments