Skip to main content

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

time-helper-0.1.3.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

time_helper-0.1.3-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file time-helper-0.1.3.tar.gz.

File metadata

  • Download URL: time-helper-0.1.3.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for time-helper-0.1.3.tar.gz
Algorithm Hash digest
SHA256 b2415f12cfdf285c73011ce0217db4e3c810c332f9ee719c64edfff350b3ccdd
MD5 4ef790c854eaff674cec4e976c5f584f
BLAKE2b-256 cac720e32d5b91f414f4978820dab3c1c929e28a432a959b234b6f493ff067aa

See more details on using hashes here.

File details

Details for the file time_helper-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: time_helper-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for time_helper-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 709c5ee37fba31a5bbf060ac2159ff52275f4a47eb7a4b0e497b03fe18b01e59
MD5 e5bf0e6a52f59bd1c3fb4ac1a95a068b
BLAKE2b-256 3a627fd17fb27c1df8014ddc6aa3ccdcf5e38dcb6b70ec3ae56801ee84255125

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page