Various Helper Tools to handle different time data
Project description
Time Helper
Simple helper library to handle different time related tasks in python. This ties into pandas and numpy as well as pytz.
The general idea is to have a bunch of one-stop functions that allow you to easily handle datetime related tasks.
Getting Started
pip install time-helper
Then in python code:
from time_helper import make_aware
make_aware("2022-03-10")
# > datetime.datetime(2022, 3, 10, 0, 0, tzinfo=backports.zoneinfo.ZoneInfo(key='CET'))
Library Logic
The library is build to extend around various datetime objects (such as python internal datetime, date, as well as np.datetime). It provides a bunch of helper functions that are grouped into various categories:
Convert
This gets datetimes in and out of the library. This includes a range of functions for converting strings and different datetime types into canonical py-datetime types:
import time_helper as th
# convert a unix datetime
dt = th.unix_to_datetime(1649491287)
dt = th.any_to_datetime(1649491287)
# convert string to datetime
dt = th.any_to_datetime("2022-03-19")
dt = th.any_to_datetime("2022-03-19 20:15")
# convert a date to datetime
from datetime import date
dt = th.any_to_datetime(date(2022, 3, 10))
It also allows to easily switch between aware and unaware datetime:
dt = th.any_to_datetime("2022-03-10")
aware_dt = th.make_aware(dt)
aware_dt = th.make_aware(dt, "UTC")
unaware_dt = th.make_unaware(dt)
unaware_dt = th.make_unaware(dt, "UTC")
Note that for make_unaware
you can still provide a datetime. In that case the datetime is first converted into the regarding timezone before the
timezone information is removed from the object. You can also explicitly convert the timezone with localize_datetime
.
Operations & Ranges
The library also defines a range of operations to make working with timezones easier. These include handling modifications of a single datetime:
day = th.round_time(dt, "D")
# results in: datetime.datetime(2022, 3, 10, 0, 0)
day = th.round_time(dt, "D", max_out=True)
# results in: datetime.datetime(2022, 3, 10, 23, 59, 59, 999999)
has_tz = th.has_timezone(aware_dt)
has_tz = th.has_timezone(unaware_dt)
# compute a diff (between aware and unaware datetimes)
diff = th.time_diff(
th.round_time(aware_dt, "M", max_out=True),
unaware_dt
)
# results in: datetime.timedelta(seconds=3659, microseconds=999999)
It also supports a bunch of range operations:
# converts the time into a interval (float) value for the defined range
# default i
dt = datetime(2022, 3, 12, 12, 0)
pos = th.time_to_interval(dt, offset=0)
# results in: 0.0 (as it is noon)
pos = th.time_to_interval(dt, offset=0, zero_center=False)
# results in: 0.5 (as half day is gone and center is no longer zeroed)
# create interval tuples
ivs = th.create_intervals(dt, dt + timedelta(days=2), interval=1)
ivs = th.create_intervals(dt, dt + timedelta(days=2), interval=timedelta(minutes=30))
Wrapper
This library also provides a wrapper class to make all functions more accessible and first class citizens of the system.
Note: This part of the library is still under construction.
Notes
There is still a lot to improve on this library, please feel free to create PRs or contact me if you wish to contribute!
Project details
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 time-helper-0.1.5.tar.gz
.
File metadata
- Download URL: time-helper-0.1.5.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d93b6e0ff54e8c854f37b71c9a7b15f5dc5d6852068b85b29a19d0b9edb98b4 |
|
MD5 | 36d095192791a82a0793ac338cb76795 |
|
BLAKE2b-256 | b7ad0a3e54aab73114912c5817d12cd838f9be94c4e23d5f7bd83c83eb947601 |
File details
Details for the file time_helper-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: time_helper-0.1.5-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce29d5048e403493ea42728d863b32b9570d460b1b50cdabb6280664578601fd |
|
MD5 | 3bf8606a5e3a85333a90e2414a331bd3 |
|
BLAKE2b-256 | 36ad4bb4b971bab3729fe8ac8dcdab65e8ca134c959331bebae8dda9cc228218 |