由于数据库中所有日期数据我存储是utc时间(unix timestamp with timezone),而用户大部分为非UTC时区用户,所以查询当天所有数据,指定日期所有数据少有些许繁琐。
查询当日内所有数据:
def check_order(): sql = """ SELECT * FROM table_name WHERE (DATE_PART('day', (order_date::TIMESTAMP AT TIME ZONE 'Asia/Tokyo') - (now()::TIMESTAMP AT TIME ZONE 'Asia/Tokyo')) = 0 ) ; """ data = pgsql.selectAll(sql) return data
查询指定日期所有数据:
def check_order(date): sql = """ SELECT * FROM table_name WHERE (DATE_PART('day', (order_date::TIMESTAMP AT TIME ZONE 'Asia/Tokyo') - ('%s'::DATE AT TIME ZONE 'Asia/Tokyo')) = 0 ) ; """ % date data = pgsql.selectAll(sql) return data
返回的date也是utc时间,如果需要显示指定时区时间可以:
SELECT xxx,xxxx,order_date AT TIME ZONE 'Asia/Tokyo' FROM table_name