Journey's End

Apr 20
2026

Datetime Display in Django

Suppose you have instruments in different timezones taking readings. You might have the following Django data model:

class Instrument(models.Model):
    timezone_utc_offset_hours = models.FloatField(...)
    ...

class Reading(models.Model):
    instrument = models.ForeignKey(Instrument, ...)
    timestamp = models.DateTimeField(auto_now_add=True)
    ...

Furthermore suppose settings.py contains the following:

...
USE_TZ = True
TIME_ZONE = "Europe/Paris"
...

It would be convenient to be able to display the timestamp for Readings in the timezone of the instrument. To do this, we need to convert the UTC timestamps stored in the database into local time of each instrument:

import datetime

class Instrument(models.Model):
    ...
    @property
    def timezone(self) -> datetime.timezone:
        utc_offset = datetime.timedelta(hours=self.timezone_utc_offset_hours)
        return datetime.timezone(utc_offset)

class Reading(models.Model):
    ...

    @property
    def local_timestamp(self) -> datetime.datetime:
        return self.timestamp.astimezone(self.instrument.timezone)

However if you were to display local_timestamp in a template, e.g. {{a_reading.local_timestamp | time}}, you will get the equivalent time in Paris, not the time where the instrument is located. This is because, when USE_TZ is set, TIME_ZONE in settings controls the default time zone used to display datetimes.

This time zone conversion can be made explicit by using a custom time format that includes the time zone offset: {{a_reading.local_timestamp | time:"H:i O"}}. This will produce something like

16:52 +0200

regardless of the value of timezone_utc_offset_hours due to automatic conversion to TIME_ZONE. In order to display the instrument-local time, we have to disable this automatic conversion using the localtime template tag:

{% load tz %}
...
{% localtime off %}
...
{{ a_reading.local_timestamp | time:"H:i O" }}
...
{% endlocaltime %}

With this change the display time is now in the instrument's timezone:

00:52 +1000

Template Tag Scoping

Note that due to the way templates work, {% localtime off %}...{% endlocaltime %} must be inside the same block as the time display. If specified at the template level, e.g.

{% extends "base.html" %}
{% load tz %}
{% localtime off %}
{% block page-content %}
...
    {{ a_reading.local_timestamp | time:"H:i O" }}
...
{% endblock %}
{% endlocaltime %}

It will have no effect inside the page-content block!

May 24
2025

Adventures in Compressed DNG

I have been working with industrial cameras and frame-grabbers that simply gives me a dump of their sensor data. To make that data useful it needs to undergo demosaicing, ideally using one of the modern demosaic algorithms like RCD rather than simple interpolation or the rather outdated VNG. Unfortunately the …

Apr 10
2025

Python One-liner for Local QR Code Decoding

Because KeepassXC lacks the ability to decode QR code for TOTP setup and not all MFA implementations show the required secret in text format, I use the following snipet to read the QR code information locally:

  python -c 'from cv2 import QRCodeDetector; from PIL.ImageGrab import grabclipboard; import numpy as …
ts=03:07 tags=[python,software,oss]

Feb 04
2023

Aug 05
2018

IPython Notebook on GPS Timing and CDMA

In \[1\]:
%matplotlib inline import matplotlib import numpy as np import matplotlib.pyplot as plt
# GPS Timing Carrier-phase detection is suppose to yield better timing information than tracking the pseudorandom code stream. The reason for this is supposedly that the higher frequency carrier allows for more accurate measurements of the …

May 07
2016

Sep 12
2014

Sep 12
2014

Adventures in Python Optimisation

Recently I found myself looking at the following piece of code and trying to optimise it:

from __future__ import division

import numpy as np

MagneticPermeability = 4*np.pi*1e-7
MagneticPermeabilityOver4Pi = MagneticPermeability / (4*np.pi)

def fieldfunc_py(p, q, moment):
  r = p - q 
  rmag = np.sqrt(r.dot(r))

  rdash3 = rmag …
ts=14:41 tags=[python,c,code,software]

Jul 21
2012

GNU readline with System Python (OS X 10.7)

By default system python loads "fake" readline (fake because it actually uses libedit) from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/readline.so. Even if you install readline (which goes into /Library/Python/2.7/site-packages), it will still load it, even if you fiddle …

ts=01:46 tags=[python,software,osx]

Jan 27
2011

iPhone 4 layer export script for Gimp

I often mockup iPhone interfaces on the Gimp, then exporting each layer for use as backgrounds. For iOS 4 each image needs to be exported twice: one at full resolution with @2x in its filename, and once at half-resolution without the @2x. e.g. nav_bg@2x.png and nav_bg.png …

Oct 11
2009

qrbackup

I generated a gpg keypair for myself today, and I was looking for a fairly safe way to back it up. I don't particularly trust DVD/CDs, and keeping it on flash is even more worrying. I wanted a means of backup I can see and touch.

Paperbak would be …

Oct 09
2008

Jul 16
2008

Apr 28
2008

{} I <3 thee

Firstly, I am the kind of guy who likes tabs over spaces, because I don't like forcing my particular preferences on to other people. To wax poetic, I like to give other people the freedom of choosing how they want their code indented. This of course brings me into the …

Dec 31
2007

html2pml

I was tired of converting all my HTML ebooks to rtf, then to pdb using Palm Doc Converter only to lose all the nice touches like bold, italics and headings. Since there didn't seem to be a HTML to PML converter for OS X, I wrote one to get more …

ts=10:15 tags=[python,code,palm]