Skip to main content

A Python library to get relative datetime strings and parse datetime strings.

Project description

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 datetime objects 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 datetime object is in the past or future.
  • Parse a wide variety of datetime string formats into datetime objects.
  • 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.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. 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.

These examples should provide a clear understanding of how to use the relative-datetime library. You can expand or modify them based on the specific use cases of your library.

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

relative_datetime-0.1.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

relative_datetime-0.1.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file relative_datetime-0.1.1.tar.gz.

File metadata

  • Download URL: relative_datetime-0.1.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.4

File hashes

Hashes for relative_datetime-0.1.1.tar.gz
Algorithm Hash digest
SHA256 00e2c3072363feeb66ba803ec81fcfb9511b4c7776fc6ba665e7a683de6e3a81
MD5 dd3b1ad359edfe6e1da5d210f740e1e2
BLAKE2b-256 92d028e36b1e920c7277827f91f1825d2359fb1da1646848f01d8b729e04b61e

See more details on using hashes here.

File details

Details for the file relative_datetime-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for relative_datetime-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 977e7e16e96353e178d0a03f69b0196d314c48eaad28ca11f4125cbf09080e83
MD5 6de2748d1336f4b258d380037fe90aff
BLAKE2b-256 192300f7b503d4fc3bb96093fec34d247532443d973a7b21850e6d5dbdd543ed

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