Skip to main content

Parse strings into Python datetime objects; extends Python's datetime.strptime() with additional features.

Project description

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, pip install poetry, and run poetry install.

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.

$ python -m strpdatetime "IMG_*_%Y_%m_%d" *.jpg
IMG_2131_2022_11_20.jpg: 2022-11-20 00:00:00
IMG_2132.jpg: time data 'IMG_2132.jpg' does not match format 'IMG_*_%Y_%m_%d'
IMG_2134_2022_11_20.jpg: 2022-11-20 00: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.

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

strpdatetime-0.2.0.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

strpdatetime-0.2.0-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file strpdatetime-0.2.0.tar.gz.

File metadata

  • Download URL: strpdatetime-0.2.0.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for strpdatetime-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5827bd1fe29ad54a2754e29bc178ba24a2b698881475195b1984be49e1fc9600
MD5 5a4213ab6fcb152425b06572185925c3
BLAKE2b-256 cfdf8d73d59b40245f20430dc9bb4f7f8052617eb59929d17858fa7b178dfb61

See more details on using hashes here.

File details

Details for the file strpdatetime-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: strpdatetime-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for strpdatetime-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 914d65fe980cdef3543d3cde529ef4ccc0ce3d23643a070189285d2620961711
MD5 9cc94cd851989696eace995c44133284
BLAKE2b-256 08f6c30cec808d5092d9a1bb09fca7cf698dcc5b8c65e76b6c9f9671b6657b7f

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