scrapinghub / dateparser

python parser for human readable dates
BSD 3-Clause "New" or "Revised" License
2.56k stars 465 forks source link

'two weeks ago' at 0:00 UTC #1230

Closed tisonkun closed 5 months ago

tisonkun commented 5 months ago

Currently, two weeks ago goes back to two weeks ago at the current moment. But I may prefer to align with 0:00.

Is it possible to achieve?

Gallaecio commented 5 months ago

Does RELATIVE_BASE work?

tisonkun commented 5 months ago

@Gallaecio Sounds like I should calculate the start of today and use it for this setting?

tisonkun commented 5 months ago

@Gallaecio Thank you! I got something like this works:

    start_of_today = datetime.combine(datetime.now(UTC), time(0, 0, 0, 0, UTC))
    date_since = dateparser.parse(
        os.getenv('DATE_SINCE') or '2 weeks ago',
        settings={
            'RETURN_AS_TIMEZONE_AWARE': True,
            'RELATIVE_BASE': start_of_today,
        })
    date_until = dateparser.parse(
        os.getenv('DATE_UNTIL') or 'now',
        settings={
            'RETURN_AS_TIMEZONE_AWARE': True,
            'RELATIVE_BASE': start_of_today,
        })
    print(f"Date since: {date_since}")
    print(f"Date until: {date_until}")