"""
Implements the :class:`ArrowFactory <arrow.factory.ArrowFactory>` class,
providing factory methods for common :class:`Arrow <arrow.arrow.Arrow>`
construction scenarios.
"""
import calendar from datetime import date, datetime from datetime import tzinfo as dt_tzinfo from decimal import Decimal from time import struct_time from typing import Any, List, Optional, Tuple, Type, Union, overload
from dateutil import tz as dateutil_tz
from arrow import parser from arrow.arrow import TZ_EXPR, Arrow from arrow.constants import DEFAULT_LOCALE from arrow.util import is_timestamp, iso_to_gregorian
class ArrowFactory: """A factory for generating :class:`Arrow ` objects.
:param type: (optional) the :class:`Arrow <arrow.arrow.Arrow>`-based class to construct from.
Defaults to :class:`Arrow <arrow.arrow.Arrow>`.
def get(self, *args: Any, **kwargs: Any) -> Arrow: """Returns an :class:`Arrow ` object based on flexible inputs.
:param locale: (optional) a ``str`` specifying a locale for the parser. Defaults to 'en-us'.
:param tzinfo: (optional) a :ref:`timezone expression <tz-expr>` or tzinfo object.
Replaces the timezone unless using an input form that is explicitly UTC or specifies
the timezone in a positional argument. Defaults to UTC.
:param normalize_whitespace: (optional) a ``bool`` specifying whether ornot to normalize
redundant whitespace (spaces, tabs, and newlines) in a datetime string before parsing.
Defaults to false.
# if kwargs given, send to constructor unless only tzinfo provided if len(kwargs) > 1:
arg_count = 3
# tzinfo kwarg is not provided if len(kwargs) == 1 and tz isNone:
arg_count = 3
# () -> now, @ tzinfo or utc if arg_count == 0: if isinstance(tz, str):
tz = parser.TzinfoParser.parse(tz) return self.type.now(tzinfo=tz)
if isinstance(tz, dt_tzinfo): return self.type.now(tzinfo=tz)
return self.type.utcnow()
if arg_count == 1:
arg = args[0] if isinstance(arg, Decimal):
arg = float(arg)
# (None) -> raises an exception if arg isNone: raise TypeError("Cannot parse argument of type None.")
# try (int, float) -> from timestamp @ tzinfo elifnot isinstance(arg, str) and is_timestamp(arg): if tz isNone: # set to UTC by default
tz = dateutil_tz.tzutc() return self.type.fromtimestamp(arg, tzinfo=tz)
# (Arrow) -> from the object's datetime @ tzinfo elif isinstance(arg, Arrow): return self.type.fromdatetime(arg.datetime, tzinfo=tz)
# (struct_time) -> from struct_time elif isinstance(arg, struct_time): return self.type.utcfromtimestamp(calendar.timegm(arg))
# (iso calendar) -> convert then from date @ tzinfo elif isinstance(arg, tuple) and len(arg) == 3:
d = iso_to_gregorian(*arg) return self.type.fromdate(d, tzinfo=tz)
else: raise TypeError(f"Cannot parse single argument of type {type(arg)!r}.")
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.