Python package for converting various units.
Project description
unic
unic is a lightweight Python package for converting:
- time units (
hour,min,sec,msec) - Unix timestamps to
datetime/date - datetime strings to Unix timestamps
- metric length units (
nmtoTm)
It is designed for simple scripting, data processing, analytics, and timestamp handling.
Installation
pip install unic
Quick Start
import unic
# Time conversion
time_model = unic.load_model("time")
print(time_model.convert(2, from_unit="hour", to_unit="min")) # 120
# Unixtime -> datetime
datetime_model = unic.load_model("datetime")
print(datetime_model.convert(1657985494, format="datetime"))
# String datetime -> unixtime
unixtime_model = unic.load_model("unixtime")
print(unixtime_model.convert("2022-07-18 13:49:00", tz="Asia/Tokyo"))
# Metric system conversion
metric_model = unic.load_model("metric_system")
print(metric_model.convert(1200, from_unit="mm", to_unit="m")) # 1.2
Supported Conversions
TimeModel
hour/min/sec/msec
DatetimeModel
- unixtime ->
datetime.datetime - unixtime ->
datetime.date
UnixtimeModel
- datetime string -> unixtime
- supported formats:
yyyy-mm-dd hh:mm:ssyyyy/mm/dd hh:mm:ss
MetricSystemModel
- metric length units:
nm,um,mm,cm,m,km,Mm,Gm,Tm
Examples
Convert a single value
import unic
model = unic.load_model("time")
result = model.convert(7.5, from_unit="hour", to_unit="min")
print(result) # 450
Convert a list of values
import unic
model = unic.load_model("time")
result = model.convert([2, 4, 6], from_unit="hour", to_unit="min")
print(result) # [120, 240, 360]
Convert unixtime to datetime
import unic
model = unic.load_model("datetime")
result = model.convert(1657985494, format="datetime", tz="Asia/Tokyo")
print(result)
Convert datetime string to unixtime
import unic
model = unic.load_model("unixtime")
result = model.convert("2022-07-18 13:49:00.123", unit="msec")
print(result)
Notes
convert()supports both single values and lists.convert_batch()is deprecated. Please useconvert()with a list instead.- If
unitis omitted inUnixtimeModel, the default issec.
Documentation
- See
docs/UserGuide.md - Add future pages:
docs/QuickStart.mddocs/SupportedConversions.mddocs/FAQ.mddocs/MigrationGuide.md
Why unic?
- Simple API
- Lightweight package
- Supports list conversion
- Supports timezone-aware conversion
- Tested with multiple Python versions
License
MIT
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
unic-2.2.1.tar.gz
(57.5 kB
view details)
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
unic-2.2.1-py3-none-any.whl
(15.0 kB
view details)
File details
Details for the file unic-2.2.1.tar.gz.
File metadata
- Download URL: unic-2.2.1.tar.gz
- Upload date:
- Size: 57.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51128b1271be85db3f1d8a0d23bf05e096ecae77fe936468b3e04fa390d8cab5
|
|
| MD5 |
20eed121ed162e3ca0e71b691905cc73
|
|
| BLAKE2b-256 |
a405343a759d0aa5731a5ed271db44e05a1aa51f9d73e2adf398c7f943261fdf
|
File details
Details for the file unic-2.2.1-py3-none-any.whl.
File metadata
- Download URL: unic-2.2.1-py3-none-any.whl
- Upload date:
- Size: 15.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a1fa0b8a431f41ccf5e3341d5c5c7e5dbe0bcd68436b7b7c3d6c3c351cef18b
|
|
| MD5 |
cfa53783889493e29c504ab9253e5312
|
|
| BLAKE2b-256 |
d27562d6cb5a16322b032c8aac9807ab13b6290b00bf7df10784635a342f628b
|