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

Handle timezones more efficiently #345

Open
turicas opened this issue Mar 29, 2020 · 0 comments
Open

Handle timezones more efficiently #345

turicas opened this issue Mar 29, 2020 · 0 comments

Comments

@turicas
Copy link
Owner

turicas commented Mar 29, 2020

I've run into some problems serializing/deserializing datetime objects with timezones - in the end came up the this DIY solution:

import pytz

TIMEZONE_UTC = pytz.utc
TIMEZONE_BRAZIL = pytz.timezone("America/Sao_Paulo")

class BrazilianDatetimeField(rows.fields.DatetimeField):
    @classmethod
    def deserialize(cls, value):
        value = str(value or "").strip()
        if not value:
            return None
        dt = datetime.datetime.fromisoformat(value)
        if dt.tzinfo is None:  # Force naive to be UTC
            dt = TIMEZONE_UTC.localize(dt)
        dt = dt.astimezone(TIMEZONE_BRAZIL)
        return dt

    @classmethod
    def serialize(cls, value):
        if value is None:
            return ""
        return value.isoformat()

It'd be awesome if rows could handle timezones in more standard way.

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

No branches or pull requests

1 participant