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

Support for multiple timezones #19

Open
rcooke opened this issue Feb 17, 2016 · 0 comments
Open

Support for multiple timezones #19

rcooke opened this issue Feb 17, 2016 · 0 comments

Comments

@rcooke
Copy link

rcooke commented Feb 17, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant