pt_time

Module Path: pt_dev_utility.pt_time


Description

Time utility functions for date and timestamp operations. This module provides functions for handling dates, timestamps, and time calculations for Python 3.8+.


Functions

get_current_date_components() -> Dict[str, str]

Description: Get current date components as dictionary.

Returns a dictionary containing 'day', 'month', and 'year' as strings.

Returns: - Dict[str, str] - Dictionary with keys: 'day', 'month', 'year'

Example Usage:

import pt_dev_utility as pdu

# Get current date components
date_info = pdu.get_current_date_components()
print(date_info)
# Output: {'day': '15', 'month': '03', 'year': '2024'}

# Access individual components
print(f"Today is {date_info['day']}/{date_info['month']}/{date_info['year']}")

get_current_timestamp() -> str

Description: Get current timestamp as formatted string.

Returns current timestamp in 'YYYY-MM-DD HH:MM:SS' format.

Returns: - str - Formatted timestamp string

Example Usage:

import pt_dev_utility as pdu

# Get current timestamp
timestamp = pdu.get_current_timestamp()
print(timestamp)
# Output: '2024-03-15 14:30:25'

# Use in logging or file naming
log_entry = f"[{timestamp}] Application started"
print(log_entry)

get_month_year_string(months_delta: int = 0) -> str

Description: Get month-year string with optional offset.

Takes a number of months to subtract from current date (positive values go back in time) and returns a month-year string in 'YYYY_MM' format.

Parameters:

  • months_delta (int, optional) - Number of months to subtract. Default: 0

Returns: - str - Month-year string in 'YYYY_MM' format

Example Usage:

import pt_dev_utility as pdu

# Get current month-year
current = pdu.get_month_year_string(0)
print(current)
# Output: '2024_03'

# Get previous month
previous = pdu.get_month_year_string(1)
print(previous)
# Output: '2024_02'

# Get 6 months ago
six_months_ago = pdu.get_month_year_string(6)
print(six_months_ago)
# Output: '2023_09'

get_iso_timestamp_ist() -> str

Description: Get current timestamp in ISO format with IST timezone.

Returns current timestamp as ISO formatted string with IST offset (+05:30).

Returns: - str - ISO formatted timestamp with IST timezone

Example Usage:

import pt_dev_utility as pdu

# Get ISO timestamp in IST
iso_timestamp = pdu.get_iso_timestamp_ist()
print(iso_timestamp)
# Output: '2024-03-15T14:30:25+05:30'

# Use for API calls or database entries
api_data = {
    'timestamp': iso_timestamp,
    'event': 'user_login'
}

Notes

  • All functions use the system's current time
  • IST timezone is hardcoded as UTC+05:30
  • Date components are returned as zero-padded strings
  • Functions are timezone-aware where applicable