Apr 20
2026
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
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
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 …
Feb 04
2023
In order to install the arcgis package in conda, you have to specify the architecture osx-64 otherwise the package will not be found. This works because MacOS will capable of running x86_64 code on Apple Silicon processors - that is how efficient they are!
conda install -c esri/osx-64 arcgis
In …
Aug 05
2018
# 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
I needed to read Lecroy binary waveforms files recently, and the one python script I had found didn't read them properly due to improper handling of 16 bit samples.
I wrote an alternative lecroy.py that should be a drop-in replacement. It handles 16 bit waveforms and exports a LecroyBinaryWaveform …
Sep 12
2014
Previously we found that PyPy achieves the best performance gain, executing fieldfunc_py in ~6 us. At the end of that article, I mentioned that the C implementation is up to 50x faster, managing the same calculation in ~0.12 us.
The naive conclusion is that the best thing is to …
Sep 12
2014
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 …
Jul 21
2012
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 …
Jan 27
2011
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
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
But you can get the current function's name by using:
sys._getframe().f_code.co_name
Seems to work alright for debugging purposes. See the link embedded in the title for the origins and 3 other methods.
Cheers,
Steve
Jul 16
2008
I have set up an unofficial git respository for libfg patches and new swig generated python interface, as Gavin Baker (the author) appears to be busy with other things. This is a maintance only repository as far as libfg goes - I don't plan on adding any more features (since all …
Apr 28
2008
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
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 …