Delta (Duration Parsing)

0.4.2 · maintenance · verified Mon Apr 13

Delta is a Python library designed for human-friendly, context-aware parsing of duration strings. It allows users to convert natural language descriptions of time durations (e.g., '1 year 2 months and 3 days') into `datetime.timedelta` objects, optionally considering a specific start date for accurate calculations (e.g., for '2 months' relative to January vs. April). The library is currently at version 0.4.2 and appears to have a low release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `delta.parse()` function to interpret various human-readable duration strings. It shows examples with and without an explicit context date, highlighting how 'context-aware' parsing works for units like months.

import delta
from datetime import datetime

# Parse a duration without a specific context (defaults to current date)
duration_no_context = delta.parse('1 year 2 months and 3 days')
print(f"Duration without context: {duration_no_context}")

# Parse a duration with an explicit context date
context_date = datetime(2023, 1, 15) # January 15, 2023
duration_with_context = delta.parse('1 month and 2 weeks', context_date)
print(f"Duration with context {context_date.strftime('%Y-%m-%d')}: {duration_with_context}")

# Parse different string formats
duration_short_form = delta.parse('2y 3m 4w 5d')
print(f"Duration from short form: {duration_short_form}")

view raw JSON →