turicas/rows

Handle timezones more efficiently

開放

#345 建立於 2020年3月29日

 (0 則留言) (0 個反應) (0 位負責人)Python (136 個分叉)github user discovery
bugenhancementhelp wantedrows.fields

倉庫指標

星標
 (886 顆星)
PR 合併指標
 (30 天內沒有已合併 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.

貢獻者指南