Setting timezone offsets using a background process

Hi there, hope you are all well? I would like to ask a question about Background processing and setting timezones manually to a specific region. Any advice would be appreciated. I might need to use pytz to achieve this but hoping I could with just anvil.tz. This works fine if not using background processing as you can use the local browser to inherit the timezone information, but not too sure when setting it this way.

What I’m trying to do:
I am generating a pdf using a form as a background process which will be set to UTC as it is a server side process. What I would like to try achieve, is a way to manually set to a specific region or use an offset in the form.

What I’ve tried and what’s not working:

Tried setting a datepicker date object using the offset in the form.
I have also tried to pass through the offset from the callable function.

Code Sample:

#From the pdf generated form
 self.submission_date.date = form_data['SubmissionDate'].astimezone(anvil.tz.tzoffset(hours=2))
#Tried also passing the data through from a server function with the offset in place.
signature['CreatedAt'].astimezone(anvil.tz.tzoffset(hours=2))

This sounds like it might be an XY problem. You want to achieve an effect, Y, and have decided that doing X (changing the time zone, across the entire Background Process) will achieve that effect, so you’re asking about how to do X.

Changing the time zone across the entire process will potentially affect parts of it that are far below the surface, things we normally don’t (need to) know about.

Can you tell us something about the effect you want to achieve?

Hi @p.colbert, Thanks for your reply. I have a pdf form that gets kicked off by the background process. I would like to change the datetime values in the form to be offset for a particular timezone. These values will always be generated as a utc time instead of local as the local environment would be the server. The form requires those datetime fields to display as a local time to make sense. I do not want to change the whole concept of the server side away from being utc, having the app being time-zone aware is amazing and super useful, just that particular generation. Once I am able to resolve this issue, Ill probably have the form to be generated in multiple time-zones depending on the users location.

Python’s date-time support lets you apply any time zone offset you want. So, for your intended use, it looks like you might pass the time zone, explicitly, as a parameter to the form. You’d code the form to use the passed time zone, in place of the Process’s time zone, in formatting dates and times.

This would decouple it from the Process’s own time zone, and open the door to your intended “multiple time-zone” support. To do this, you’d probably need pytz, anyway. After all, the target will not only be in a different time zone, but there may be a difference in DST settings, too, at the target location and time.

You could pass a list of time zones to the process.

Thank you so much for your reply. Makes alot of sense, I will try this out. Thanks and happy easter :slight_smile: :rabbit2:

1 Like

An alternative to pytz that I really like is Arrow

https://arrow.readthedocs.io/

Hi @stucork. Thank you so much for the recommendation. I’ll give it a shot.