Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django and Time Zones: New in the Trunk

Django and Time Zones: New in the Trunk

Presentation I gave during Pykonik meet-up (December 2011)
http://blog.pykonik.org/2011/11/grudniowe-spotkanie-december-meetup.html

Tomek Paczkowski

December 07, 2011
Tweet

More Decks by Tomek Paczkowski

Other Decks in Programming

Transcript

  1. Time zones are a lie. Sundials XIX century – Railroad

    time Artificial division Humans think in local time
  2. Time zones are a lie. Sundials XIX century – Railroad

    time Artificial division Humans think in local time Time as a point in continuum
  3. DST: Dreadful Summer Time Promises of savings beyond comprehension ...

    ... and more sunlit leisure Artificial division, even more political
  4. DST: Dreadful Summer Time Promises of savings beyond comprehension ...

    ... and more sunlit leisure Artificial division, even more political No reliably measured savings
  5. DST: Dreadful Summer Time Promises of savings beyond comprehension ...

    ... and more sunlit leisure Artificial division, even more political No reliably measured savings Reliably measured raise in suicides
  6. Time is hard. Let’s go shopping UTC = GMT CET

    → CEST (27/03/2011 2:30?) CET ← CEST
  7. Time is hard. Let’s go shopping UTC = GMT CET

    → CEST (27/03/2011 2:30?) CET ← CEST (30/10/2011 2a:30)
  8. Time is hard. Let’s go shopping UTC = GMT CET

    → CEST (27/03/2011 2:30?) CET ← CEST (30/10/2011 2a:30) CET/CEST vs Warsaw Mean Time
  9. Time is hard. Let’s go shopping UTC = GMT CET

    → CEST (27/03/2011 2:30?) CET ← CEST (30/10/2011 2a:30) CET/CEST vs Warsaw Mean Time (+1:24)
  10. Time is hard. Let’s go shopping UTC = GMT CET

    → CEST (27/03/2011 2:30?) CET ← CEST (30/10/2011 2a:30) CET/CEST vs Warsaw Mean Time (+1:24) Future dates
  11. Code, dammit! Python and datetime, time, calendar... Abstract tzinfo class,

    but no implementations datetime.utcnow().tzinfo is None
  12. Code, dammit! Python and datetime, time, calendar... Abstract tzinfo class,

    but no implementations datetime.utcnow().tzinfo is None datetime(2011, 3, 27, 2, 30, tzinfo=CET) # never happend, but no error
  13. Twitz class Profile(models.Model): user = models.OneToOneField(User) time_zone = models.CharField(max_length=200, blank=True)

    @receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance)
  14. Twitz class SettingsView(UpdateView): template_name = ’accounts/settings.html’ form_class = SettingsForm def

    get_object(self, queryset=None): return self.request.user.profile def get_success_url(self): return reverse(’settings’) settings = login_required(SettingsView.as_view())
  15. Twitz class TimeZoneMiddleware: def process_request(self, request): user = request.user if

    user.is_authenticated(): try: timezone.activate(user.profile.time_zone) except (UnknownTimeZoneError, ValueError) as e: logger.warn("Error setting time zone: %s", e)
  16. How it works Thread-locals All dates converted on output (templates)

    All dates converted on input (forms) Database stores everything in UTC
  17. How it works Thread-locals All dates converted on output (templates)

    All dates converted on input (forms) Database stores everything in UTC Migration: consult your database manual.
  18. How it works Thread-locals All dates converted on output (templates)

    All dates converted on input (forms) Database stores everything in UTC Migration: consult your database manual. ... PostgreSQL: no migration needed