Skip to content

Add failing test for to_representation with explicit default timezone #5821

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

Conversation

hakib
Copy link

@hakib hakib commented Feb 8, 2018

See discussion here:
#5435 (comment)

Note: Before submitting this pull request, please review our contributing guidelines.

Description

Please describe your pull request. If it fixes a bug or resolves a feature request, be sure to link to that issue. When linking to an issue, please use refs #... in the description of the pull request.

Copy link
Collaborator

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. This looks like a different issue. At first glance I’d think you want be setting the active time zone, rather than trying to set it on the field...

@hakib
Copy link
Author

hakib commented Feb 8, 2018

I’d think you want be setting the active time zone, rather than trying to set it on the field...

I haven't considered setting the timezone - it might have been easier than replacing DateTimeField with a SerializerMethodField.

Either way I think the current behaviour is unexpected.

I think it's reasonable to expect this snippet:

field = serializers.DateTimeField(
    default_timezone=pytz.timezone('Asia/Kolkata'),
    format=''%d/%m/%Y %H:%M'
)

to produce the date in Asia/Kolkata and not UTC as it is now.

@xordoquy
Copy link
Collaborator

xordoquy commented Feb 8, 2018

Current behavior is consistent with Django's
DRF documentation should probably add a link to it but I don't see this as an unexpected behavior.

@hakib
Copy link
Author

hakib commented Feb 8, 2018

Current behavior is consistent with Django's

Fair enough.

I'll close the PR.

@hakib hakib closed this Feb 8, 2018
@hakib
Copy link
Author

hakib commented Feb 8, 2018

Following @carltongibson advice, is this also exepcted behaviour:

def test_should_render_date_time_in_active_timezone(self):
    field = serializers.DateTimeField(default_timezone=self.kolkata, format=self.date_format)
    dt = datetime.datetime(2018, 2, 8, 14, 15, 16, tzinfo=pytz.utc)

    with timezone.override(self.kolkata):
        rendered_date = field.to_representation(dt)

    rendered_date_in_timezone = dt.astimezone(self.kolkata).strftime(self.date_format)

    assert rendered_date == rendered_date_in_timezone

The rendered date is not in the current timezone (kolkata):

E   AssertionError: assert '08/02/2018 14:15' == '08/02/2018 19:45'
E     - 08/02/2018 14:15
E     ?              --
E     + 08/02/2018 19:45
E     ?             ++

@xordoquy
Copy link
Collaborator

xordoquy commented Feb 8, 2018

It looks like the timezone is only enforced for ISO 8601.
Doesn't look close to what I said previously :(
Not even sure why it's not enforced in any case.

@carltongibson carltongibson reopened this Feb 8, 2018
@carltongibson
Copy link
Collaborator

@hakib Can you push that test as is. We'll have a look.

@hakib
Copy link
Author

hakib commented Feb 8, 2018

It looks like the timezone is only enforced for ISO 8601.

That's exactly the point - right now it's not possible to provide a format other than ISO-8601 and have the date render in a timezone other than the default one.

Just to clarify on the use case here, i'm usually working with UTC so I never noticed it. In this particular scenario I had a custom renderer aimed at a printer so I needed to make sure the date is rendered at the client's local time.

@carltongibson
Copy link
Collaborator

Closing in favour of #5833. Thanks @hakib!

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

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy