Skip to main content

Use this package to easily convert various time formats to milliseconds.

Project description

PyPI version Python Support

pymillis

Use this package to easily convert various time formats to milliseconds.

Installation

pip install pymillis

Usage

Basic Usage

from pymillis import ms

# Parse time strings to milliseconds
ms('2 days')          # 172800000
ms('1d')              # 86400000
ms('10h')             # 36000000
ms('2.5 hrs')         # 9000000
ms('2h')              # 7200000
ms('1m')              # 60000
ms('5s')              # 5000
ms('1y')              # 31557600000
ms('100')             # 100
ms('-3 days')         # -259200000
ms('-1h')             # -3600000
ms('-200')            # -200

# Format milliseconds to strings
ms(60000)             # '1m'
ms(2 * 60000)         # '2m'
ms(-3 * 60000)        # '-3m'
ms(172800000)         # '2d'

# Use long format
ms(60000, long=True)           # '1 minute'
ms(2 * 60000, long=True)       # '2 minutes'
ms(172800000, long=True)       # '2 days'
ms(ms('10 hours'), long=True)  # '10 hours'

API

ms(value, *, long=False)

Parse or format the given value.

Parameters:

  • value (str | int | float): The string or number to convert
  • long (bool, optional): Set to True to use verbose formatting. Defaults to False.

Returns:

  • If value is a string, returns milliseconds as int (for whole numbers) or float (for decimals)
  • If value is a number, returns formatted string as str

Raises:

  • MSError: If value is not a non-empty string or a number

parse(value)

Parse the given string and return milliseconds.

Parameters:

  • value (str): A string to parse to milliseconds

Returns:

  • int | float: The parsed value in milliseconds (int for whole numbers, float for decimals)

Raises:

  • MSError: If the string is invalid or cannot be parsed

format(ms_value, *, long=False)

Format the given milliseconds as a string.

Parameters:

  • ms_value (int | float): Milliseconds to format
  • long (bool, optional): Use verbose formatting if True

Returns:

  • str: The formatted string

Raises:

  • MSError: If ms_value is not a finite number

Import Options

# Import main function
from pymillis import ms

# Import specific functions
from pymillis import parse, format, parse_strict

# Import exception
from pymillis import MSError

# Import everything
from pymillis import ms, parse, format, MSError

Supported Time Units

Short Format

  • ms, msec, msecs, millisecond, milliseconds - Milliseconds
  • s, sec, secs, second, seconds - Seconds
  • m, min, mins, minute, minutes - Minutes
  • h, hr, hrs, hour, hours - Hours
  • d, day, days - Days
  • w, week, weeks - Weeks
  • mo, month, months - Months (calculated as 1/12 of a year)
  • y, yr, yrs, year, years - Years (calculated as 365.25 days)

Case Insensitive

All units are case-insensitive, so 1D, 1d, 1 Day, 1 DAY are all equivalent.

Features

  • 🚀 Simple and intuitive API
  • 📦 Zero dependencies
  • 🔄 Bidirectional conversion (string ↔ milliseconds)
  • ⏱️ Supports negative time values
  • 📝 Long and short format options
  • 🎯 Type hints for better IDE support
  • ✅ Comprehensive error handling

Common Use Cases

Setting Timeouts

import time
from pymillis import ms

# Convert to seconds for time.sleep()
timeout = ms('5s') / 1000
time.sleep(timeout)

Caching

import time
from pymillis import ms

# Set cache expiration
cache_duration = ms('1h')
expires_at = time.time() * 1000 + cache_duration

Rate Limiting

from pymillis import ms

# Define rate limit window
rate_limit_window = ms('1m')
max_requests = 100

Calculating Durations

from pymillis import ms

# Calculate time differences
meeting_duration = ms('2h') - ms('30m')  # 5400000 ms (1.5 hours)

Error Handling

The library raises MSError for invalid inputs:

from pymillis import ms, MSError

# Invalid format
try:
    ms('invalid')
except MSError as e:
    print(f"Error: {e}")

# Non-finite number
try:
    ms(float('nan'))
except MSError as e:
    print(f"Error: {e}")

# Empty string
try:
    ms('')
except MSError as e:
    print(f"Error: {e}")

# String too long (>100 characters)
try:
    ms('a' * 101)
except MSError as e:
    print(f"Error: {e}")

Notes

Precision

  • Month calculation: 1 month = 1/12 year ≈ 30.44 days (average value)
  • Year calculation: 1 year = 365.25 days (accounting for leap years)

Rounding

When formatting, values are rounded to the nearest integer for the selected unit:

ms(1500)              # '2s'  (rounded from 1.5s)
ms(90000)             # '2m'  (rounded from 1.5m)

📜 License

MIT License © 2025-PRESENT wudi

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

pymillis-1.0.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

pymillis-1.0.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file pymillis-1.0.2.tar.gz.

File metadata

  • Download URL: pymillis-1.0.2.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pymillis-1.0.2.tar.gz
Algorithm Hash digest
SHA256 013b3c27a0631df28fb4a231943b5a32d7cb1a2f0fd6e53aba4176b290878e6c
MD5 fc3a392ce2b68803de3063f6005ab51e
BLAKE2b-256 d4164577aba51f8c05746f21d0805436c100cae14caf2b262830b7df74b9f8e9

See more details on using hashes here.

File details

Details for the file pymillis-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pymillis-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pymillis-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 070d0ba1b301c7c397d01bb6013daca9fee590c91a38a87b629bf4dfd6647763
MD5 e54b50359782fde38aed78e56c9f3b48
BLAKE2b-256 f3e96afd262a4d5539f3ea23d9ca7a838eb33e3f820d0b9ec111ac784de19207

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