backtrader.utils.dateintern module¶
Date Internal Module - Date/time conversion and timezone utilities.
This module provides internal utilities for date/time conversions, timezone handling, and numeric date representations used throughout backtrader.
- Classes:
_UTC: UTC timezone implementation. _LocalTimezone: Local timezone with DST support.
- Functions:
tzparse: Parse timezone specification. Localizer: Add localize method to timezone objects. num2date: Convert numeric date to datetime. num2dt: Convert numeric date to date. num2time: Convert numeric date to time. date2num: Convert datetime to numeric format. time2num: Convert time to numeric format.
- Constants:
UTC: Singleton UTC timezone instance. TZLocal: Singleton local timezone instance. TIME_MAX: Maximum time value (23:59:59.999990). TIME_MIN: Minimum time value (00:00:00).
- backtrader.utils.dateintern.get_last_timeframe_timestamp(timestamp, time_diff)[source]¶
Get previous whole minute timestamp based on current timestamp :params timestamp int, calculate from int(time.time()) :params time_diff int, e.g. 1m timeframe using 60 :returns timestamp int
- backtrader.utils.dateintern.get_string_tz_time(tz='Asia/Singapore', string_format='%Y-%m-%d %H:%M:%S.%f')[source]¶
generate string timezone datetime in particular timezone param: tz (str): timezone in pytz.common_timezones param: string_format (str): string format
Return: now (String): timestamp
- backtrader.utils.dateintern.timestamp2datetime(timestamp)[source]¶
Convert timestamp to datetime param: timestamp timestamp param: string_format (str): string format Return: formatted_time (Str): timestamp
- backtrader.utils.dateintern.timestamp2datestr(timestamp)[source]¶
Convert timestamp to string time param: timestamp timestamp param: string_format (str): string format Return: formatted_time (Str): timestamp
- backtrader.utils.dateintern.datetime2timestamp(time_date, string_format='%Y-%m-%d %H:%M:%S.%f')[source]¶
Convert datetime to timestamp param: datetime_string (str): timezone in pytz.common_timezones param: string_format (str): string format Return: timestamp
- backtrader.utils.dateintern.datestr2timestamp(datetime_string='2023-06-01 09:30:00.0', string_format='%Y-%m-%d %H:%M:%S.%f')[source]¶
Convert datetime to timestamp param: datetime_string (str): timezone in pytz.common_timezones param: string_format (str): string format Return: timestamp
- backtrader.utils.dateintern.str2datetime(datetime_string='2023-06-01 09:30:00.0', string_format='%Y-%m-%d %H:%M:%S.%f')[source]¶
Convert string format time to datetime param: datetime_string (str): timezone in pytz.common_timezones param: string_format (str): string format Return: datetime
- backtrader.utils.dateintern.datetime2str(datetime_obj, string_format='%Y-%m-%d %H:%M:%S.%f')[source]¶
Convert datetime to string format time param: datetime_obj (datetime): timezone in pytz.common_timezones param: string_format (str): string format Return: datetime_str
- backtrader.utils.dateintern.tzparse(tz)[source]¶
Parse a timezone specification into a tzinfo object.
- Parameters:
tz – Timezone specification (string, tzinfo object, or None).
- Returns:
A tzinfo object. If pytz is available and tz is a string, returns the corresponding pytz timezone. Otherwise returns a Localizer-wrapped tz object.
- backtrader.utils.dateintern.Localizer(tz)[source]¶
Add a localize method to a timezone object.
This function adds a localize method to tz objects that don’t have one, allowing consistent timezone localization across different timezone implementations.
- Parameters:
tz – Timezone object to add localize method to.
- Returns:
The same timezone object with a localize method added.
- backtrader.utils.dateintern.num2date(x, tz=None, naive=True)[source]¶
x is a float value that gives the number of days (fraction part represents hours, minutes, seconds) since 0001-01-01 00:00:00 UTC plus one. The addition of one here is a historical artifact. Also, note that the Gregorian calendar is assumed; this is not universal practice. For details, see the module docstring. Return value is a: class:datetime instance in timezone tz (default to rcparams TZ value). If x is a sequence, a sequence of: class:datetime objects will be returned.
- backtrader.utils.dateintern.num2dt(num, tz=None, naive=True)[source]¶
Convert numeric date to date object.
- Parameters:
num – Numeric date value (days since 0001-01-01 UTC + 1).
tz – Timezone for the result (optional).
naive – If True, return naive date without timezone info.
- Returns:
Date object extracted from the datetime.
- Return type:
date
- backtrader.utils.dateintern.num2time(num, tz=None, naive=True)[source]¶
Convert numeric date to time object.
- Parameters:
num – Numeric date value (days since 0001-01-01 UTC + 1).
tz – Timezone for the result (optional).
naive – If True, return naive time without timezone info.
- Returns:
Time object extracted from the datetime.
- Return type:
time