Skip to main content

A lightweight Python library for date and time operations.

Project description

DateTimeOps

Overview

DateTimeOps is a lightweight Python package providing convenient utilities for working with dates, times, and timestamps. It simplifies common datetime operations such as formatting, validation, conversion, offsets, and differences.

Features

  • Get current UTC or timezone-specific datetime.
  • Convert between different datetime formats.
  • Validate dates and check if they are in the future.
  • Perform date arithmetic (add/subtract days, months, offsets).
  • Calculate time differences in days, months, or timestamps.
  • Convert time formats (e.g., HH:MM:SS to seconds and vice versa).
  • Get first and last date of a given month.

Installation

You can install the package using pip:

pip install datetimeops

Usage

Import the package and use its functions:

from datetimeops.datetime_utils import now_utc, add_days_to_date, validate_date, change_date_format

# Get current UTC time
print(now_utc())  # '2025-02-13 14:30:00'

# Validate a date
print(validate_date("2025-05-01"))  # True
print(validate_date("Invalid Date"))  # False

# Change date format
print(change_date_format("2025-02-13 14:30:00", "%d-%m-%Y"))  # '13-02-2025'

# Add 10 days to a date
print(add_days_to_date("2025-02-13", 10))  # '2025-02-23 00:00:00'

Functions

Date & Time Retrieval

  • now_utc(fmt="%Y-%m-%d %H:%M:%S") → Returns the current UTC time.
  • now_tz(tz='UTC', fmt="%Y-%m-%d %H:%M:%S") → Returns the current time in the specified timezone.
  • yesterday(tz='UTC', fmt="%Y-%m-%d %H:%M:%S") → Returns yesterday's time in the specified timezone.

Date Validation & Formatting

  • validate_date(d, fmt=None, chk_future_date=False) → Checks if a date is valid and optionally if it is in the future.
  • change_date_format(input_date, to_fmt, from_fmt="%Y-%m-%d %H:%M:%S") → Converts date format.

Epoch & Time Difference Calculations

  • epoch(date_str=None) → Returns Unix timestamp for a date or current time.
  • days_between_dates(start_date, end_date, fmt="%Y-%m-%d") → Returns the number of days between two dates.
  • months_between_dates(start_date, end_date) → Returns the number of months between two dates.
  • time_passed(start, end) → Returns time difference in human-readable format.

Date Arithmetic

  • add_days_to_date(date, days_to_add, to_fmt=None, from_fmt=None) → Adds days to a date.
  • add_months_to_date(date, months_to_add, to_fmt=None, from_fmt=None) → Adds months to a date.
  • offset_date(date, offset_type, offset_amount, to_fmt=None, from_fmt=None) → Applies an offset of years, months, days, etc.

Time Format Conversion

  • hhmmss_to_seconds(hhmmss) → Converts HH:MM:SS to total seconds.
  • seconds_to_hhmmss(seconds) → Converts seconds to HH:MM:SS format.

Miscellaneous

  • get_first_last_date_of_month(date, from_fmt, output_fmt) → Returns first and last date of a given month.
  • sleep_counter(seconds) → Displays a countdown timer.

Running Tests

To verify functionality, run the test suite using pytest:

pytest tests/

License

This project is licensed under the MIT License.

Contributions

Contributions are welcome! Feel free to open an issue or submit a pull request.

Author

Created by Kand Rishiraj.

Support

If you like this project, consider supporting it:

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

datetimeops-1.0.5.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

datetimeops-1.0.5-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file datetimeops-1.0.5.tar.gz.

File metadata

  • Download URL: datetimeops-1.0.5.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.24

File hashes

Hashes for datetimeops-1.0.5.tar.gz
Algorithm Hash digest
SHA256 3bb4c5444c7918e063b9a36d09589239984649a3dc014beb383edf4f685acdd1
MD5 31335ccfe85c57aa1fb4c16070507350
BLAKE2b-256 b638afb4871e8c4ca0ef2eeb63408f066ba28c55fa840fe4dc0f977357865f9a

See more details on using hashes here.

File details

Details for the file datetimeops-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: datetimeops-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.24

File hashes

Hashes for datetimeops-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 1c23ab3f4cae165aace507014e94b4271c7bc10fbd04cca3e8d8198e9fd8e45e
MD5 02f76cc50be5a19fec045c3963cf292d
BLAKE2b-256 b218fc21acd7d2a06db70ffc093f52c4e1b19a946db05060ea59ebe0773e518c

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