Skip to main content

strpdatetime

A replacement for datetime.datetime.strptime with super powers. strpdatetime is a drop-in replacement for datetime.datetime.strptime that adds a simplified regex-like syntax for finding and extracting date and time information from strings.

Why this package?

A common use case is parsing date/time information from strings, for example filenames with the date and time embedded in them. The standard library's datetime.datetime.strptime works well if the string perfectly matches the format string, but does not work if the string contains additional characters. For example, datetime.datetime.strptime("IMG_1234_2022_11_20.jpeg", "%Y_%m_%d") will fail with ValueError: time data 'IMG_1234_2022_11_20.jpeg' does not match format '%Y_%m_%d'. To use datetime.datetime.strptime in this case, you would need to first parse the string to remove the extra characters.

Third-party packages such as dateutil and datefinder are more flexible but still fail to find the date in the above example and other common filename date/time formats.

strpdatetime can find the date in the above example using strpdatetime("IMG_1234_2022_11_20.jpeg", "^IMG_*_%Y_%m_%d")

Installation

pip install strpdatetime

To install from source:

  • Clone the repository
  • Change to the project directory
  • Install uv: curl -LsSf https://astral.sh/uv/install.sh | sh
  • Create the virtual environment: uv venv or uv venv --python 3.13 to specify a specific version
  • Activate the virtual environment: source .venv/bin/activate
  • Install package dependencies: uv pip install -r pyproject.toml --extra dev

Source Code

The source code is available on GitHub.

Usage

>>> import datetime
>>> from strpdatetime import strpdatetime
>>> dt = strpdatetime("IMG_1234_2022_11_20.jpeg","^IMG_*_%Y_%m_%d.*")
>>> assert dt == datetime.datetime(2022,11,20)
>>>

Syntax

In addition to the standard strptime format codes, strpdatetime supports the following:

  • *: Match any number of characters
  • ^: Match the beginning of the string
  • $: Match the end of the string
  • {n}: Match exactly n characters
  • {n,}: Match at least n characters
  • {n,m}: Match at least n characters and at most m characters
  • In addition to %% for a literal %, the following format codes are supported: %^, %$, %*, %|, %{, %} for ^, $, *, |, {, } respectively
  • |: join multiple format codes; each code is tried in order until one matches
  • Unlike the standard library, the leading zero is not optional for %d, %m, %H, %I, %M, %S, %j, %U, %W, and %V
  • For optional leading zero, use %-d, %-m, %-H, %-I, %-M, %-S, %-j, %-U, %-W, and %-V

Examples

>>> from strpdatetime import strpdatetime
>>> strpdatetime("IMG_1234_2022_11_20.jpg","^IMG_{4}_%Y_%m_%d")
datetime.datetime(2022, 11, 20, 0, 0)
>>> strpdatetime("IMG_1234_2022_11_20.jpg","IMG_*_%Y_%m_%d")
datetime.datetime(2022, 11, 20, 0, 0)
>>> strpdatetime("1234_05_06_2022_11_20","%Y_%m_%d$")
datetime.datetime(2022, 11, 20, 0, 0)
>>> strpdatetime("1234_05_06_2022_11_20","IMG_*_%Y_%m_%d|%Y_%m_%d$")
datetime.datetime(2022, 11, 20, 0, 0)
>>>

Command Line

strpdatetime includes a very simple command line interface. It can be used to test the regex-like syntax. Use python -m strpdatetime --help for more information.

python -m strpdatetime "^IMG_*_%Y_%m_%d|IMG{4}_%Y-%m-%d-%H%M%S" *.jpg
IMG1234_2022-11-20-063400.jpg, 2022-11-20T06:34:00
IMG_1234_2022_11_21.jpg, 2022-11-21T00:00:00
time data 'IMG_2132.jpg' does not match format '^IMG_*_%Y_%m_%d|IMG{4}_%Y-%m-%d-%H%M%S'
IMG_2132.jpg,
IMG_5678_2022_11_20.jpg, 2022-11-20T00:00:00

License

To ensure backwards compatibility with the Python standard library, strpdatetime makes use of original code from the standard library and is thus licensed under the Python Software Foundation License, just as Python itself is.

Contributing

Contributions of all kinds are welcome! Please open an issue or pull request on GitHub.

Metadata

Release files for strpdatetime 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for strpdatetime 0.4.1
File Size Uploaded Provenance
strpdatetime-0.4.1.tar.gz 27.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for strpdatetime 0.4.1
File Interpreter ABI Platform
strpdatetime-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 44.9 kB

Release files / strpdatetime-0.4.1.tar.gz

Download URL strpdatetime-0.4.1.tar.gz
Size 27.2 kB
Tags Source
SHA-256 checksum
How to use checksums
095e8a8d21ea9de65fe8847bf892eef93bfa57dd3d675165fd67d22da39542b9
BLAKE2b-256 checksum
How to use checksums
9793901f61b8b0f615d9ac9fe57f2c7f2e23e0f58d5f2d95c1afa13e9d2b3940
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.2

Release files / strpdatetime-0.4.1-py3-none-any.whl

Download URL strpdatetime-0.4.1-py3-none-any.whl
Size 17.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
29992659517aadff685a7989635434a9b2757447d4205580ae0d277570d76d15
BLAKE2b-256 checksum
How to use checksums
57fa1db733414923b6ca4807a7e07f71745f44d9d9c078e16eb6c369b535673e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.2

Security for strpdatetime 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page