Skip to main content

tn

Utils to work with time

To install: pip install tn

Overview

The tn package provides a collection of utilities to work with time, especially focusing on conversions between different time units and formats, such as milliseconds, seconds, and datetime objects. It includes functions to handle time zones and to convert between UTC and local times. This package is useful for applications that require precise time manipulation and formatting.

Features

  • Conversion between milliseconds, seconds, nanoseconds, and datetime objects.
  • Getting current time in UTC in various units (milliseconds, seconds, nanoseconds).
  • Conversion between UTC and local datetime.
  • Handling of time zones using dateutil.tz.
  • Deprecated functions for backward compatibility, raising warnings to encourage the use of updated functions.

Usage Examples

Getting Current UTC Time

import tn

# Get current UTC time in seconds
current_utc_seconds = tn.utcnow_timestamp()
print("Current UTC time in seconds:", current_utc_seconds)

# Get current UTC time in milliseconds
current_utc_milliseconds = tn.utcnow_ms()
print("Current UTC time in milliseconds:", current_utc_milliseconds)

# Get current UTC time in nanoseconds
current_utc_nanoseconds = tn.utcnow_ns()
print("Current UTC time in nanoseconds:", current_utc_nanoseconds)

Converting UTC Datetime to Milliseconds

from datetime import datetime
import tn

# Current datetime in UTC
current_datetime_utc = datetime.utcnow()

# Convert UTC datetime to milliseconds
utc_milliseconds = tn.utc_datetime_to_utc_ms(current_datetime_utc)
print("Milliseconds since epoch:", utc_milliseconds)

Converting UTC Milliseconds to Local Datetime

import tn

# Example UTC milliseconds
utc_milliseconds = 1609459200000  # Corresponds to 2021-01-01 00:00:00 UTC

# Convert UTC milliseconds to local datetime
local_datetime = tn.utc_ms_to_local_datetime(utc_milliseconds)
print("Local datetime:", local_datetime)

Time Zone Conversion

from datetime import datetime
import tn

# Current time in UTC
utc_now = datetime.utcnow()

# Convert UTC datetime to local time
local_time = tn.utc_to_local(utc_now)
print("Local time:", local_time)

# Convert local time back to UTC
utc_converted = tn.local_to_utc(local_time)
print("UTC time:", utc_converted)

Working with Days

import tn

# Get milliseconds for the start of the day from a given UTC milliseconds
utc_milliseconds = 1609459200000  # Some example UTC milliseconds
day_start_utc_milliseconds = tn.day_utc_ms_from_utc_ms(utc_milliseconds)
print("Day start in UTC milliseconds:", day_start_utc_milliseconds)

# Convert UTC milliseconds to a datetime object representing start of the day
day_start_datetime = tn.day_datetime_from_utc_ms(utc_milliseconds)
print("Day start datetime:", day_start_datetime)

Formatting Seconds into Minutes and Seconds

import tn

# Example seconds
seconds = 3661  # 1 hour, 1 minute, and 1 second

# Convert seconds to mm:ss format
formatted_time = tn.seconds_to_mmss_str(seconds)
print("Formatted time:", formatted_time)

Function/Class Documentation

  • utcnow_timestamp(): Returns the current UTC time as a timestamp in seconds since the Unix epoch.
  • utcnow_ms(): Returns the current UTC time in milliseconds since the Unix epoch.
  • utcnow_ns(): Returns the current UTC time in nanoseconds since the Unix epoch.
  • utc_datetime_to_utc_ms(utc_datetime): Converts a UTC datetime object to milliseconds since the Unix epoch.
  • utc_ms_to_utc_datetime(ums): Converts milliseconds since the Unix epoch to a UTC datetime object.
  • utc_ms_to_local_datetime(ums): Converts milliseconds since the Unix epoch to a local datetime object.
  • utc_to_local(utc_date): Converts a UTC datetime object to local time.
  • local_to_utc(local_date): Converts a local datetime object to UTC.
  • day_utc_ms_from_utc_ms(ums): Calculates the milliseconds at the start of the day for a given UTC time in milliseconds.
  • day_datetime_from_datetime(date_time): Returns a datetime object set to midnight of the day of the provided datetime.
  • day_datetime_from_utc_ms(ums): Converts UTC milliseconds to a datetime object representing the start of the day.
  • seconds_to_mmss_str(s): Formats a number of seconds into a string in mm:ss format.

Deprecated Functions

  • unix_time_ms_to_datetime(ums): Deprecated. Use utc_ms_to_local_datetime instead.
  • datetime_to_unix_time_ms(date): Deprecated. Use utc_datetime_to_utc_ms instead.

This package is maintained by thor, and aims to provide robust tools for handling and manipulating time data efficiently.

Metadata

Release files for tn 0.0.5

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

Source distribution (sdist)

Source distribution for tn 0.0.5
File Size Uploaded Provenance Actions
tn-0.0.5.tar.gz 7.8 kB Without provenance Details for tn-0.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tn 0.0.5
File Interpreter ABI Platform Actions
tn-0.0.5-py3-none-any.whl Python 3 none any Details for tn-0.0.5-py3-none-any.whl

Total release size: 15.8 kB

Release files / tn-0.0.5.tar.gz

Download URL tn-0.0.5.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
001c6d73e937e4e60fe9e6f89b6ed0ba45db19f06ddddef04831aa7cd0471992
BLAKE2b-256 checksum
How to use checksums
7bbd054a8b4854128df6a4cab16fd996b4850bc3b286dfd6d46991855bdf6296
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.13

Release files / tn-0.0.5-py3-none-any.whl

Download URL tn-0.0.5-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d6e0dfccfcbcd6c15c9921150cc6a8e40041bae3ed7158ac88f28121a19cf212
BLAKE2b-256 checksum
How to use checksums
5466e8a31c5943b7180a82325238ec7bc6db5f67e515dc0fc0d9bab239bd5c9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.13

Security for tn 0.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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