Skip to main content

Utils to work with dates

Project description

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.

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

ky-0.0.5.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

ky-0.0.5-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ky-0.0.5.tar.gz
Algorithm Hash digest
SHA256 2f09f76b99f9b35b6aa266fc4a266e2762b73a2a568b8a5a951062430fa87228
MD5 1903cd36baffaba4fca6a9247a3358a5
BLAKE2b-256 0b420afbe2300ff4de689cfadcd5f57bc7bf5cfa2d1c8e1f0dfcb9596d8e3acc

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ky-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9ca9fc411a72df17509a70f3762b300416b67bc4e74bdd23243f2dd0fe98c3d9
MD5 221126a64cd3328679abeae17bd22ac7
BLAKE2b-256 3065d15abce8690a59a4875bc86ba931c068193c53f864d2025dc8c0a37ed254

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