python - TypeError: can't compare offset-naive and offset-aware datetimes -
the server hosts db
in u.s
. when add item db
, want time stored using australia/sydney
time. regardless of country user in, if retrieve record, should returned using australia/sydney
time. doing this:
tz = pytz.timezone('australia/sydney') orderdate = datetime.now(tz) return add_order_to_db(orderid, orderdate, ordercost) # adds order object db
the issue arises when want update orderdate
above order
object.
tz = pytz.timezone('australia/sydney') orderdate = datetime.now(tz) contextlib.closing(dbsession()) session: try: existingorder = session.query(order).filter_by(ordersid=orderid).first() existingorder.ordersdate = orderdate session.commit() except exc.sqlalchemyerror, error: session.rollback() raise_database_error(error)
order object
class order(base): __tablename__ = 'orders' ordersid = column(integer, primary_key=true) ordersdate = column(datetime, nullable=false) orderscost = column(numeric(19, 4), nullable=false)
in mysql
, ordersdate
datetime
variable.
how retaining offset-naive nature of datetime object applying offset current utc time?
tz = pytz.timezone('australia/sydney') orderdate = datetime.datetime.now() + tz._utcoffset