Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix date and datetime filters #123

Open
KhaledBousrih opened this issue Feb 24, 2022 · 0 comments
Open

Fix date and datetime filters #123

KhaledBousrih opened this issue Feb 24, 2022 · 0 comments

Comments

@KhaledBousrih
Copy link
Collaborator

KhaledBousrih commented Feb 24, 2022

Description of the problem

Concrete range / comparaison filters use a method convert_type that takes an argument close_period (defaults to True):

For DateTime fields, if close_period is true, we consider the end of the day, otherwise we consider the start of the day.

Example:

convert_type("2022-02-24T15:50:00Z", "DateTimeField", close_period=True)  # 2022-02-24T00:00:00Z
convert_type("2022-02-24T15:50:00Z", "DateTimeField", close_period=False)  # 2022-02-24T23:59:59Z

If we have a model MyModel with field datetime, and two instances of this model:

  • instance1: datetime = 2022-02-24T18:45:12.1653Z
  • instance2: datetime = 2022-02-24T20:02:33.233Z

If we apply the API filter: ?datetime__range=2022-02-24T19:00:00Z,2022-02-24T22:00:00Z, we expect to get the instance 2 in the results of the request, but we have both results, because what is actually done is a datetime__range=2022-02-24T00:00:00Z,2022-02-24T23:59:59Z

Fix

⚠️ This issue should be handeled first #124

Supported formats for DateTime filters

  • YYYY-MM-DD: (regex: r'^\d{4}-\d{2}-\d{2}$')
  • YYYY-MM-DDTHH:mm:ssZ: (regex: r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$')
  • YYYY-MM-DDTHH:mm:ss.xxxxxxZ: (regex: r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,6})?Z$')

Possible filters

Comparaison filters

lookup \ date format YYYY-MM-DD YYYY-MM-DDTHH:mm:ssZ YYYY-MM-DDTHH:mm:ss.xxxxxxZ
__gte dt.start_of('day') dt.start_of('second') dt
__gt dt.end_of('day') dt.end_of('second') dt
__lte dt.end_of('day') dt.end_of('second') dt
__lt dt.start_of('day') dt.start_of('second') dt

Range filters

For the range filter: __range=datetime_min,datetime_max:

value \ date format YYYY-MM-DD YYYY-MM-DDTHH:mm:ssZ YYYY-MM-DDTHH:mm:ss.xxxxxxZ
datetime_min dt.start_of('day') dt.start_of('second') dt
datetime_max dt.end_of('day') dt.end_of('second') dt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant