Skip to main content

ky

Utils to work with dates

To install: pip install ky

Overview

The ky package provides a collection of utilities for handling and manipulating dates and times in Python. It includes functions to convert times between timezones, extract specific time information from datetime objects, iterate over ranges of dates, and more. Below are detailed descriptions and examples of how to use each function.

Functions

function_to_convert_to_timezone_from_timezone(to_timezone, from_timezone='UTC')

Converts a datetime object from one timezone to another. By default, it converts from UTC to the specified timezone.

Parameters:

  • to_timezone (str or pytz.timezone): The timezone to convert to.
  • from_timezone (str or pytz.timezone, optional): The timezone to convert from. Defaults to 'UTC'.

Returns:

  • A function that takes a datetime object as input and returns it converted to the specified timezone.

Example:

from datetime import datetime
from ky import function_to_convert_to_timezone_from_timezone

convert_to_EST = function_to_convert_to_timezone_from_timezone('America/New_York')
utc_time = datetime.utcnow()
est_time = convert_to_EST(utc_time)
print(f"UTC Time: {utc_time}, EST Time: {est_time}")

mk_time_info_extractor(spec)

Creates a function that extracts specified components from a datetime object.

Parameters:

  • spec (list or dict): Specification of the components to extract.

Returns:

  • A function that takes a datetime object and returns a dictionary with the specified components.

Example:

from datetime import datetime
from ky import mk_time_info_extractor

extractor = mk_time_info_extractor(['tm_year', 'tm_mon', 'tm_mday'])
now = datetime.now()
print(extractor(now))  # Output might be {'tm_year': 2023, 'tm_mon': 12, 'tm_mday': 25}

day_of_week_integer_to_string(day_of_week_integer)

Converts an integer representing a day of the week to its corresponding string name.

Parameters:

  • day_of_week_integer (int): Integer representing the day of the week (0=Sunday, 6=Saturday).

Returns:

  • String representing the day of the week.

Example:

from ky import day_of_week_integer_to_string

print(day_of_week_integer_to_string(1))  # Output: 'Mon'

daterange(start_date, end_date)

Generates dates from start_date to one day before end_date.

Parameters:

  • start_date (datetime.date): Start date.
  • end_date (datetime.date): End date.

Returns:

  • A generator yielding dates from start to end.

Example:

from datetime import date
from ky import daterange

start = date(2023, 1, 1)
end = date(2023, 1, 5)
for single_date in daterange(start, end):
    print(single_date)

datetimes_ranges_defining_months(from_date, to_date)

Generates datetime ranges that define the start and end of each month between from_date and to_date.

Parameters:

  • from_date (datetime.datetime): Starting datetime.
  • to_date (datetime.datetime): Ending datetime.

Returns:

  • A generator yielding tuples (start of month, end of month) for each month in the range.

Example:

from datetime import datetime
from ky import datetimes_ranges_defining_months

start = datetime(2023, 1, 1)
end = datetime(2023, 4, 1)
for month_start, month_end in datetimes_ranges_defining_months(start, end):
    print(f"Start: {month_start}, End: {month_end}")

This package is designed to simplify common date and time manipulations, making it easier to handle timezones, extract date components, and iterate over date ranges.

Metadata

Release files for ky 0.0.6

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

Source distribution (sdist)

Source distribution for ky 0.0.6
File Size Uploaded
ky-0.0.6.tar.gz 8.7 kB Details

Built distribution (wheel)

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

Total release size: 17.1 kB

Release files / ky-0.0.6.tar.gz

Download URL ky-0.0.6.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e25387df89c794537e8e016229a86eca3a085f90df170338dc293487e04b4306
BLAKE2b-256 checksum
How to use checksums
e89861399ea9837ca985eb2059d208a94cd925f8e5cb00ebf8dc7c5d37c08598
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / ky-0.0.6-py3-none-any.whl

Download URL ky-0.0.6-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
35cfa29268bbc525e671eba40299073e7725c8fccff199297beb57ec6c46351d
BLAKE2b-256 checksum
How to use checksums
cd3cf3d0f582713f5daf2f5690e09f686288af3fbb16051d0b4c60cc2aca583e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

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