Terarea  2
The automation project
Loading...
Searching...
No Matches
sql_time_manipulation.py
Go to the documentation of this file.
1"""
2 File in charge of containing the functions that allow for time conversion between datetime and strings.
3"""
4
5from datetime import datetime
6
7from display_tty import Disp, TOML_CONF, SAVE_TO_FILE, FILE_NAME
8
9from . import sql_constants as SCONST
10
11
13 """_summary_
14 """
15
16 def __init__(self, debug: bool = False) -> None:
17 """_summary_
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
20
21 Args:
22 debug (bool, optional): _description_. Defaults to False.
23 """
24 self.debug: bool = debug
25 # ----------------------- Inherited from SCONST -----------------------
26 self.date_only: str = SCONST.DATE_ONLY
27 self.date_and_time: str = SCONST.DATE_AND_TIME
28 # --------------------------- logger section ---------------------------
29 self.disp: Disp = Disp(
30 TOML_CONF,
31 SAVE_TO_FILE,
32 FILE_NAME,
33 debug=self.debug,
34 logger=self.__class__.__name__
35 )
36
37 def datetime_to_string(self, datetime_instance: datetime, date_only: bool = False, sql_mode: bool = False) -> str:
38 """_summary_
39 Convert a datetime instance to a string.
40
41 Args:
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.
45
46 Raises:
47 ValueError: _description_: If the datetime instance is not a datetime, a valueerror is raised.
48
49 Returns:
50 str: _description_: A string instance of the datetime.
51 """
52
53 if isinstance(datetime_instance, datetime) is False:
54 self.disp.log_error(
55 "The input is not a datetime instance.",
56 "datetime_to_string"
57 )
58 raise ValueError("Error: Expected a datetime instance.")
59 if date_only is True:
60 return datetime_instance.strftime(self.date_only)
61 converted_time = datetime_instance.strftime(self.date_and_time)
62 if sql_mode is True:
63 microsecond = datetime_instance.strftime("%f")[:3]
64 res = f"{converted_time}.{microsecond}"
65 else:
66 res = f"{converted_time}"
67 return res
68
69 def string_to_datetime(self, datetime_string_instance: str, date_only: bool = False) -> str:
70 """_summary_
71 Convert a datetime instance to a string.
72
73 Args:
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.
76
77 Raises:
78 ValueError: _description_: If the datetime instance is not a datetime, a valueerror is raised.
79
80 Returns:
81 str: _description_: A string instance of the datetime.
82 """
83
84 if isinstance(datetime_string_instance, str) is False:
85 self.disp.log_error(
86 "The input is not a string instance.",
87 "string_to_datetime"
88 )
89 raise ValueError("Error: Expected a string instance.")
90 if date_only is True:
91 return datetime.strptime(datetime_string_instance, self.date_only)
92 return datetime.strptime(datetime_string_instance, self.date_and_time)
93
94 def get_correct_now_value(self) -> str:
95 """_summary_
96 Get the current date and time in the correct format for the database.
97
98 Returns:
99 str: _description_
100 """
101 current_time = datetime.now()
102 return current_time.strftime(self.date_and_time)
103
105 """_summary_
106 Get the current date and time in the correct format for the database.
107
108 Returns:
109 str: _description_
110 """
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 string_to_datetime(self, str datetime_string_instance, bool date_only=False)