Implement pytz for front end

I’d like to be able to do simple time operations, and having pytz implemented on the front end would help with that.

Thanks :slight_smile:

2 Likes

This just came across my desk:

The article should help anyone avoid those “footguns”.

1 Like

What sort of time operations did you have in mind?

1 Like

Currently I have a custom DateTime + Time component that I’ve made. Currently I go to the server for some operations, but I’d like it to be in the front end :slight_smile:

Currently…

import anvil.server
from datetime import datetime
import pytz

@anvil.server.callable
def get_current_time_and_tz(tz_name):
    tz = pytz.timezone(tz_name)
    current_time = datetime.now(tz)
    formatted_time = current_time.strftime('%I:%M %p')
    return current_time.date(), formatted_time, tz.zone


@anvil.server.callable
def selected_datetime_less_than_or_equal_to_current_datetime(selected_date, selected_time, selected_tz, current_date, current_time, current_tz):
    if not selected_time.strip():
        return False  # or handle the missing time case differently
    selected_datetime = datetime.strptime(selected_date + ' ' + selected_time, '%Y-%m-%d %I:%M %p')
    print('Selected date time was ' + str(selected_datetime))
    current_datetime = datetime.strptime(current_date.strftime('%Y-%m-%d') + ' ' + current_time, '%Y-%m-%d %I:%M %p')
    selected_datetime = pytz.timezone(selected_tz).localize(selected_datetime)
    current_datetime = pytz.timezone(current_tz).localize(current_datetime)
    return selected_datetime <= current_datetime