Skip to main content

Utils to work with time

Project description

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.

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

tn-0.0.5.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

tn-0.0.5-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file tn-0.0.5.tar.gz.

File metadata

  • Download URL: tn-0.0.5.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for tn-0.0.5.tar.gz
Algorithm Hash digest
SHA256 001c6d73e937e4e60fe9e6f89b6ed0ba45db19f06ddddef04831aa7cd0471992
MD5 ec9ca59efb8c440b86bfe695ee220ad2
BLAKE2b-256 7bbd054a8b4854128df6a4cab16fd996b4850bc3b286dfd6d46991855bdf6296

See more details on using hashes here.

File details

Details for the file tn-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: tn-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for tn-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d6e0dfccfcbcd6c15c9921150cc6a8e40041bae3ed7158ac88f28121a19cf212
MD5 86b65f78f7c7223a1a87d9d5fa456925
BLAKE2b-256 5466e8a31c5943b7180a82325238ec7bc6db5f67e515dc0fc0d9bab239bd5c9a

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