Skip to main content

A robust way of dealing with datetimes in python by ensuring all datetimes are timezone aware at runtime.

Project description

Heliclockter

heliclockter is a robust way of dealing with datetimes and timestamps in python. It is statically type checkable as well as runtime enforceable and integrates with pydantic.

The library exposes 3 classes:

  • datetime_tz, a datetime ensured to be timezone-aware.
  • datetime_local, a datetime ensured to be timezone-aware in the local timezone.
  • datetime_utc, a datetime ensured to be timezone-aware in the UTC+0 timezone.

as well as various utilities to instantiate, mutate and serialize those classes.

See our announcement post for a more background on why we wrote heliclockter.

Examples

Say you want to create a timestamp of the current time in the UTC+0 timezone.

from heliclockter import datetime_utc

now = datetime_utc.now()
# datetime_utc(2022, 11, 4, 15, 28, 10, 478176, tzinfo=zoneinfo.ZoneInfo(key='UTC'))

Or imagine you want to create a timestamp 2 hours in the future from now:

from heliclockter import datetime_utc

two_hours_from_now = datetime_utc.future(hours=2)
# datetime_utc(2022, 11, 4, 17, 28, 52, 478176, tzinfo=zoneinfo.ZoneInfo(key='UTC'))

Features

  • Runtime enforcable timezone-aware datetimes
  • Utilities for instantiating, mutating and serializing timezone-aware datetimes
  • Statically type check-ble
  • Pydantic integration
  • Extensive test suite
  • No third party dependencies

Installation

To install heliclockter, simply:

$ pip install heliclockter

More examples

Imagine you want to parse a JSON response from a third party API which includes a timestamp, and you want to handle the timestamp in the UTC+0 timezone regardless of how the 3rd party relays it. This can easily be done with pydantic and heliclockter:

import requests
from pydantic import BaseModel
from heliclockter import datetime_utc


class ApiResponse(BaseModel):
    current_time: datetime_utc


def get_response() -> ApiResponse:
    response = requests.get('https://some-api.com/time')
    return ApiResponse.parse_obj(response.json())

The returned ApiResponse instance is guaranteed to have parsed the current_time attribute as UTC+0 no matter how the api provided the timestamp. If no timezone information is provided, it will be assumed to be UTC+0.

Expanding the module can be done with little effort, by creating a new class that inherits datetime_tz:

from zoneinfo import ZoneInfo
from heliclockter import datetime_tz


class datetime_cet(datetime_tz):
    """
    A `datetime_cet` is a `datetime_tz` but which is guaranteed to be in the 'CET' timezone.
    """

    assumed_timezone_for_timezone_naive_input = ZoneInfo('CET')

If you have a timestamp which is naive, but the timezone in which it is made is known to you, you can easily create a datetime_tz instance using your own defined classes:

aware_dt = datetime_cet.strptime('2022-11-04T15:49:29', '%Y-%m-%dT%H:%M:%S')
# datetime_cet(2022, 11, 4, 15, 49, 29, tzinfo=zoneinfo.ZoneInfo(key='CET'))

About the name

heliclockter is a word play of "clock" and "helicopter". The module aims to guide the user and help them make little to no mistakes when handling datetimes, just like a helicopter parent strictly supervises their children.

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

heliclockter-1.0.2.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

heliclockter-1.0.2-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file heliclockter-1.0.2.tar.gz.

File metadata

  • Download URL: heliclockter-1.0.2.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for heliclockter-1.0.2.tar.gz
Algorithm Hash digest
SHA256 ce41383aed84cee3a7dd8a7fd7dcfef496dff94c97a9e0c040831bc21f9e1220
MD5 8bbaafc294151eb5e75b4eac7b5b1cf7
BLAKE2b-256 691416e51890f4dbc9b0a2ab37bd086f05f949a19793bc79c3d2d05d87dae9ee

See more details on using hashes here.

File details

Details for the file heliclockter-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: heliclockter-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for heliclockter-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d174228e088d88a4f6f6a9929e89a030032cbb4715ca49642154b460a723c2e1
MD5 a07525e17c99a54a80bf61bb16d4b360
BLAKE2b-256 d612ba5d6bffa1067a165f0108c8889a3ff6a629f60733fca0f7c134fb718ec6

See more details on using hashes here.

Supported by

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