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]

Mar 28
2023

Unexpected Latency in Tinyproxy

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

LogLevel Info …
ts=20:58 tags=[software,oss]

Mar 28
2023

Conda and Slow Proxies

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 …

ts=19:39 tags=[software,oss]

Feb 04
2023

Jan 10
2023

Verifying Local iOS Backups

Backups are a great idea, and encrypted backups are a even better idea. When backing up iOS devices on macOS, it is possible to set an encryption key to protect local files. There is however no straightforward GUI method to verify the encryption key.

Enter mvt:

conda create -n mvt …
ts=14:40 tags=[software,osx,ios,macos]

Dec 20
2022

Exif Tag Types

Went on a journey trying to figure out what valid exif tag types are, in both JPEG and TIFF files. In summary:

TYPE # Name Exif 2.32 Tiff 6.0 Adobe TN[1]
1 BYTE X
X
2 ASCII X X
3 SHORT X X
4 LONG
X X
5 …
ts=04:37 tags=[software]

Dec 20
2022

FSCK'ing Veracrypt Volumes on macOS

Due to unexpected power loss or disconnection of the underlying storage, the filesystem inside Veracrypt volumes is desired to prevent future data loss.

On macOS I tried using 

diskutil verifyDisk /dev/diskXXX

This didn't  work for some reason, most likely because of some interaction with veracrypt. Same results via Disk …

ts=04:33 tags=[software,macos]

Jul 30
2022

TigerVNC and Systemd

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:

  1. Type=forking so systemd correctly identifies the real VNC process, not the perl …
ts=01:51 tags=[software,linux,oss]

Jul 21
2022

Using SubShapeBinders and Assembly4

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

Kopia: Handling Directories That No Longer Exist

Scenario

  1. There exists a policy that snapshots directory ABC 
  2. Directory ABC now no longer exists
  3. You want to keep existing snapshots
  4. You want to stop kopia from attempting to take any more snapshots

In order to achieve (4):

  1. Set the policy to manual only
  2. Disable inheritance from parent/global policy …

Jan 02
2022

Goodbye SpiderOak

I have been using SpiderOak One since Edward Snowden recommended it in 2013, almost a decade ago. I have over 1.5T of deduplicated data spread across 6 or so devices. This year, in 2022, I will not be renewing my subscriptions.

The main issues:

  1. Lack of updates: the last …
ts=08:57 tags=[computer,software]

May 07
2016

Apr 16
2016

Apr 12
2016

Method Forwarding in Objective-C Pitfall

A common pitfall of trying to use message forwarding in Objective-C is forgetting to implement

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector

in addition to

- (void)forwardInvocation:(NSInvocation *)anInvocation

Another problem is that sometimes calling objects will use

- (BOOL)respondsToSelector:(SEL)aSelector

to determine if your proxy object can respond to a particular …

Mar 23
2016

Mar 23
2016

Notes on Migrating a Linux Install

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

Thinkpad Mute Buttons and Xubuntu

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 …

Oct 04
2015

Next → Page 1 of 4