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 …
Aug 11
2024
Introduction
I want to be able to raise system level interrupts from PIO programs and there aren’t too many examples of this, so here is my simple contribution.
PIO program
First, the PIO program, which simply waits for a pin to change and then immediately raises PIO IRQ 0 …
Mar 28
2023
I love tinyproxy, it is simple to setup and more than enough for my needs. Recently however I noticed that some clients were experiencing unexpected long delays when doing something as simple as curl google.com. After some sleuthing, I tracked it down to the fact I had
Mar 28
2023
The default conda connect timeout is 9.15 seconds (no idea why this is, seems to have been the case from day one). If the proxy is slow, e.g. it is in another country or AWS region, this timeout can be triggered and conda will prematurely close the connection …
Jul 30
2022
A quick systemd service definition file for tigervnc on Ubuntu 20.04
[Unit]
Description=tigervnc
[Service]
Type=forking
ExecStart=/usr/bin/vncserver -localhost no -depth 24 -geometry 1920x1080 -noreset
User=steve
[Install]
WantedBy=multi-user.target
Key components:
Type=forking so systemd correctly identifies the real VNC process, not the perl …
Jul 21
2022
Assembly4 and SubShapeBinders make it possible to assemble Parts, then using their position in the assembly to generate new Parts. However because new Parts depend on Parts in the assembly, if you try to add them to the assembly FreeCAD 0.19 will complain about cyclic redundancy, which makes sense …
Jul 21
2022
Scenario
- There exists a policy that snapshots directory ABC
- Directory ABC now no longer exists
- You want to keep existing snapshots
- You want to stop kopia from attempting to take any more snapshots
In order to achieve (4):
- Set the policy to manual only
- Disable inheritance from parent/global policy …
Mar 05
2022
In astrophotography the combination of a goto mount, a camera and plate-solving is a powerful one. It allows you to do all kind of neat things, like polar-alignment without having a clear view of the south, extremely accurate goto functionality, and automated capture of multiple predefined targets.
There are roughly …
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 …
Apr 16
2016
Arduino-Makefile needed coaxing to in order to compile code targeting the SparkFun Pro Micro 3.3V. A major source of headache is the requirement for BOARD_SUB which isn't documented and required diving into the source code, and also the fact the Pro Micro has 2 PIDs for each variant (5V …
Mar 23
2016
I recently started having trouble with Arduino 1.6.x not being able to program Arduino Micro or SparkFun Pro Micro, with avrdude complaining about permission denied when trying to open /dev/ACM0.
The usual solutions to this is to add oneself to the dialup group, or mess around in …
Mar 23
2016
Some issues I ran into when trying to move a Linux install from a 500 GB drive to a smaller 120 G SSD:
- When duplicating the filesystem, make sure that /proc exists, otherwise when you boot the new drive the kernel will complaint that /proc is missing and it can't …
Mar 23
2016
I recently acquired a used Lenovo X220 for use as a linux laptop, and needed the following in my openbox configuration XML to make the hardware speaker and mic mute buttons work:
<!-- Modified for X220 -->
<keybind key="XF86AudioMute">
<action name="Execute">
<command>pactl set-sink-mute 0 toggle</command>
</action>
</keybind>
<keybind …
Aug 26
2014
 |
Fluorescence image of
cavitation effects,visualised
using FITC-Dextran.
|
If, like me, you use Nikon's NIS Elements to do fluorescence imaging, you would have also noticed that nothing other than NIS Elements can read the metadata that is saved with TIFFs. ImageJ can't read it, the GIMP can't read it, tiffinfo …
Oct 16
2012
If you are getting the error
"Unknown control sequence: \doublespacing" when you try to compile your
beamer presentation in Lyx, change
Document→Settings→Text Layout→Line spacing
to Single.
Cheers,
Steve
Sep 14
2012
Calibre is a great tool, especially for converting between ebook formats. Here are some notes for getting it to run on a headless server.
- The binary installer off the website works fine — ignore warnings about completion and desktop integration failing.
- The installer will always pollute /usr/bin regardless of the …
Aug 23
2012
I used to use Jumpcut a long time ago, and was a big fan of it. At some point, I think around 10.5, it stopped working, and I let it go -- too busy at the time to poke at it.
Recently however my interest in it was piqued again …
Apr 28
2012
Lyx 2.0 and onwards on OS X will use spellchecker provided by the OS X. Note however that the language it will use for spellchecking is determined by the document's language setting, not your system language setting.
Furthermore, in version 2.0.3, if you change your document's language …