【问题标题】:How to filter data by comparing normal date to datetime with timezone?如何通过将正常日期与日期时间与时区进行比较来过滤数据?
【发布时间】:2020-05-21 10:50:42
【问题描述】:

我有一些数据存储在数据库中,我正在通过烧瓶和烧瓶-sqlalchemy 访问和过滤这些数据。

我正在尝试根据startend 日期过滤数据,这些日期将作为来自网址中request.args.get() 的用户输入。在数据库中,数据为"2020-05-12T00:00:00+02:00" 格式,当用户输入日期时,他将只输入日期而不是时间或时区(例如:2020-05-12)。因此,根据用户为startend(包括end)输入的值,我想比较日期并返回值。但是,返回的值必须具有原始日期格式

数据如下:

{
    "items": [
        {
            "id": 1,
            "value": 21.4,
            "start": "2020-05-12T00:00:00+02:00",
            "end": "2020-05-12T01:00:00+02:00",
            "country_code": "DE",
            "price_type": "DAY_AHEAD"
        },
        {
            "id": 2,
            "value": 18.93,
            "start": "2020-05-12T01:00:00+02:00",
            "end": "2020-05-12T02:00:00+02:00",
            "country_code": "DE",
            "price_type": "DAY_AHEAD"
        },
        {
            "id": 3,
            "value": 18.06,
            "start": "2020-05-13T02:00:00+02:00",
            "end": "2020-05-13T03:00:00+02:00",
            "country_code": "DE",
            "price_type": "DAY_AHEAD"
        },
        {
            "id": 4,
            "value": 17.05,
            "start": "2020-05-13T03:00:00+02:00",
            "end": "2020-05-13T04:00:00+02:00",
            "country_code": "DE",
            "price_type": "DAY_AHEAD"
        },
        {
            "id": 5,
            "value": 16.84,
            "start": "2020-05-14T04:00:00+02:00",
            "end": "2020-05-14T05:00:00+02:00",
            "country_code": "DE",
            "price_type": "DAY_AHEAD"
        },
        {
            "id": 6,
            "value": 19.3,
            "start": "2020-05-14T05:00:00+02:00",
            "end": "2020-05-14T06:00:00+02:00",
            "country_code": "DE",
            "price_type": "DAY_AHEAD"
        }]}

示例:

如果:

start2020-05-12 & end2020-05-14,从以上数据来看,必须返回以下数据:

{
    "items": [
        {
            "id": 1,
            "value": 21.4,
            "start": "2020-05-12T00:00:00+02:00",
            "end": "2020-05-12T01:00:00+02:00",
            "country_code": "DE",
            "price_type": "DAY_AHEAD"
        },
        {
            "id": 2,
            "value": 18.93,
            "start": "2020-05-12T01:00:00+02:00",
            "end": "2020-05-12T02:00:00+02:00",
            "country_code": "DE",
            "price_type": "DAY_AHEAD"
        },
        {
            "id": 3,
            "value": 18.06,
            "start": "2020-05-13T02:00:00+02:00",
            "end": "2020-05-13T03:00:00+02:00",
            "country_code": "DE",
            "price_type": "DAY_AHEAD"
        },
        {
            "id": 4,
            "value": 17.05,
            "start": "2020-05-13T03:00:00+02:00",
            "end": "2020-05-13T04:00:00+02:00",
            "country_code": "DE",
            "price_type": "DAY_AHEAD"
        },
        {
            "id": 5,
            "value": 16.84,
            "start": "2020-05-14T04:00:00+02:00",
            "end": "2020-05-14T05:00:00+02:00",
            "country_code": "DE",
            "price_type": "DAY_AHEAD"
        }]}

【问题讨论】:

    标签: python python-3.x datetime flask flask-sqlalchemy


    【解决方案1】:

    如果您从request.args.get() 获取值如下:

    start_date = request.args.get('start')
    end_date = request.args.get('end')
    

    然后您可以使用dateutil.parser.parse 将您的日期解析为isoformat(),然后对其进行过滤:

    start_date = dateutil.parser.parse(filters['start']).isoformat()
    end_date = dateutil.parser.parse(filters['end']).isoformat()
    items = YourModel.query.filter(YourModel.start >= start_date).filter(YourModel.end <= end_date)
    

    【讨论】:

      猜你喜欢
      • 2014-05-03
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多