U
vi @ sd d Z ddlZddlmZ ddlZddlmZ ddlmZ da e
ZG dd dZedd d
Z
dS )z)Utils to get right Date parsing function. N)version_infotimezone)parserc @ sL e Zd ZdZejfejddddZe dddZ
d d
Zeddd
ZdS )
DateHelperax
DateHelper to groups different implementations of date operations.
If you would like to serialize the query results to custom timezone, you can use following code:
.. code-block:: python
from influxdb_client.client.util import date_utils
from influxdb_client.client.util.date_utils import DateHelper
import dateutil.parser
from dateutil import tz
def parse_date(date_string: str):
return dateutil.parser.parse(date_string).astimezone(tz.gettz('ETC/GMT+2'))
date_utils.date_helper = DateHelper()
date_utils.date_helper.parse_date = parse_date
N)r returnc C s
|| _ dS )z
Initialize defaults.
:param timezone: Default timezone used for serialization "datetime" without "tzinfo".
Default value is "UTC".
Nr )selfr r W/opt/alt/python38/lib/python3.8/site-packages/influxdb_client/client/util/date_utils.py__init__" s zDateHelper.__init__)date_stringc C s dS )z
Parse string into Date or Timestamp.
:return: Returns a :class:`datetime.datetime` object or compliant implementation
like :class:`class 'pandas._libs.tslibs.timestamps.Timestamp`
Nr )r r r r r
parse_date+ s zDateHelper.parse_datec C s. |j d d }|jd }|jd }|| | S )z
Get number of nanoseconds in timedelta.
Solution comes from v1 client. Thx.
https://github.com/influxdata/influxdb-python/pull/811
iQ i ʚ;i )dayssecondsmicroseconds)r deltaZnanoseconds_in_daysZnanoseconds_in_secondsZnanoseconds_in_microsr r r
to_nanoseconds4 s
zDateHelper.to_nanoseconds)valuec C s* |j s| |j| jdS |tjS dS )zt
Convert datetime to UTC timezone.
:param value: datetime
:return: datetime in UTC
)tzinfoN)r to_utcreplacer
astimezonetzutc)r r r r r
r A s zDateHelper.to_utc)
__name__
__module____qualname____doc__r r datetimer r strr
r r r r r r
r s
r )r c C s| t dkrxtf t dkrnt } zddl}|j| _W n8 tk
rh tjtj fdkr\t
j
j| _ntj
| _Y nX | a W 5 Q R X t S )z
Return DateHelper with proper implementation.
If there is a 'ciso8601' than use 'ciso8601.parse_datetime' else
use 'datetime.fromisoformat' (Python >= 3.11) or 'dateutil.parse' (Python < 3.11).
Nr ) )date_helperlock_r ciso8601Zparse_datetimer
ModuleNotFoundErrorr majorminorr
fromisoformatr parse)Z_date_helperr$ r r r
get_date_helperN s r* )r r sysr threadingr r Zdateutilr r" Lockr# r r* r r r r