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 …
Dec 23
2023
There now exists NSDateFormatter and NSISO8601DateFormatter and their behaviour when a timezone is not set is not the same: NSDateFormatter produces strings formatted for local time while NSISO8601DateFormatter produces string formatted for UTC.
As an example, given a date-time of 2000-12-31T23:00:00Z
NSDateFormatter with no timezone set and full …
Feb 19
2022
I recently tried my hands at implementing motion event support for canvas widgets in Toga on Linux, which uses GTK3 as its GUI back end. In the process I had learn to navigate GDK's event handling mechanics and documentation. This post is a summary of notes I made during the …
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 …
Apr 12
2016
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 …
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 …
Aug 26
2014
This is sort of a placeholder post. Busy meeting a deadline, but this should help future Steve and anyone else when you need to turn your Lyx document into a Word document while keeping the format mostly sane. Broken, but sane.
- Export as
Latex (plain).
-
Run
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 …
Jan 27
2012
- Create a virtual env and activate it
- hg clone https://code.google.com/p/pyglet/
- pushd pyglet; python setup.py install
- popd
- easy_install pyobjc==2.3 # the ==2.3 is required as of 2012-01-27, or pyobjc-core will not install
- python -c "import pyglet" # this step should not fail
Cheers,
Steve …
May 18
2011
svn st | grep '^?' | sed 's/^[? ]*/"/' | sed 's/\$/"/' | xargs svn add
Cheers,
Steve
May 08
2011
Here is my firehol.conf that allows multicast mDNS packets through:
# define mdns so we will accept it
server_mdns_ports="udp/5353"
client_mdns_ports="5353"
interface eth+ multi
policy return
server mdns accept
server multicast accept
interface eth+ home src "\${home_ips}"
server all accept
client all accept
Initially I had the server …
Apr 17
2011
- const char *foo; is not the same as char * const foo; The former declares a pointer to constant char, while the latter declares a constant pointer to char.
- - [NSError localizedDescription] returns the object for key NSLocalizedDescriptionKey in the error's userInfo dictionary. I always wondered how to set it since there …
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 …
Jan 22
2011
Socket.IO is a great library and framework that doesn't seem to have a good description of the protocol available. In interest of helping others when they google "socket.io framing protocol", this post was born.
The description of the framing protocol used by Socket.IO below is taken from …
Jan 16
2011
I generate UIImages in some of my iOS applications, and prior to the iPhone 4 they all worked fine. With the introduction of the iPhone 4 and Retina Display, I realised my generated images were blurry even though I am rendering them at twice the resolution.
It turns out this …
Sep 21
2010
I would like to start this post off by saying that emotionally I agree with the sentiment that HTML is not a programming language. Intellectually, however, my position is that HTML is a programming language.
My argument for HTML as a programming language is very abstract, and to put it …
Jun 09
2010
This script produces an IPA, then verifies to make sure the IPA is actually valid.
PRODUCT_NAME=""
IPA="\\({PRODUCT_NAME}.ipa"
APP="\\).app"
if [ -z "\${PRODUCT_NAME}" ]; then
echo "PRODUCT_NAME not set";
exit 1;
fi
rm -rf ipa
mkdir -p ipa/Payload &&
cp -R build/Debug-iphoneos/\\({APP} ipa/Payload/ &&
pushd ipa &&
zip -q …
Mar 26
2010
- Maximum RA guide pulse is 1000ms (1s). If PHD is not locking on to the star and it says "dur=1000" in the status bar, a better polar alignment is required.
- RA hysteresis is used in a 2-term weighted moving average:
RA_dist = (1.0 - RA_hysteresis) * RA_dist + RA_hysteresis * last_guide
Cheers,
Steve …