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.5.tar.gz (15.3 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.5-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for str2td-0.0.5.tar.gz
Algorithm Hash digest
SHA256 13461e9bf02647f092065fb1694e0b82fc3a81f705e5499357e65eaa186eee75
MD5 e2ed2b35c65eff5eb8a3cf545fe94b8b
BLAKE2b-256 24b010cd228ab34f837090e5dda31ac11ab295bd3502ec721915c2b7bf4ee963

See more details on using hashes here.

File details

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

File metadata

  • Download URL: str2td-0.0.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 07c964ab6f42ee2efbecb9bceaf00329adb1b70a9acce24c2ebbc713628853c7
MD5 d4529643b89b07376ef440837ec95d5c
BLAKE2b-256 2e64aa7c361e5bf2ac407ab2dba6bc24bb5765b3cc229064c622c952a8d3d98f

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