dt
Timer ¶
Timer(*, ms: bool = True)
A simple timer class that keeps track of all the stops.
Source code in stdl/dt.py
stop ¶
stop(label: str | None = None) -> TimerStop
Stop the timer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str | None
|
Label for the stop. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
TimerStop |
TimerStop
|
The stop data |
Source code in stdl/dt.py
taken_seconds ¶
reset ¶
datetime_fmt ¶
datetime_fmt(d: str | float | int | None | datetime = None, fmt: str = 'Ymd', dsep: str = '-', tsep: str = ':', *, ms: bool = False, utc: bool = True) -> str
Format date and time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
str | float | None | datetime
|
Input datetime. Defaults to current date and time. |
None
|
fmt
|
str
|
Date format. |
'Ymd'
|
dsep
|
str
|
Date values separator. |
'-'
|
tsep
|
str
|
Time values separator. |
':'
|
ms
|
bool
|
include milliseconds. |
False
|
utc
|
bool
|
Use the UTC timezone when building a datetime object from a timestamp. |
True
|
Raises:
| Type | Description |
|---|---|
TypeError
|
Raises TypeError if the type of |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Formatted date and time. |
Example
Source code in stdl/dt.py
time_fmt ¶
time_fmt(t: float | datetime | int | None = None, sep: str = ':', *, ms: bool = False, utc: bool = True) -> str
Format time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
float | datetime | int | None
|
Input time. Defaults to current time. |
None
|
sep
|
str
|
Character(s) that separates hours, minutes, seconds... |
':'
|
ms
|
bool
|
include milliseconds. |
False
|
utc
|
bool
|
Use the UTC timezone when building a time object from a timestamp. |
True
|
Raises:
| Type | Description |
|---|---|
TypeError
|
Raises TypeError if the type of |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Formatted time. |
Source code in stdl/dt.py
date_fmt ¶
Format date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
float | date | datetime | int | None
|
Input date. Defaults to current date. |
None
|
fmt
|
str
|
Date format. |
'Ymd'
|
sep
|
str
|
Character(s) that separates days, months and years. |
'-'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
Raises TypeError if the type of |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Formatted date. |
Example
Source code in stdl/dt.py
date_range ¶
Return a generator for dates between start and end.
Example
Source code in stdl/dt.py
datetime_range ¶
Returns a generator for datetimes between start and end with a given step.
Example
Source code in stdl/dt.py
sleep ¶
Sleeps for a random duration within a specified range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lo
|
float
|
The lower bound of the sleep duration range (inclusive). |
required |
hi
|
float
|
The upper bound of the sleep duration range (inclusive). If not provided, the function will sleep for the exact duration specified by |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The actual sleep duration. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in stdl/dt.py
seconds_to_hms ¶
Convert time in seconds to a string in the format HH:MM:SS[.mmm].
Example
seconds_to_hms(123.456) '00:02:03' seconds_to_hms(123.456, ms=True) '00:02:03.456' seconds_to_hms(-123.456, ms=True) '-00:02:03.456'
Source code in stdl/dt.py
hms_to_seconds ¶
Converts a string in the format HH:MM:SS[.mmm] or h:m:s or m:s to time in seconds.
Example
hms_to_seconds('00:02:03') 123.0 hms_to_seconds('00:02:03.456', ms=True) 123.456