Use this package to easily convert various time formats to milliseconds.
Project description
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 convertlong(bool, optional): Set toTrueto use verbose formatting. Defaults toFalse.
Returns:
- If
valueis a string, returns milliseconds asint(for whole numbers) orfloat(for decimals) - If
valueis a number, returns formatted string asstr
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 formatlong(bool, optional): Use verbose formatting ifTrue
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- Millisecondss,sec,secs,second,seconds- Secondsm,min,mins,minute,minutes- Minutesh,hr,hrs,hour,hours- Hoursd,day,days- Daysw,week,weeks- Weeksmo,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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
013b3c27a0631df28fb4a231943b5a32d7cb1a2f0fd6e53aba4176b290878e6c
|
|
| MD5 |
fc3a392ce2b68803de3063f6005ab51e
|
|
| BLAKE2b-256 |
d4164577aba51f8c05746f21d0805436c100cae14caf2b262830b7df74b9f8e9
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
070d0ba1b301c7c397d01bb6013daca9fee590c91a38a87b629bf4dfd6647763
|
|
| MD5 |
e54b50359782fde38aed78e56c9f3b48
|
|
| BLAKE2b-256 |
f3e96afd262a4d5539f3ea23d9ca7a838eb33e3f820d0b9ec111ac784de19207
|