使用 PYTZ 添加和返回 UTC 时间

Adding and returning UTC time using PYTZ

当我将 order 添加到我的 DB 时,我想将时间包括为 UTC

ts = time.time()
utc = pytz.utc
orderDateUTC = datetime.utcfromtimestamp(ts).replace(tzinfo=utc)

return add_order_to_db(tableID, userID, restaurantID, orderDateUTC, orderCost, orderStatus, orderItems)

当我将 date 添加到 DB 时,我将其添加为 UTC 然后尝试将其转换回 local time 就在 [=53 之前=]发送给用户。

add_order_to_db(...)

def add_order_to_db(tableID, userID, restaurantID, orderDateUTC, orderCost, orderStatus, orderItems):

    receivedOrder = ORDER(tableID, userID, restaurantID, orderDateUTC, orderCost, orderStatus)
    with contextlib.closing(DBSession()) as session:
        try:
           session.add(receivedOrder)
           session.commit()
        except exc.SQLAlchemyError, error:
            session.rollback()
            raise_database_error(error)
        else:
            au_tz = timezone('Australia/Sydney')
            au_dt = au_tz.normalize(orderDateUTC.astimezone(au_tz))
            receivedOrder.ORDERSDATE = au_dt
            order = Order(receivedOrder, 'true')
            return jsonify(data=order.serialize())

序列化()

   def serialize(self):                   
            return {                    
                'orderDate' : self.ORDERSDATE                                                                                                                                 
                }

ORDERSDATE 定义为:

ORDERSDATE = Column(DateTime, nullable=False)

问题是 time 没有正确转换回来。

returned outputDB中的数据是:orderDate: "Mon, 06 Apr 2015 01:13:00 GMT"

但是我需要 returned outputorderDate: "Mon, 06 Apr 2015 11:13:00 GMT"

我发现如果我 return return str(au_dt),我会得到正确的输出,但格式不同:2015-04-06 11:13:37.461000+10:00

However I need the returned output to be orderDate: "Mon, 06 Apr 2015 11:13:00 GMT"

这是不正确的。如果满足以下条件,则不需要它:

两者

The returned output and the data in the DB is: orderDate: "Mon, 06 Apr 2015 01:13:00 GMT"

I find that if I return return str(au_dt), I get the correct output but in a different form: 2015-04-06 11:13:37.461000+10:00

是否相同时间:

local time = utc time + utc offset
     11:13 =    01:13 + 10