memoryscope.core.utils.datetime_handler
- class memoryscope.core.utils.datetime_handler.DatetimeHandler(dt: datetime | str | int | float | None = None)[源代码]
基类:
object
Handles operations related to datetime such as parsing, extraction, and formatting, with support for both Chinese and English contexts including weekday names and specialized text parsing for date components.
- __init__(dt: datetime | str | int | float | None = None)[源代码]
Initialize the DatetimeHandler instance with a datetime object, string, integer, or float representation of a timestamp. If no argument is provided, the current time is used.
- 参数:
dt (datetime.datetime | str | int | float, optional) -- The datetime to be handled. Can be a datetime object, a timestamp string, or a numeric timestamp. Defaults to None, which sets the instance to the current datetime.
- self._dt
The internal datetime representation of the input.
- Type:
datetime.datetime
- self._dt_info_dict
A dictionary containing parsed datetime information, defaults to None.
- Type:
dict | None
- get_dt_info_dict(language: LanguageEnum)[源代码]
Property method to get the dictionary containing parsed datetime information. If None, initialize using _parse_dt_info.
- 返回:
A dictionary with parsed datetime information.
- 返回类型:
dict
- classmethod extract_date_parts_cn(input_string: str) dict [源代码]
Extracts various components of a date (year, month, day, etc.) from an input string based on Chinese formats.
This method identifies year, month, day, weekday, and hour components within the input string based on predefined patterns. It supports relative terms like '每' (every) and translates weekday names into numeric representations.
- 参数:
input_string (str) -- The Chinese text containing date and time information.
- 返回:
- A dictionary with keys 'year', 'month', 'day', 'weekday', and 'hour',
each holding the corresponding extracted value. If a component is not found, it will not be included in the dictionary. For relative terms like '每' (every), the value is set to -1.
- 返回类型:
dict
- classmethod extract_date_parts_en(input_string: str) dict [源代码]
Extracts various components of a date (year, month, day, etc.) from an input string based on English formats.
This method employs regex patterns to identify and parse different date and time elements within the provided text. It supports extraction of year, month name, day, 12-hour and 24-hour time formats, and weekdays.
- 参数:
input_string (str) -- The English text containing date and time information.
- 返回:
A dictionary containing the extracted date parts with default values of -1 where components are not found. Keys include 'year', 'month', 'day', 'hour', 'minute', 'second', and 'weekday'.
- 返回类型:
dict
- classmethod extract_date_parts(input_string: str, language: LanguageEnum) dict [源代码]
Extracts various date components from the input string based on the current language context.
This method dynamically selects a language-specific function to parse the input string and extract date parts such as year, month, day, etc. If the function for current language context does not exist, a warning is logged and an empty dictionary is returned.
- 参数:
input_string (str) -- The string containing date information to be parsed.
language (str) -- current language.
- 返回:
A dictionary containing extracted date components, or an empty dictionary if parsing fails.
- 返回类型:
dict
- classmethod has_time_word_cn(query: str, datetime_word_list: List[str]) bool [源代码]
Check if the input query contains any datetime-related words based on the cn language context.
- 参数:
query (str) -- The input string to check for datetime-related words.
datetime_word_list (list[str]) -- datetime keywords
- 返回:
True if the query contains at least one datetime-related word, False otherwise.
- 返回类型:
bool
- classmethod has_time_word_en(query: str, datetime_word_list: List[str]) bool [源代码]
Check if the input query contains any datetime-related words based on the en language context.
- 参数:
query (str) -- The input string to check for datetime-related words.
datetime_word_list (list[str]) -- datetime keywords
- 返回:
True if the query contains at least one datetime-related word, False otherwise.
- 返回类型:
bool
- classmethod has_time_word(query: str, language: LanguageEnum) bool [源代码]
- datetime_format(dt_format: str = '%Y%m%d') str [源代码]
Format the stored datetime object into a string based on the provided format.
- 参数:
dt_format (str, optional) -- The datetime format string. Defaults to "%Y%m%d".
- 返回:
A formatted datetime string.
- 返回类型:
str
- string_format(string_format: str, language: LanguageEnum) str [源代码]
Format the datetime information stored in the instance using a custom string format.
- 参数:
string_format (str) -- A format string where placeholders are keys from dt_info_dict.
language (str) -- current language.
- 返回:
A formatted datetime string.
- 返回类型:
str
- property timestamp: int
Get the timestamp representation of the stored datetime.
- 返回:
A timestamp value.
- 返回类型:
int