Skip to content

pendulum.utcnow() sets tzinfo to TimezoneInfo object instead of Timezone #160

@bolkedebruin

Description

@bolkedebruin

I was basically using the following code (bit shortened):

n = pendulum.utcnow()

n = make_naive(n)
n = convert_to_utc(n)
n = make_naive(n) # error happens here

This got me the following error:

    dttm = timezone.make_naive(dttm, self.timezone)
  File "/Users/bolke/Documents/dev/airflow/airflow/utils/timezone.py", line 118, in make_naive
    o = value.astimezone(timezone)
  File "/Users/bolke/Documents/dev/airflow_env/lib/python2.7/site-packages/pendulum/tz/timezone_info.py", line 99, in fromutc
    tzinfo = self._tz._tzinfos[self._tz._transitions[idx]._tzinfo_index]

make_naive looks like this:

def make_naive(value, timezone=None):
    """
    Make an aware datetime.datetime naive in a given time zone.

    :param value: datetime
    :param timezone: timezone
    :return: naive datetime
    """
    if timezone is None:
        timezone = TIMEZONE

    # Emulate the behavior of astimezone() on Python < 3.6.
    if is_naive(value):
        raise ValueError("make_naive() cannot be applied to a naive datetime")

    o = value.astimezone(timezone)

    # cross library compatibility
    naive = dt.datetime(o.year,
                        o.month,
                        o.day,
                        o.hour,
                        o.minute,
                        o.second,
                        o.microsecond)

    return naive

It happens because pendulum.utcnow() sets the tzinfo to TimezoneInfo. When changing it to

def utcnow():
    """
    Get the current date and time in UTC
    :return:
    """
    d = dt.datetime.utcnow()
    d = d.replace(tzinfo=utc)

    return d

It gets fixed as now the tzinfo is set to a Timezone object.

Am I implementing this incorrectly or is this a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions