Registry / serialization / datetime-truncate

datetime-truncate

JSON →
library1.1.1pypypi✓ verified 91d ago

Truncate datetime objects to a specified level of precision (e.g., year, month, day, hour, minute, second). Version 1.1.1 is stable with no active changes; the package is in maintenance mode.

pip install datetime-truncate
INSTALL
IMPORT
SIG · DATETIME-TRUNCATE
D
datetime-truncate
serializationpythonv1.1.1
Install
1.5s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.1.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.002s · 17.8MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.002s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

truncate
✓ from datetime_truncate import truncate
✗ from datetime import truncate
datetime_truncate is a separate package, not part of stdlib.

Truncates to 'year', 'month', 'day', 'hour', 'minute', or 'second'. Returns a new datetime object.

from datetime import datetime from datetime_truncate import truncate dt = datetime(2025, 4, 15, 10, 30, 45, 123456) truncated = truncate(dt, 'hour') print(truncated) # 2025-04-15 10:00:00
Debug
Known issues
gotchaThe function flips between truncate() and truncate_to(). In version 1.0.0, the function was named truncate_to; in 1.1.0 it was renamed to truncate. Check your version.
fix
If using 1.0.x, use truncate_to. For 1.1+, use truncate.
affects: >=1.0.0, <1.1.0
deprecatedLevel parameter uses singular names: 'year', 'month', 'day', 'hour', 'minute', 'second'. Not plural.
fix
Use 'hour' not 'hours'.
affects: all
gotchaTime zones are not preserved; the function returns a naive datetime. If you pass a timezone-aware datetime, the tzinfo is lost.
fix
Manually re-attach timezone after truncation: result = truncate(dt, 'day').replace(tzinfo=dt.tzinfo).
affects: all
Errors
Common errors & fixes
ImportError: cannot import name 'truncate_to' from 'datetime_truncate'
The function was renamed from truncate_to to truncate in version 1.1.0.
fix
Install latest version: pip install datetime-truncate --upgrade. Then use from datetime_truncate import truncate.
AttributeError: 'datetime.datetime' object has no attribute 'truncate'
datetime_truncate does not monkey-patch datetime; it provides a standalone function.
fix
Import and call truncate() from datetime_truncate, not as a method on a datetime object.
ValueError: invalid level: 'weeks'
The library does not support truncation to weeks; only year, month, day, hour, minute, second.
fix
Use 'day' or 'hour' and adjust manually.
Upgrade
Version history
1.1.1latest on PyPI · released Aug 6, 2020
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
datetime-truncate — pip install datetime-truncate · libregistry