turicas/rows

Handle timezones more efficiently

オープン

#345 opened on 2020/03/29

 (0 件のコメント) (0 件のリアクション) (0 人の担当者)Python (136 件のフォーク)github user discovery
bugenhancementhelp wantedrows.fields

Repository metrics

Stars
 (886 個のスター)
PR merge metrics
 (30d に merged PR はありません)

説明

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.

コントリビューターガイド