Convert relative string tokens into datetime objects
Project description
This package allows you to store complex relative dates in string tokens.
To define the initial/starting point in time (typically written as now), to work with dates in the past or in the future. Ideal to perform date simulations.
Time zone configuration. Additionally to the starting point in time, TZs can be provided to as to somehow abstract the user away from localizing datetimes objects in their apps.
As a disclaimer, if the custom datetime specified as the starting point (now’s value) is tz-unaware or naive, it will be treated as an UTC one. Default now’s value also fallsback to datetime.datetime.utcnow, localized to UTC.
Now, if a time zone is specified, the now’s value will be coerced to that TZ prior to applying both snap and modifier expressions. This is handy to quickly resolve tokens given any point in time (either naive or aware), a time zone and the datetoken itself.
Motivation
Have you ever needed to make an application where dates needed to be represented in a relative fashion, like background periodic tasks, datetime range pickers… in a compact and stringified format? This library enables you to persist these string tokens during the lifetime of a process or even longer, since calculations are performed in the moment of evaluation. Theses tokens are also useful when caching URLs as replacement of timestamps, which would break caching given their mutability nature.
Some common examples of relative tokens:
Today: now/d, now
Yesterday: now-d/d, now-d@d
Last 24 hours: now-1d, now. Also writable as: now-24h, now
Last business week: now-w/bw, now-w@bw
This business week: now/bw, now@bw
Last month: now-1M/M, now-1M@M
Last year: now-1Y/Y, now-1Y@Y
Last month first business week: now-M/M+w/bw, now-M/M+w@bw
As you may have noticed, token follow a pattern:
The word now. It means the point in the future timeline when tokens are parsed to their datetime form.
Optionally, modifiers to add and/or subtract the future value of now can be used. Unsurprisingly, additions are set via +, while - mean subtractions. These modifiers can be chained as many times as needed. E.g: now-1M+3d+2h. Along with the arithmetical sign and the amount, the unit of time the amount refers to must be specified. Currently, the supported units are:
s seconds
m minutes
h hours
d days
w weeks
M months
Y years
Optionally, there exist two extra modifiers to snap dates to the start or the end of any given snapshot unit. Those are:
/ Snap the date to the start of the snapshot unit.
@ Snap the date to the end of the snapshot unit.
Snapshot units are the same as arithmetical modifiers, plus the following ones:
bw, business week
mon, Monday
tue, Tuesday
wed, Wednesday
thu, Thursday
fri, Friday
sat, Saturday
sun, Sunday
With this, we achieve a simple way to define canonical relative date ranges, such as Today or Last month. As an example of the later:
String representation: now-1M/M, now-1M@M
Being today 15 Jan 2018, the result range should be: 2018-01-01 00:00:00 / 2018-01-31 23:59:59
Installing
Install and update via either pipenv or pip
pipenv install datetoken
or
pip install datetoken
Examples and usage
Most probably you will be dealing with simple presets such as yesterday or the last 24 hours.
>>> from datetoken.utils import token_to_date
>>> from datetime import datetime
>>> print(datetime.utcnow())
2018-10-18 14:08:47
>>> token_to_date('now-d/d') # Start of yesterday
2018-10-17 00:00:00
>>> token_to_date('now-d@d') # End of yesterday
2018-10-17 23:59:59
For more details, refer to README.
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 datetoken-0.6.0.tar.gz
.
File metadata
- Download URL: datetoken-0.6.0.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d90b9b0ce6965a19e4ac66b5c5971add88fe92879dd3bda77497caa55869c6f |
|
MD5 | d874a379147006e2bc70d785dc8f689c |
|
BLAKE2b-256 | 2322018e521ddf633a30d7641d39858ef75fe6d5afa93890b42f4bc6c40598c7 |
File details
Details for the file datetoken-0.6.0-py2.py3-none-any.whl
.
File metadata
- Download URL: datetoken-0.6.0-py2.py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4537efed1e05daef17935bff903643f1c9b5e86dbc20fde968b5d1cd831e743d |
|
MD5 | b4170880d3f7e1cf2a0424e85789ece3 |
|
BLAKE2b-256 | 183b13077f5afe0c37bdd5ae40e9e737d299c2bca0005421a72046e1b4b5d297 |