Skip to main content

Flexible datetime handling for Python

Project description

FlexDateTime

FlexDateTime is a Python class that provides flexible and enhanced functionality for handling and comparing dates and times using the Arrow library and Pydantic.

Description

The FlexDateTime class allows you to:

  • Parse dates and times from strings with various formats
  • Mask specific components (year, month, day, hour, minute, second) for comparison purposes
  • Serialize and deserialize dates and times with Pydantic V2
  • Easily compare dates and times with masked components

Installation

To use FlexDateTime, you need to install the dependencies: arrow and pydantic.

pip install flexible-datetime

Usage

Creating an Instance

You can create a FlexDateTime instance by providing a date string and an optional input format.

from flexdatetime import FlexDateTime

# Create an instance with the current utc time
current_time = FlexDateTime()

# Create an instance from a date string
date_time = FlexDateTime.from_str("2023-06-28T15:30:00")

# Create an instance from a date string with only year month day
date_time = FlexDateTime.from_str("2023-06-28")

# Create an instance from a date string with only year and moth
date_time = FlexDateTime.from_str("2023-06")

# Create an instance from a date string with only year
date_time = FlexDateTime.from_str("2023")

Masking Components

Masking is automatically determined at initialization, but can also be explicitly set.

Mask specific components of the date/time to exclude them from comparisons.

# Mask the year and month
date_time.apply_mask(year=True, month=True)

Comparing Instances

You can compare FlexDateTime instances while respecting the mask.

date_time1 = FlexDateTime.from_str("2023-01-01T15:30:00")
date_time2 = FlexDateTime.from_str("2024-01-01")
date_time1.apply_mask(day=True, hour=True, second=True)
# Compare the two instances
print(date_time1 == date_time2)  # True, because only the year, month, day have not been masked

String Representation

Get the string representation of the date/time considering the mask.

date_time = FlexDateTime.from_str("2000-01")
print(str(date_time))  # Output: "2000-01"

Example

Here's a complete example demonstrating the usage of FlexDateTime:

from flexdatetime import FlexDateTime

# Create an instance from a date string
date_time1 = FlexDateTime.from_str("2000-01-01")
date_time2 = FlexDateTime.from_str("2024-01-01")

# Mask the year
date_time2.apply_mask(year=True)

# Compare the two instances
print(date_time == another_date_time)  # True, because the year is masked

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

flexible_datetime-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

flexible_datetime-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file flexible_datetime-0.1.0.tar.gz.

File metadata

  • Download URL: flexible_datetime-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.9 Darwin/23.0.0

File hashes

Hashes for flexible_datetime-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b681bd77cfb77ba81c3cef9d83503a94ce1f07ee2e4ec0c0f13d782f65593573
MD5 1af5026d30c567fb154a20691f5939e2
BLAKE2b-256 5d32168009aa5f93aa2664568193d248d88cde162faec7420f4c5f3b9e75d167

See more details on using hashes here.

File details

Details for the file flexible_datetime-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flexible_datetime-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 496a1386d81979bed6ec931e7b41d3c2e9d177f2813f66a5c8fa882e45bfc6e7
MD5 0737ebc464059ed57233a986dd6fce06
BLAKE2b-256 67d088cbeb4f464004828ed497a335c7c98a3be646b3707f9992a8e1b87f19b5

See more details on using hashes here.

Supported by

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