Ejemplo n.º 1
0
    def fromtimestamp(cls, timestamp, tzinfo=None):
        """ Constructs an :class:`Arrow <arrow.arrow.Arrow>` object from a timestamp, converted to
        the given timezone.

        :param timestamp: an ``int`` or ``float`` timestamp, or a ``str`` that converts to either.
        :param tzinfo: (optional) a ``tzinfo`` object.  Defaults to local time.

        Timestamps should always be UTC. If you have a non-UTC timestamp::

            >>> arrow.Arrow.utcfromtimestamp(1367900664).replace(tzinfo='US/Pacific')
            <Arrow [2013-05-07T04:24:24-07:00]>

        """

        if tzinfo is None:
            tzinfo = dateutil_tz.tzlocal()

        if not util.is_timestamp(timestamp):
            raise ValueError(
                "The provided timestamp '{}' is invalid.".format(timestamp))

        dt = util.safe_fromtimestamp(float(timestamp), tzinfo)

        return cls(
            dt.year,
            dt.month,
            dt.day,
            dt.hour,
            dt.minute,
            dt.second,
            dt.microsecond,
            dt.tzinfo,
        )
Ejemplo n.º 2
0
    def _build_datetime(parts):

        timestamp = parts.get("timestamp")

        if timestamp is not None:
            return util.safe_fromtimestamp(timestamp, tz=tz.tzutc())

        expanded_timestamp = parts.get("expanded_timestamp")

        if expanded_timestamp is not None:

            if expanded_timestamp > MAX_TIMESTAMP:
                if expanded_timestamp < MAX_TIMESTAMP_MS:
                    expanded_timestamp /= 1000.0
                elif expanded_timestamp < MAX_TIMESTAMP_US:
                    expanded_timestamp /= 1000000.0
                else:
                    raise ValueError(
                        "The specified timestamp '{}' is too large.".format(
                            expanded_timestamp))

            return util.safe_fromtimestamp(expanded_timestamp, tz=tz.tzutc())

        day_of_year = parts.get("day_of_year")

        if day_of_year is not None:
            year = parts.get("year")
            month = parts.get("month")
            if year is None:
                raise ParserError(
                    "Year component is required with the DDD and DDDD tokens.")

            if month is not None:
                raise ParserError(
                    "Month component is not allowed with the DDD and DDDD tokens."
                )

            date_string = "{}-{}".format(year, day_of_year)
            try:
                dt = datetime.strptime(date_string, "%Y-%j")
            except ValueError:
                raise ParserError(
                    "The provided day of year '{}' is invalid.".format(
                        day_of_year))

            parts["year"] = dt.year
            parts["month"] = dt.month
            parts["day"] = dt.day

        am_pm = parts.get("am_pm")
        hour = parts.get("hour", 0)

        if am_pm == "pm" and hour < 12:
            hour += 12
        elif am_pm == "am" and hour == 12:
            hour = 0

        # Support for midnight at the end of day
        if hour == 24:
            if parts.get("minute", 0) != 0:
                raise ParserError(
                    "Midnight at the end of day must not contain minutes")
            if parts.get("second", 0) != 0:
                raise ParserError(
                    "Midnight at the end of day must not contain seconds")
            if parts.get("microsecond", 0) != 0:
                raise ParserError(
                    "Midnight at the end of day must not contain microseconds")
            hour = 0
            day_increment = 1
        else:
            day_increment = 0

        # account for rounding up to 1000000
        microsecond = parts.get("microsecond", 0)
        if microsecond == 1000000:
            microsecond = 0
            second_increment = 1
        else:
            second_increment = 0

        increment = timedelta(days=day_increment, seconds=second_increment)

        return (datetime(
            year=parts.get("year", 1),
            month=parts.get("month", 1),
            day=parts.get("day", 1),
            hour=hour,
            minute=parts.get("minute", 0),
            second=parts.get("second", 0),
            microsecond=microsecond,
            tzinfo=parts.get("tzinfo"),
        ) + increment)