Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
RobbertDM opened this issue Sep 3, 2024 · 4 comments
Open
2 tasks done

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

RobbertDM opened this issue Sep 3, 2024 · 4 comments

Comments

@RobbertDM
Copy link

  • 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?

@stripedpumpkin
Copy link

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).

@RobbertDM
Copy link
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.

@stripedpumpkin
Copy link

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.

@stripedpumpkin
Copy link

stripedpumpkin commented Oct 8, 2024

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants