Elasticsearch datemath and dateformat parsing library. Zero dependencies
Project description
esdateutil
Provides utilities for handling dates like how Elasticsearch does.
In particular:
- Datemath parsing and evaluation (ES Datemath Docs)
- Datetime string format parsing (ES Dateformat Docs)
The goals of this project are:
- Be as close to Elasticsearch behaviour as Python makes sensible.
- No mandatory runtime dependencies.
- Customizability; most functionality should be parameterizable.
Examples
Basic Usage
>>> from datetime import datetime
>>> datetime.now() # now is as below for all examples
datetime.datetime(2024, 9, 24, 8, 36, 17, 503027)
>>> from esdateutil import datemath, dateformat
>>> df = dateformat.DateFormat() # defaults to strict_date_optional_time||epoch_millis
>>> df.parse("2024-09-24T08:36Z") # strict_date_optional_time
datetime.datetime(2024, 9, 24, 08, 36, tzinfo=datetime.timezone.utc)
>>> df.parse("1727163377503") # epoch_millis
datetime.datetime(2024, 9, 24, 8, 36, 17, 503000)
>>> dm = DateMath()
>>> dm.eval("now-5m/h") # now minus 5 minutes rounded to the hour
datetime.datetime(2024, 9, 24, 8, 0)
>>> dm.eval("2024-09-24||-5m/h") # absolute time minus 5 minutes rounded to the hour
datetime.datetime(2024, 9, 23, 23, 0)
Roadmap
This project will be version 1.0 when it provides:
- Robust tests for weird stuff like datemath rounding on DST boundaries in 3.3, 3.5, 3.8+
- Thread safety and tests thereof
See also the TODO file.
Links
https://pypi.org/project/esdateutil/
Building
Requires pyenv and pyenv-virtualenv to be installed on your machine. Requires pyenv-init (pyenv and pyenv-virtualenv) to be run for pyenv local to work w/ virtualenv
Differences from Elasticsearch
One of the consequences of using Python's built-in datetime objects and functions by default is that they can behave very differently from version to version and from Elasticsearch defaults. Below are some of the most important differences in functionality to be aware of.
- The default time resolution in Elasticsearch is milliseconds, whereas in Python datetime it is microseconds. This shouldn't be important unless you are using datemath.UNITS_ROUND_UP_MICROS or another custom round implementation. UNITS_ROUND_UP_MILLIS is provided as an alternative.
- Elasticsearch has optional support for nanosecond precision - because Python datetimes use microsecond precision, we cannot support this completely. This impacts dateformat strict_date_option_time_nanos, which can still be used for microsecond precision instead of millis precision.
- For custom dateformat strings we use strptime as a backup instead of Java's time format strings.
Alternatives
python-datemath
There is another Python project python-datemath for parsing datemath expressions. This projects has different goals to esdateutil, the main difference between them is that python-datemath parses a custom datemath variant, whereas esdateutil.datemath adheres strictly to the Elasticsearch datemath syntax. This means that although the syntax overlaps they will accept and reject different strings.
In most cases, this probably doesn't matter. See the table below for a specific feature difference breakdown.
Difference | esdateutil.datemath | python-datemath |
---|---|---|
Syntax | Accepts and rejects same syntax as Elasticsearch. Unit chars are configurable. | Allows additional uppercase unit chars (Y for year, W for week, D for day, S for second), allows long-form units (e.g. seconds , days ), allows fractional durations (e.g. +1.2d), does not allow missing number (e.g. +y vs +1y), treats expressions without anchors as having now (e.g. +2d is equivalent to now+2d ) |
Date String Support | Accepts the equivalent of strict_date_optional_time||epoch_millis by default. Date parser can be overwritten by a user function. |
Accepts epoch seconds or all formats supported by arrow.get by default. |
Date Types | Uses Python's built-in datetime, timedelta, and timezone types for all date operations. | Uses arrow's Arrow type for all operations. Supports converting to datetime. |
Dependencies | 0 runtime dependencies. 3 build dependencies (pytest, setuptools, wheel). | 4 runtime dependenices, including transitive dependencies: arrow --> python-dateutil --> six + types-python-dateutil. 47 build dependencies. |
Version Support | Supports Python 3.3+ | Supports Python 3.8+ with arrow 1.0.3+. Previous versions support 2.7 and 3.x |
Performance | Processes 1 million datemath strings in 11.2s On My MachineTM. See profiling/ for details and to reproduce. | Processes 1 million datemath strings in 107.1s On My MachineTM. See profiling/ for details and to reproduce. |
Type Hints | No type hints. | Strict type checking with inline types. |
Thread Safety | DateMath objects are not thread safe. This is a planned v1.0 feature. | Types are essentially stateless, thread safety is not a concern. |
Timezones | Returns tz-unaware datetimes by default, unless tzinfo is provided in a date string or given as a timezone object arugment. | Assumes datetimes are UTC by default, unless tz is provided in a date string or given as a string argument. |
Options | https://git.sr.ht/~murr/esdateutil/tree/master/item/esdateutil/datemath.py#L73 | https://github.com/nickmaccarthy/python-datemath/blob/master/datemath/helpers.py#L85 |
Logging | Provides debug logging via the stdlib. Log level is set via the normal Python stdlib log level options. | Debug logging via the stdlib. Log level is set by an environment variable. |
Licence | MIT | Apache 2.0 |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file esdateutil-0.3.2.tar.gz
.
File metadata
- Download URL: esdateutil-0.3.2.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.1.dev0+g94f810c.d20240519 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77151158ff18dc30b444c50ce1328aa5c0977cd76b856de39117a2f8856c474e |
|
MD5 | cdf0bc8e62cd7ec613d63cf4cead9db6 |
|
BLAKE2b-256 | ab4f2975808e255928ee8fef35ad12c595b39a6df00f6fd59209d7eac0853c20 |
File details
Details for the file esdateutil-0.3.2-py3-none-any.whl
.
File metadata
- Download URL: esdateutil-0.3.2-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.1.dev0+g94f810c.d20240519 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ab4589493183eeae2bbb7b965e3596c137d8088885039186f7f9433cd2122ba |
|
MD5 | 139d2c783339795b2b6fd6f3abbdd59f |
|
BLAKE2b-256 | 9ba4b92ba285b1aa8bcbedb00f545690aea3fb9acacae6e25aee187e49549626 |