Does Anvil recognize datetime.timedelta?

Hi -

I’m trying to display data table rows in a repeating panel if the row ‘date_created’ field is less than one week ago.

image

I’m attempting to use datetime and timedelta to acheive this

image

Here’s the code I’m using, it takes datetime.today() just fine, but timedelta is throwing an error. :cry:

The goal is to then set an if statement to True if the row[‘date_created’] is between seven_days_ago and today

Thanks! -Ian

Yes, Anvil has full support for datetime.timedelta. The problem you’re seeing is because you’ve imported the datetime and timedelta classes directly, so you can refer to them directly:

from datetime import datetime, timedelta

today = datetime.today()
t_delta = timedelta(days=7)

Alternatively, just import the datetime package:

import datetime

today = datetime.datetime.today()
t_delta = datetime.timedelta(days=7)

Hope that helps!

Yes it does help thank you, although now running into timezone aware vs. naive issues.

I assume the data table is aware, and ‘today’ (from the above example) is naive.

I am trying to import some modules like pytz and tzlocal to make the datetime aware, but it seems this must be done on the server module

I was using restricted Python 2 server from when I had a free trial and it appears it wasn’t able to import these libraries

I switched to Full Python 3.6 and am waiting 24 hours for server module to update, and then will try again

Glad to hear that got things working. See my Time Zone issues post for a detailed description of naïve-vs-aware datetimes.

The code in that Time Zone post works great.

Would you recommend establishing the local timezone and any other datetime rules in a server module vs. a browser code? If I’m using a timedelta in an if statement to display certain items, could someone theoretically change the timedelta and manipulate what items are displayed?

That’s a good question, with quite a general answer. Browser code (in Forms and Modules) is always untrusted, because the user can manipulate their browser to do whatever they want. Server code (in Server Modules) is never seen by the user, so they cannot modify it. It is trusted.

So if code in the browser is choosing something like a date range, a sufficiently motivated user could theoretically modify that date range, yes. I would do something like pass the requested start and end dates to a server function, which would then validate the range and return the results in the validated range (or perhaps raise an Exception).

There’s one extra thing to think about in your case - the local timezone of the user. The only way you can possibly know the timezone of the user is by querying their browser. That’s how anvil.tz.tzlocal works, and why it will only work in code running on the browser (on the server it always returns UTC). This means that the user has complete control over which timezone they report anyway, so you don’t lose anything security-wise by doing timezone calculations in the browser code.

Hope that helps!

1 Like