Name | strpdatetime JSON |
Version |
0.3.0
JSON |
| download |
home_page | |
Summary | Parse strings into Python datetime objects; extends Python's datetime.strptime() with additional features. |
upload_time | 2023-11-13 15:45:50 |
maintainer | |
docs_url | None |
author | Rhet Turnbull |
requires_python | >=3.9,<3.13 |
license | Python Software Foundation License |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# 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](https://github.com/dateutil/dateutil) and [datefinder](https://github.com/akoumjian/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](https://github.com/RhetTbull/strpdatetime).
## Usage
```pycon
>>> 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](https://docs.python.org/3/library/datetime.html#strftime-and-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
```pycon
>>> 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.
```bash
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](https://github.com/RhetTbull/strpdatetime).
Raw data
{
"_id": null,
"home_page": "",
"name": "strpdatetime",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.9,<3.13",
"maintainer_email": "",
"keywords": "",
"author": "Rhet Turnbull",
"author_email": "rturnbull+git@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/19/cd/67359a49878a92da35aa5fec9d3970a34aa3da44b5e29f0c7032be478bc1/strpdatetime-0.3.0.tar.gz",
"platform": null,
"description": "# strpdatetime\n\nA 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.\n\n## Why this package?\n\nA common use case is parsing date/time information from strings, for example filenames with\nthe date and time embedded in them. The standard library's `datetime.datetime.strptime` works\nwell if the string perfectly matches the format string, but does not work if the string\ncontains 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.\n\nThird-party packages such as [dateutil](https://github.com/dateutil/dateutil) and [datefinder](https://github.com/akoumjian/datefinder) are more flexible but still fail to find the date in the above example and other common filename date/time formats.\n\n`strpdatetime` can find the date in the above example using `strpdatetime(\"IMG_1234_2022_11_20.jpeg\", \"^IMG_*_%Y_%m_%d\")`\n\n## Installation\n\n`pip install strpdatetime`\n\nTo install from source, clone the repository, `pip install poetry`, and run `poetry install`.\n\n## Source Code\n\nThe source code is available on [GitHub](https://github.com/RhetTbull/strpdatetime).\n\n## Usage\n\n```pycon\n>>> import datetime\n>>> from strpdatetime import strpdatetime\n>>> dt = strpdatetime(\"IMG_1234_2022_11_20.jpeg\",\"^IMG_*_%Y_%m_%d.*\")\n>>> assert dt == datetime.datetime(2022,11,20)\n>>>\n```\n\n## Syntax\n\nIn addition to the standard `strptime` [format codes](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes), `strpdatetime` supports the following:\n\n- *: Match any number of characters\n- ^: Match the beginning of the string\n- $: Match the end of the string\n- {n}: Match exactly n characters\n- {n,}: Match at least n characters\n- {n,m}: Match at least n characters and at most m characters\n- In addition to `%%` for a literal `%`, the following format codes are supported:\n `%^`, `%$`, `%*`, `%|`, `%{`, `%}` for `^`, `$`, `*`, `|`, `{`, `}` respectively\n- |: join multiple format codes; each code is tried in order until one matches\n- Unlike the standard library, the leading zero is not optional for %d, %m, %H, %I, %M, %S, %j, %U, %W, and %V\n- For optional leading zero, use %-d, %-m, %-H, %-I, %-M, %-S, %-j, %-U, %-W, and %-V\n\n## Examples\n\n```pycon\n>>> from strpdatetime import strpdatetime\n>>> strpdatetime(\"IMG_1234_2022_11_20.jpg\",\"^IMG_{4}_%Y_%m_%d\")\ndatetime.datetime(2022, 11, 20, 0, 0)\n>>> strpdatetime(\"IMG_1234_2022_11_20.jpg\",\"IMG_*_%Y_%m_%d\")\ndatetime.datetime(2022, 11, 20, 0, 0)\n>>> strpdatetime(\"1234_05_06_2022_11_20\",\"%Y_%m_%d$\")\ndatetime.datetime(2022, 11, 20, 0, 0)\n>>> strpdatetime(\"1234_05_06_2022_11_20\",\"IMG_*_%Y_%m_%d|%Y_%m_%d$\")\ndatetime.datetime(2022, 11, 20, 0, 0)\n>>>\n```\n\n## Command Line\n\n`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.\n\n```bash\npython -m strpdatetime \"^IMG_*_%Y_%m_%d|IMG{4}_%Y-%m-%d-%H%M%S\" *.jpg\nIMG1234_2022-11-20-063400.jpg, 2022-11-20T06:34:00\nIMG_1234_2022_11_21.jpg, 2022-11-21T00:00:00\ntime data 'IMG_2132.jpg' does not match format '^IMG_*_%Y_%m_%d|IMG{4}_%Y-%m-%d-%H%M%S'\nIMG_2132.jpg,\nIMG_5678_2022_11_20.jpg, 2022-11-20T00:00:00\n```\n\n## License\n\nTo 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.\n\n## Contributing\n\nContributions of all kinds are welcome! Please open an issue or pull request on [GitHub](https://github.com/RhetTbull/strpdatetime).\n\n",
"bugtrack_url": null,
"license": "Python Software Foundation License",
"summary": "Parse strings into Python datetime objects; extends Python's datetime.strptime() with additional features.",
"version": "0.3.0",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1e323fdee5137beca0aea2502db4b6343a8a852f1648a92b716245a0aeae631a",
"md5": "4bd6536e9c9bc7325e3bbf6c3a50ec21",
"sha256": "daf11a50e438b62b47241762bdee8857f1890e7eedfcca595ce1f20ab937e967"
},
"downloads": -1,
"filename": "strpdatetime-0.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4bd6536e9c9bc7325e3bbf6c3a50ec21",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9,<3.13",
"size": 16952,
"upload_time": "2023-11-13T15:45:48",
"upload_time_iso_8601": "2023-11-13T15:45:48.225874Z",
"url": "https://files.pythonhosted.org/packages/1e/32/3fdee5137beca0aea2502db4b6343a8a852f1648a92b716245a0aeae631a/strpdatetime-0.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "19cd67359a49878a92da35aa5fec9d3970a34aa3da44b5e29f0c7032be478bc1",
"md5": "ae25f8138ba6de835359cf202c7cd526",
"sha256": "01b95a4841db31503f616b3ba545f0b548841598296b17000ba00fbe11e49264"
},
"downloads": -1,
"filename": "strpdatetime-0.3.0.tar.gz",
"has_sig": false,
"md5_digest": "ae25f8138ba6de835359cf202c7cd526",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9,<3.13",
"size": 17242,
"upload_time": "2023-11-13T15:45:50",
"upload_time_iso_8601": "2023-11-13T15:45:50.175458Z",
"url": "https://files.pythonhosted.org/packages/19/cd/67359a49878a92da35aa5fec9d3970a34aa3da44b5e29f0c7032be478bc1/strpdatetime-0.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-13 15:45:50",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "strpdatetime"
}