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

Question: how to use .between() for date interval? #407

Closed
circulon opened this issue Mar 6, 2021 · 10 comments
Closed

Question: how to use .between() for date interval? #407

circulon opened this issue Mar 6, 2021 · 10 comments
Labels
enhancement A feature that exists, works as intended but needs to be improved

Comments

@circulon
Copy link
Contributor

circulon commented Mar 6, 2021

This is a general "how to" question

I was looking to use .between() for a date interval eg low: today - 7 days, high: date today.
Currently I'm using postgres so the where_raw looks like this

    .where_raw("\"{}\".\"updated_at\" BETWEEN current_date - integer '7' AND current_date".format(table_a))

which is very grammar specific.
is there a more db agnostic way to do this?

Happy to discuss
Thanks

@circulon circulon added the enhancement A feature that exists, works as intended but needs to be improved label Mar 6, 2021
@josephmancuso
Copy link
Member

josephmancuso commented Mar 6, 2021

Can't you just use the between and pass 2 dates? Masonite ORM installs with pendulum so it would look something like:

Invoice.between('created_at', pendulum.now().subtract(days=7).to_datetime_string(), pendulum.now().to_datetime_string())

@Marlysson
Copy link
Contributor

@josephmancuso There some method to simulate dates greater or lower than some point?

@josephmancuso
Copy link
Member

@Marlysson what do you mean? for pendulum?

@josephmancuso
Copy link
Member

@Marlysson
Copy link
Contributor

No, in database level. Like this https://docs.djangoproject.com/en/3.1/ref/models/querysets/#year

@josephmancuso
Copy link
Member

josephmancuso commented Mar 7, 2021

@Marlysson

User.where('created_at', '<=', pendulum.now().subtract(days=7).to_datetime_string())

@circulon
Copy link
Contributor Author

circulon commented Mar 7, 2021

Let me clarify

Consider this scenario:
A server is running a db engine in Sydney
The timezone for the server was never changed so its AEDT.
The database timezone is set for UTC to force a standard.
All Date columns are DateTime's ie include timezone data.

Another server runs the webserver is the US for example.

Won't generating a pendulum date on the webserver be transformed into an incorrect date when sent to the database?
Hence I am currently doing this on the db side prevents possible incorrect dates being used.

I'm concerned as I'm starting work with AWS services and the RDS could be in any timezone and the lambdas could be in any other timezone.

Am I overthinking this?
Date standardization and transforms are a PITA at the best of times as many devs know ;(

@josephmancuso
Copy link
Member

I think so? date standardization is even more a PITA when you're trying to understand someone else PITA problem 😆

If all dates are in UTC then there should be no issues right?

If the db engine in Sydney has dates stored as UTC then it doesn't matter which dates you check against as long as all dates are in UTC you are checking against.

Its been a long day and I'm tired so my brain is shot as well .. In other words, if you make sure all dates are in the same timezone (UTC) across the board then I think you should be good

@circulon
Copy link
Contributor Author

circulon commented Mar 7, 2021

@josephmancuso

Thanks for the reply.
I will have to do some testing but yeah generating the pendulum dates in utc could work nicely.

Get some rest and keep up the great work
Thanks again

@josephmancuso
Copy link
Member

👍 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature that exists, works as intended but needs to be improved
Projects
None yet
Development

No branches or pull requests

3 participants