Types Module¶
mosaicolabs.types.Time ¶
Bases: BaseModel
A high-precision time representation.
The Time class splits a timestamp into a 64-bit integer for seconds and a 32-bit
unsigned integer for nanoseconds.
Attributes:
| Name | Type | Description |
|---|---|---|
seconds |
int
|
Seconds since the epoch (Unix time). |
nanoseconds |
int
|
Nanoseconds component within the current second, ranging from 0 to 999,999,999. |
nanoseconds
instance-attribute
¶
Nanoseconds component within the current second, ranging from 0 to 999,999,999.
validate_nanosec
classmethod
¶
Ensures nanoseconds are within the valid [0, 1e9) range.
from_float
classmethod
¶
Factory method to create a Time object from a float (seconds since epoch).
This method carefully handles floating-point artifacts by using rounding for the fractional part to ensure stable nanosecond conversion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ftime
|
float
|
Total seconds since epoch (e.g., from |
required |
Returns:
| Type | Description |
|---|---|
Time
|
A normalized |
from_milliseconds
classmethod
¶
Factory method to create a Time object from a total count of milliseconds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_milliseconds
|
int
|
Total time elapsed in milliseconds. |
required |
Returns:
| Type | Description |
|---|---|
Time
|
A |
from_nanoseconds
classmethod
¶
Factory method to create a Time object from a total count of nanoseconds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_nanoseconds
|
int
|
Total time elapsed in nanoseconds. |
required |
Returns:
| Type | Description |
|---|---|
Time
|
A |
from_datetime
classmethod
¶
Factory method to create a Time object from a Python datetime instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
datetime
|
A standard Python |
required |
Returns:
| Type | Description |
|---|---|
Time
|
A |
to_float ¶
Converts the high-precision time to a float.
Precision Loss
Converting to a 64-bit float may result in the loss of nanosecond precision due to mantissa limitations.
to_nanoseconds ¶
Converts the time to a total integer of nanoseconds.
This conversion preserves full precision.
to_milliseconds ¶
Converts the time to a total integer of milliseconds.
This conversion preserves full precision.
to_datetime ¶
Converts the time to a Python UTC datetime object.
Microsecond Limitation
Python's datetime objects typically support microsecond precision;
nanosecond data below that threshold will be truncated.