date_time_utils
This module contains the DateTimeActions class that is used to perform various date and time related actions.
DateTimeActions
A class used to perform various date and time related actions.
...
Attributes
datetime_format : str a formatted string that defines the date and time format logger_class : CoreLogger an instance of the CoreLogger class logger : Logger a standard Python logger instance
Source code in libs\cafex_core\src\cafex_core\utils\date_time_utils.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
__init__(date_time_format='%Y-%m-%d %H:%M:%S')
Constructs all the necessary attributes for the DateTimeActions object.
Parameters
date_time_format : str, optional
a formatted string that defines the date and time format
(default is "%Y-%m-%d %H:%M:%S")
Source code in libs\cafex_core\src\cafex_core\utils\date_time_utils.py
28 29 30 31 32 33 34 35 36 37 38 39 40 | |
get_current_date_time()
Returns the current date and time in ISO 8601 format with milliseconds.
Examples:
from cafex_core.utils.date_time_utils import DateTimeActions DateTimeActions().get_current_date_time()
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The current date and time in ISO 8601 format with milliseconds. |
Source code in libs\cafex_core\src\cafex_core\utils\date_time_utils.py
65 66 67 68 69 70 71 72 73 74 75 76 | |
get_current_datetime(to_str=False)
Returns the current date and time.
If the to_str parameter is True, the date and time is returned as a string. Otherwise, it is returned as a datetime object.
Examples:
from cafex_core.utils.date_time_utils import DateTimeActions DateTimeActions().get_current_datetime() DateTimeActions().get_current_datetime(to_str=True)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_str
|
bool
|
A flag that defines the return type (default is False). |
False
|
Returns:
| Type | Description |
|---|---|
|
datetime or str: The current date and time as a datetime object or a |
|
|
formatted string. |
Source code in libs\cafex_core\src\cafex_core\utils\date_time_utils.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
get_time_by_zone(timezone)
Fetches the current time in a specified timezone.
This method takes a timezone string and returns the current time in that timezone.
Examples:
from cafex_core.utils.date_time_utils import DateTimeActions DateTimeActions().get_time_by_zone("America/New_York")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timezone
|
str
|
The timezone for which the current time is to be fetched. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The current time in the specified timezone. |
Raises:
| Type | Description |
|---|---|
Exception
|
If there is an error in fetching the current time in the specified timezone. |
Source code in libs\cafex_core\src\cafex_core\utils\date_time_utils.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
get_time_difference_seconds(end, start)
Returns the time difference in seconds between two times.
Examples:
from cafex_core.utils.date_time_utils import DateTimeActions DateTimeActions().get_time_difference_seconds('2025-03-04T18:29:01.295', '2025-03-04T18:21:01.287')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
end
|
str
|
the end time in ISO 8601 format. |
required |
start
|
str
|
The start time in ISO 8601 format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The time difference in seconds. |
Raises:
| Type | Description |
|---|---|
Exception
|
If there is an error in calculating the time difference. |
Source code in libs\cafex_core\src\cafex_core\utils\date_time_utils.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
seconds_to_human_readable(seconds)
Converts seconds to a human-readable format.
Examples:
from cafex_core.utils.date_time_utils import DateTimeActions DateTimeActions().seconds_to_human_readable(3661)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
int
|
The number of seconds. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A human-readable string representing the duration. |
Raises:
| Type | Description |
|---|---|
Exception
|
If there is an error in converting seconds to a human-readable format. |
Source code in libs\cafex_core\src\cafex_core\utils\date_time_utils.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |