Skip to main content

Shorthand datetime

Simple package to parse shorthand datetime strings in the Elasticsearch date math format.

Using the package

# Simplest usage example
from shorthand_datetime.shorthand import parse_shorthand_datetime

string = 'now-7d'
dt = parse_shorthand_datetime(string)
print(f'{string} -> {dt}')
# Suppose the current date is 2023-10-25 12:21:45+00:00
# Output: now-7d -> 2023-10-18 12:21:45+00:00
# Example with freeze_time for testings
from freezegun import freeze_time
from shorthand_datetime import parse_shorthand_datetime

@freeze_time("2023-10-25 12:21:45+00:00")
def test_now():
    strings = ['now-7d', 'now/d', 'now-7d/d', 'now/M', 'now-1M/M',
               'now-3W+6h', 'now-1M+1W/d']
    for string in strings:
        print(f'{string} -> {parse_shorthand_datetime(string)}')

# Call the test and print the results
test_now()

Output:

now-7d -> 2023-10-18 12:21:45+00:00
now/d -> 2023-10-25 00:00:00+00:00
now-7d/d -> 2023-10-18 00:00:00+00:00
now/M -> 2023-10-01 00:00:00+00:00
now-1M/M -> 2023-09-01 00:00:00+00:00
now-3W+6h -> 2023-10-04 18:21:45+00:00
now-1M+1W/d -> 2023-10-02 00:00:00+00:00
# Example with timezone
from shorthand_datetime import parse_shorthand_datetime

# Using the timezone as a parameter
string_1 = 'now-7d'
dt_1 = parse_shorthand_datetime(string_1, tz='Europe/Brussels')

# Using the timezone as part of the string
string_2 = 'now-7d"Europe/Brussels"'
dt_2 = parse_shorthand_datetime(string_2)

assert str(dt_1.tzinfo) == str(dt_2.tzinfo) == "Europe/Brussels"

Typical examples

By default, datetime strings are parsed in UTC. However, you can specify a timezone using the tz parameter or by including it in the string.

  • now-7d: current timestamp minus 7 days

  • now/d: today, rounded to the start of the day

  • now-7d/d: 7 days ago, rounded to the start of the day

  • now/M: first day of this month

  • now-1M/M: first day of previous month

  • now-3W+6h: 3 weeks ago plus 6 hours

  • now-1M+1W/d: 1 month ago plus 1 week, rounded to the start of the day

  • now'America/New_York': current timestamp in America/New_York

  • now-7d'America/New_York': 7 days ago in America/New_York

Acknowledgements

© 2024 24SEA - Monitoring Offshore Structures. All rights reserved.

Metadata

Release files for shorthand-datetime 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shorthand-datetime 0.4.0
File Size Uploaded
shorthand_datetime-0.4.0.tar.gz 8.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for shorthand-datetime 0.4.0
File Interpreter ABI Platform
shorthand_datetime-0.4.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 15.4 kB

Release files / shorthand_datetime-0.4.0.tar.gz

Download URL shorthand_datetime-0.4.0.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
00e3070c5480074eb08eb559524f8e5308e810812b502db19f620052fd1d941a
BLAKE2b-256 checksum
How to use checksums
a40118d958d086c477fbda290989f44aef8d7cf9696fa025f5e6cac5af5efaf1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.8.18

Release files / shorthand_datetime-0.4.0-py2.py3-none-any.whl

Download URL shorthand_datetime-0.4.0-py2.py3-none-any.whl
Size 7.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
51ce402de3d7d4997c0a67e59cb392157f213802b267014b76cb3abd4a4f4cca
BLAKE2b-256 checksum
How to use checksums
42e010d5ab59471e80ba8dad8884887246460528f84695e29c00ad86d431c109
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.8.18

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page