memoryscope.core.utils.datetime_handler

class memoryscope.core.utils.datetime_handler.DatetimeHandler(dt: datetime | str | int | float | None = None)[source]

Bases: 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)[source]

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.

Parameters:

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)[source]

Property method to get the dictionary containing parsed datetime information. If None, initialize using _parse_dt_info.

Returns:

A dictionary with parsed datetime information.

Return type:

dict

classmethod extract_date_parts_cn(input_string: str) dict[source]

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.

Parameters:

input_string (str) – The Chinese text containing date and time information.

Returns:

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.

Return type:

dict

classmethod extract_date_parts_en(input_string: str) dict[source]

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.

Parameters:

input_string (str) – The English text containing date and time information.

Returns:

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’.

Return type:

dict

classmethod extract_date_parts(input_string: str, language: LanguageEnum) dict[source]

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.

Parameters:
  • input_string (str) – The string containing date information to be parsed.

  • language (str) – current language.

Returns:

A dictionary containing extracted date components, or an empty dictionary if parsing fails.

Return type:

dict

classmethod has_time_word_cn(query: str, datetime_word_list: List[str]) bool[source]

Check if the input query contains any datetime-related words based on the cn language context.

Parameters:
  • query (str) – The input string to check for datetime-related words.

  • datetime_word_list (list[str]) – datetime keywords

Returns:

True if the query contains at least one datetime-related word, False otherwise.

Return type:

bool

classmethod has_time_word_en(query: str, datetime_word_list: List[str]) bool[source]

Check if the input query contains any datetime-related words based on the en language context.

Parameters:
  • query (str) – The input string to check for datetime-related words.

  • datetime_word_list (list[str]) – datetime keywords

Returns:

True if the query contains at least one datetime-related word, False otherwise.

Return type:

bool

classmethod has_time_word(query: str, language: LanguageEnum) bool[source]
datetime_format(dt_format: str = '%Y%m%d') str[source]

Format the stored datetime object into a string based on the provided format.

Parameters:

dt_format (str, optional) – The datetime format string. Defaults to “%Y%m%d”.

Returns:

A formatted datetime string.

Return type:

str

string_format(string_format: str, language: LanguageEnum) str[source]

Format the datetime information stored in the instance using a custom string format.

Parameters:
  • string_format (str) – A format string where placeholders are keys from dt_info_dict.

  • language (str) – current language.

Returns:

A formatted datetime string.

Return type:

str

property timestamp: int

Get the timestamp representation of the stored datetime.

Returns:

A timestamp value.

Return type:

int