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)[源代码]

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')[源代码]

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)[源代码]

Convert timestamp to datetime param: timestamp timestamp param: string_format (str): string format Return: formatted_time (Str): timestamp

backtrader.utils.dateintern.timestamp2datestr(timestamp)[源代码]

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')[源代码]

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')[源代码]

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')[源代码]

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')[源代码]

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)[源代码]

Parse a timezone specification into a tzinfo object.

参数:

tz -- Timezone specification (string, tzinfo object, or None).

返回:

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)[源代码]

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.

参数:

tz -- Timezone object to add localize method to.

返回:

The same timezone object with a localize method added.

backtrader.utils.dateintern.num2date(x, tz=None, naive=True)[源代码]

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)[源代码]

Convert numeric date to date object.

参数:
  • 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.

返回:

Date object extracted from the datetime.

返回类型:

date

backtrader.utils.dateintern.num2time(num, tz=None, naive=True)[源代码]

Convert numeric date to time object.

参数:
  • 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.

返回:

Time object extracted from the datetime.

返回类型:

time

backtrader.utils.dateintern.date2num(dt, tz=None)[源代码]

Convert: mod:datetime to the Gregorian date as UTC float days, preserving hours, minutes, seconds and microseconds. Return value is a: func:float.

backtrader.utils.dateintern.time2num(tm)[源代码]

Converts the hour/minute/second/microsecond part of tm (datetime.datetime or time) to a num