relative-datetime
relative-datetime is a Python library for working with relative datetime strings and parsing datetime strings. It provides utility functions to easily convert datetime objects to human-readable relative times and to parse various datetime string formats into datetime objects.
Features
- Convert
datetimeobjects to human-readable relative time strings, such as:- "2 seconds ago"
- "5 minutes ago"
- "3 hours ago"
- "1 day ago"
- "2 weeks ago"
- "1 month ago"
- "2 years ago"
- Determine if a
datetimeobject is in the past or future. - Parse a wide variety of datetime string formats into
datetimeobjects. - Support for timezones.
Installation
You can install the library using pip:
pip install relative-datetime
Usage
Importing the Library
from relative_datetime import DateTimeUtils
Getting Relative Datetime Strings
To convert a datetime object to a relative datetime string and determine if it is in the past or future, use the relative_datetime method.
Example 1: Datetime in the Past
from datetime import datetime, timedelta
import pytz
from relative_datetime import DateTimeUtils
# Example datetime object (1 day ago)
input_datetime = datetime.now(pytz.utc) - timedelta(days=1)
# Get relative datetime string and direction
relative_time, direction = DateTimeUtils.relative_datetime(input_datetime)
print(f"Relative time: {relative_time}, Direction: {direction}")
# Output: Relative time: 1 day, Direction: past
Example 2: Datetime in the Future
from datetime import datetime, timedelta
import pytz
from relative_datetime import DateTimeUtils
# Example datetime object (in 2 hours)
input_datetime = datetime.now(pytz.utc) + timedelta(hours=2)
# Get relative datetime string and direction
relative_time, direction = DateTimeUtils.relative_datetime(input_datetime)
print(f"Relative time: {relative_time}, Direction: {direction}")
# Output: Relative time: 2 hours, Direction: future
Parsing Datetime Strings
To parse a datetime string into a datetime object, use the parse_datetime method.
Example 1: ISO 8601 Format
from relative_datetime import DateTimeUtils
# Example datetime string in ISO 8601 format
datetime_string = "2023-06-01T12:34:56Z"
# Parse the datetime string
parsed_datetime = DateTimeUtils.parse_datetime(datetime_string)
print(f"Parsed datetime: {parsed_datetime}")
# Output: Parsed datetime: 2023-06-01 12:34:56+00:00
Example 2: Custom Format
from relative_datetime import DateTimeUtils
# Example datetime string in a custom format
datetime_string = "June 1, 2023 12:34 PM"
# Parse the datetime string
parsed_datetime = DateTimeUtils.parse_datetime(datetime_string)
print(f"Parsed datetime: {parsed_datetime}")
# Output: Parsed datetime: 2023-06-01 12:34:00+00:00
Handling Different Timezones
The library can also handle datetime objects with different timezones.
Example: Datetime with a Specific Timezone
from datetime import datetime
import pytz
from relative_datetime import DateTimeUtils
# Example datetime object with a specific timezone
timezone = pytz.timezone("Asia/Singapore")
input_datetime = timezone.localize(datetime(2023, 6, 1, 12, 0, 0))
# Get relative datetime string and direction
relative_time, direction = DateTimeUtils.relative_datetime(input_datetime)
print(f"Relative time: {relative_time}, Direction: {direction}")
Contributing
Contributions are welcome! Please fork the repository and submit a pull request with your changes.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
Explanation of Examples
- Example 1: Datetime in the Past: Demonstrates how to convert a datetime object representing a time in the past to a relative time string.
- Example 2: Datetime in the Future: Demonstrates how to convert a datetime object representing a future time to a relative time string.
- Example 1: ISO 8601 Format: Shows how to parse a datetime string in the ISO 8601 format.
- Example 2: Custom Format: Shows how to parse a datetime string in a custom format.
- Example: Datetime with a Specific Timezone: Demonstrates handling of datetime objects with specific timezones.
Metadata
Release files for relative-datetime 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| relative_datetime-0.1.4.tar.gz | 4.4 kB | Without provenance | Details for relative_datetime-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| relative_datetime-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for relative_datetime-0.1.4-py3-none-any.whl |
Total release size: 8.9 kB
Release files / relative_datetime-0.1.4.tar.gz
| Download URL | relative_datetime-0.1.4.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
150dec9196964c0211c7dd39273ddaa8ffde2df44c2c27fb80c5148ffafdf0d0
|
|
BLAKE2b-256 checksum How to use checksums |
1d4bfd8bd2b4ed555851e3077e1f396f608b3cfc37fcda2214e159aed87f2b28
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.10.4
|
Release files / relative_datetime-0.1.4-py3-none-any.whl
| Download URL | relative_datetime-0.1.4-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1fee9b915d85b43e62547c4c8d69a66456786c439ff2d42b056a49b6f3fd7f93
|
|
BLAKE2b-256 checksum How to use checksums |
e75cdc08ccc12227f9becdbc0220ca8d3aa0acfd8807898f72edbbab036ae937
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.10.4
|
Security for relative-datetime 0.1.4
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.