Skip to content

tz not taken into account when calling pendulum.instance #841

Open

Description

@RobbertDM
  • I am on the latest Pendulum version.
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • OS version and name: ubuntu 22.04
  • Pendulum version: 3.0.0

Issue

If you pass a DateTime that has timezone information, for example pendulum.now(), to pendulum.instance(), the tz= argument seems to do nothing.

Try for example the following:
pendulum.instance(pendulum.now(), tz="EST")
For me, that returns DateTime(2024, 9, 3, 13, 56, 35, 64854, tzinfo=Timezone('Europe/Brussels')) since I am in Brussels.

I would expect this tz= argument for instance to make sure the returned object is in that timezone. If not, what is it used for?

Activity

  1. stripedpumpkin commented on Oct 8, 2024

    @stripedpumpkin

    I think what you need is

    pendulum.now().in_timezone('Europe/Brussels')
    

    or even if actually using #now you can do directly

    pendulum.now(tz='Europe/Brussels')
    

    The instance method is I believe more intended for datetime.datetime conversion (into pendulum.Datetime).

  2. RobbertDM commented on Oct 8, 2024

    @RobbertDM
    Author

    Yeah we already have our fix, I just wanted to report this behavior by the instance method, because I don't understand why there is a tz argument if it's not used? This caused a bug earlier in our code because we thought the pendulum.Datetime object that would come out of pendulum.instance(dt, tz='EST') would've been one with tzinfo=Timezone('EST') but it's not.

  3. stripedpumpkin commented on Oct 8, 2024

    @stripedpumpkin

    My best guess is that pendulum.instance expects a datetime.datetime with a tzinfo attributes, not a pendulum.Datetime, as the point of #instance is to get a pendulum.Datetime, from a datetime.datetime I think.

  4. stripedpumpkin commented on Oct 8, 2024

    @stripedpumpkin

    Ah ok here is what happens:

        @classmethod
        def instance(
            cls,
            dt: datetime.datetime,
            tz: str | Timezone | FixedTimezone | datetime.tzinfo | None = UTC,
        ) -> Self:
            tz = dt.tzinfo or tz
    
            if tz is not None:
                tz = pendulum._safe_timezone(tz, dt=dt)
    

    If you pass a pendulum DateTime as argument it will have a tzinfo, so this will take priority over the one you injected. But again I don't think this method is intended to perform time zone conversions as discussed above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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