Skip to main content

Easily convert from short, human readable, and easy to input strings to timedeltas and datetimes

Project description

str2td

Easily express a timedelta in a succinct and human-readable way.

Usage example

from datetime import datetime
from lark import LarkError
from str2td import str2td

now = datetime.now().astimezone()
tz = now.tzinfo  # can be different if the string's timezone is different than your calculation timezone.

print(f"{now=:%Y-%m-%d %H:%M:%S (%Z)}\n")

try:
	string = input("Set a reminder in: ")
	td = str2td(string, now=now, tz=tz)
	print(f"Setting reminder at {now + td:%Y-%m-%d %H:%M:%S} ({td} from now).\n")

	string = input("When's your birthday? ")
	td = str2td(string, now=now, tz=tz)
	print(f"Great! Your nearest birthday is at: {now + td:%Y-%m-%d %H:%M:%S}, in {td.days} days.\n")

	string = input("[Seconds calculator]: ")
	td = str2td(string, now=now, tz=tz)
	print(f"{string} = {td.total_seconds()} seconds")
except LarkError as e:
	print("\nInvalid Syntax!")
	print("=" * 50)
	print(e)
	print("=" * 50)
Successful inputs
now=2025-05-13 21:01:41 (Central European Summer Time)

Set a reminder in: 3d!10:
Setting reminder at 2025-05-17 10:00:00 (3 days, 12:58:18.493712 from now).

When's your birthday? 10-10
Great! Your nearest birthday is at: 2025-10-10 21:01:41, in 150 days.

[Seconds calculator]: 3d-.25d5.5h1s
3d-.25d5.5h1s = 257401.0 seconds.
Erroring inputs
now=2025-05-13 21:12:22 (Central European Summer Time)

Set a reminder in: 1huj
Invalid Syntax!
Error trying to process rule "robostr_segment":

Unknown unit: 'huj'

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

str2td-0.0.4.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

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

str2td-0.0.4-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file str2td-0.0.4.tar.gz.

File metadata

  • Download URL: str2td-0.0.4.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for str2td-0.0.4.tar.gz
Algorithm Hash digest
SHA256 75757fa96a6e11f04ef0bdd1489e0b9bbe6cb7ef96933e48b4e72c08b150d83d
MD5 d98422f52a074bee93663496d0446df1
BLAKE2b-256 fba9c961daab42d291f819bb75a37cc5144831544e5633e99f72574df56a049b

See more details on using hashes here.

File details

Details for the file str2td-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: str2td-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for str2td-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 381cd9d636a9a3d0ca3d458e8a4d4cb3edc4b4fab305f7975265bed9f08544ec
MD5 9688b8ff182bc2a725055b21bdf25985
BLAKE2b-256 52464f5a5a8ff7b2c240337274e631e805b8dbc0893f171875494636f0a18aec

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