dakrauth / django-swingtime

📆 Event and occurrence scheduling application for Django
https://dakrauth.github.io/django-swingtime/
MIT License
318 stars 99 forks source link

Support for multiple timezones #19

Open rcooke opened 8 years ago

rcooke commented 8 years ago

Does 0.6 or later support users in different time zones?

I need to support this, but when I set Django to EST (Toronto) I got lots of naive date warnings.

I think I fixed it by adding in use of django.utils.timezone.make_aware(). I added a function to utils.py that "upgrades" any naive dates passed in to use the current TZ setting.

I assume later, when I allow each user to specify their local time zone, this will automatically update the active DJango TZ and allow them to see all events in their local time, and also to correctly interpret any created dates.

Have I totally missed the boat on how this should be handled? Or should I carry on - make some tests and issue a pull request?

Regards, Rich.

UTILS.py:


# To fix niave datetimes:
from django.utils.timezone import make_aware, is_naive

#-------------------------------------------------------------------------------
def my_make_aware(dt):
    '''
    Upgrade any naive date/times to current active timezone.  
    If the passed in date/time already has a time zone setting,
    return it as-is. (we don't want to override its TZINFO)
    '''

    if is_naive(dt):
        return make_aware(dt)
    else:
        return dt

The function is used in the create timeslot table function. In 0.6 the "start_time=" line is not present. I migrated my changes to 0.7.2 but do not know yet if there is a conflict with your changes.

create_timeslot_table()::


    dt = dt or datetime.now()
    start_time = start_time.replace(tzinfo=dt.tzinfo) if not start_time.tzinfo else start_time
    dtstart = my_make_aware(datetime.combine(dt.date(), start_time))

I also made an improvement to the STR value for the Occurrence model:

    def __str__(self):
        return '{}: {} for {} hour(s)'.format(self.title, localtime(self.start_time).strftime('%Y-%m-%d, %I:%M:%S %p'), str((self.end_time-self.start_time).total_seconds()/(60*60)))

Using django.utils.localtime() to convert values to the user's TZ and I think a better layout.