跳转至

Date time

labridge.tools.common.date_time

labridge.tools.common.date_time.get_current_date_time()

This function is used to get the date of today, current time. If you are not sure about current date or time, use this tool to help you.

The returned date is of the following format: "Year-Month-Day". The returned time is of the following format: "Hour:Minute:Time".

Source code in labridge\tools\common\date_time.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def get_current_date_time() -> FuncOutputWithLog:
	r"""
	This function is used to get the date of today, current time.
	If you are not sure about current date or time, use this tool to help you.

	The returned date is of the following format: "Year-Month-Day".
	The returned time is of the following format: "Hour:Minute:Time".
	"""
	today = datetime.datetime.today()
	current_date = today.date().strftime(DATE_FORMAT)
	current_time = today.time().strftime(TIME_FORMAT)
	current_weekday = today.weekday() + 1

	datetime_str = (
		f"Today is {current_date}\n"
		f"Today is the No.{current_weekday} day in a week.\n"
		f"Current time is {current_time}\n"
	)
	return FuncOutputWithLog(
		fn_output=datetime_str,
		fn_log="",
	)

labridge.tools.common.date_time.get_date_time_from_now(backward, days)

This function is used to infer the exact date that a statement means. such as '3 days ago', '2 months later'.

PARAMETER DESCRIPTION
backward

the direction, if the statement means the date earlier than now, it is set to True. Otherwise, it is False.

TYPE: bool

days

the number of days

TYPE: int

RETURNS DESCRIPTION
FuncOutputWithLog

the result date.

Source code in labridge\tools\common\date_time.py
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
def get_date_time_from_now(backward: bool, days: int) -> FuncOutputWithLog:
	r"""
	This function is used to infer the exact date that a statement means. such as '3 days ago', '2 months later'.

	Args:
		backward (bool): the direction, if the statement means the date earlier than now, it is set to `True`.
			Otherwise, it is `False`.
		days (int): the number of days

	Returns:
		the result date.
	"""
	today = datetime.datetime.today()
	if backward:
		result_datetime = today - datetime.timedelta(days=days)
	else:
		result_datetime = today + datetime.timedelta(days=days)

	result_date = result_datetime.date().strftime(DATE_FORMAT)
	result_weekday = result_datetime.weekday() + 1
	direction_str = "ago" if backward else "later"
	datetime_str = (
		f"{days} days {direction_str} is {result_date}\n"
		f"the No.{result_weekday} day in a week.\n"
	)
	return FuncOutputWithLog(
		fn_output=datetime_str,
		fn_log="",
	)