使用带有mysql unix时间戳的sqlalchemy定义表

rgz*_*rgz 3 python mysql sqlalchemy

背景,有几种方法可以在MySQ中存储日期.

  1. 作为字符串,例如"09/09/2009".
  2. 作为使用函数UNIX_TIMESTAMP()的整数,这应该是传统的unix时间表示(你知道自纪元加上/减去闰秒后的秒数).
  3. 作为MySQL TIMESTAMP,mysql特定的数据类型与unix时间戳不同.
  4. 作为MySQL Date字段,另一种mysql特定的数据类型.

    不要将案例2与案例3(或案例4)混淆是非常重要的.我有一个带有整数日期字段的现有表(案例2)如何在sqlalchemy中以一种我不必访问mysql的"FROM_UNIXTIME"函数的方式定义它?

    对于记录,只使用sqlalchemy.types.DateTime并希望它在检测到整数列时做正确的事情不起作用,它适用于时间戳字段和日期字段.

Pav*_*pin 6

我认为您展示的类型装饰器存在一些问题.

  1. impl应该sqlalchemy.types.Integer而不是DateTime.
  2. 装饰器应该允许可以为空的列.

这就是我的想法:


import datetime, time
from sqlalchemy.types import TypeDecorator, DateTime, Integer

class IntegerDateTime(TypeDecorator):
    """a type that decorates DateTime, converts to unix time on
    the way in and to datetime.datetime objects on the way out."""
    impl = Integer # In schema, you want these datetimes to
                   # be stored as integers.
    def process_bind_param(self, value, _):
        """Assumes a datetime.datetime"""
        if value is None:
            return None # support nullability
        elif isinstance(value, datetime.datetime):
            return int(time.mktime(value.timetuple()))
        raise ValueError("Can operate only on datetime values. "
                         "Offending value type: {0}".format(type(value).__name__))
    def process_result_value(self, value, _):
        if value is not None: # support nullability
            return datetime.datetime.fromtimestamp(float(value))
Run Code Online (Sandbox Code Playgroud)