2 File in charge of containing the functions that allow for time conversion between datetime and strings.
5from datetime
import datetime
7from display_tty
import Disp, TOML_CONF, SAVE_TO_FILE, FILE_NAME
9from .
import sql_constants
as SCONST
16 def __init__(self, debug: bool =
False) ->
None:
18 This is the class that contains functions in charge of manipulating time.
19 It can convert time from as string to a datetime and vice-versa
22 debug (bool, optional): _description_. Defaults to False.
34 logger=self.__class__.__name__
37 def datetime_to_string(self, datetime_instance: datetime, date_only: bool =
False, sql_mode: bool =
False) -> str:
39 Convert a datetime instance to a string.
42 datetime_instance (datetime): _description_: The datetime item
43 date_only (bool, optional): _description_. Defaults to False.: if True will only return the date section, otherwise will return the date and time section.
44 sql_mode (bool, optional): _description_. Defaults to False.: if True, will add the microseconds to the response so that it can be directly inserted into an sql command.
47 ValueError: _description_: If the datetime instance is not a datetime, a valueerror is raised.
50 str: _description_: A string instance of the datetime.
53 if isinstance(datetime_instance, datetime)
is False:
55 "The input is not a datetime instance.",
58 raise ValueError(
"Error: Expected a datetime instance.")
60 return datetime_instance.strftime(self.
date_only)
61 converted_time = datetime_instance.strftime(self.
date_and_time)
63 microsecond = datetime_instance.strftime(
"%f")[:3]
64 res = f
"{converted_time}.{microsecond}"
66 res = f
"{converted_time}"
71 Convert a datetime instance to a string.
74 datetime_string_instance (str): _description_: The string datetime item
75 date_only (bool, optional): _description_. Defaults to False.: if True will only return the date section, otherwise will return the date and time section.
78 ValueError: _description_: If the datetime instance is not a datetime, a valueerror is raised.
81 str: _description_: A string instance of the datetime.
84 if isinstance(datetime_string_instance, str)
is False:
86 "The input is not a string instance.",
89 raise ValueError(
"Error: Expected a string instance.")
91 return datetime.strptime(datetime_string_instance, self.
date_only)
92 return datetime.strptime(datetime_string_instance, self.
date_and_time)
96 Get the current date and time in the correct format for the database.
101 current_time = datetime.now()
106 Get the current date and time in the correct format for the database.
111 current_time = datetime.now()
112 return current_time.strftime(self.
date_only)
str datetime_to_string(self, datetime datetime_instance, bool date_only=False, bool sql_mode=False)
str get_correct_current_date_value(self)
None __init__(self, bool debug=False)
str get_correct_now_value(self)
str string_to_datetime(self, str datetime_string_instance, bool date_only=False)