Tuesday, October 27, 2020

The curious case of Python's datetime -0753 UTC offset

 "Time, the devourer of all things." - Ovid's Metamorphoses

Confused why your python datetime object's UTC offset is showing up as -0753 instead of -0700 (PDT) or -0800 (PST)? I certainly was.

The tl;dr is because Python's pytz does not do the right thing with daylight saving's time when simply adding a timezone to a naive datetime object, and you must localize it.

Here's an example of what I was encountering. I was parsing a timestamp which was in the America/Los_Angeles timezone. I needed to change the printed format, so I parsed the date and did a quick strftime and... -0753??

from datetime import datetime
import pytz

tz = pytz.timezone("America/Los_Angeles")

# Super duper sure this is America/Los_Angeles tz
niave_string = "2020-10-05 14:03:40"

dateobj = datetime.strptime(niave_string, "%Y-%m-%d %H:%M:%S")

# Make it a timezone-aware object (Or so you think)
dateobj = dateobj.replace(tzinfo=tz)

# Notice the -0753 UTC offset...
print(dateobj.strftime("%Y-%m-%d %H:%M:%S%z"))

'2020-10-05 14:03:40-0753'

The mismatch happens because you can't just slap a tz onto a naive datetime object and hope it will do the right thing. More info on what's happening here can be found on this great stack overflow answer.

The fix for this is to localize your date objects, not just replace the TZ:

from datetime import datetime
import pytz

tz = pytz.timezone("America/Los_Angeles")

# Super duper sure this is America/Los_Angeles tz
niave_string = "2020-10-05 14:03:40"

dateobj = datetime.strptime(niave_string, "%Y-%m-%d %H:%M:%S")

# Localize your date object to your timezone
dateobj = tz.localize(dateobj)

# 🤞🏻
print(dateobj.strftime("%Y-%m-%d %H:%M:%S%z"))

'2020-10-05 14:03:40-0700'

Much better.

Tuesday, August 11, 2020

GP, BD, SB, and other satellite annotations in cgps/xgps gpsd service

Sometimes I stumble onto a problem and Google really has no idea what I'm asking about.

In my case it's the 2-letter satellite code in the CGPS/XGPS program. What are these 2-letter symbols and what do they represent? They are different satellite-based radionavigation (RNSS) identification codes.

Below is the output from cgps, a terminal GUI for the gpsd software I currently have running on my stratum-1 raspberry pi NTP timeserver:

Not my real GPS coordinates :-)

Here's what those two-letter satellite codes mean:

The GPS system that everyone knows and loves started (like many pieces of technology) as a military research project. In its early days it was used to track nuclear-capable subs, and was later expanded by the DOD to be a general purpose navigation system[1].

Now GPS is used worldwide for virtually all positioning applications. However, the satellites that power GPS are still largely under the control of the US government, and the things that are given can also be taken away.

As you can imagine the US hegemony of control is seen as a national security risk by other countries. Given that, some countries have launched their own competing technologies so as not to rely on a rival's generosity. The three largest alternatives to GPS are Russia's GLONASS, China's Beidou, and the EU's Galileo.

Here's a summary of these systems:

GPS/GNSS (United States) 🇺🇸
By far the most widely-used navigation system. Originally developed for military use. Accurate down to 10 centimeters or less. This is the system you think of when you hear the word "GPS".

GLONASS (Russia) 🇷🇺
Competing technology designed in the 1970s by the USSR, now used throughout Russia.

BeiDou (China) 🇨🇳
Launched in 2000 as a rival to GPS. The positions of the satellites are said to make this system more accurate around Asia. China has 22 operational BeiDou satellites and is expected to continue to launch more and increase the coverage and accuracy of this system. Did you know that China uses datum displacement to intentionally obfuscate GNSS map accuracy? Read more here.

Galileo (EU) 🇪🇺
Launched in 2011 by the European Union. Galileo currently operates 14 satellites and is intended to be more accurate at higher latitudes than GNSS systems such as GPS.

There are other systems as we the Indian Regional Navigation Satellite System (IRNSS). These systems are meant to improve the accurate and coverage of specific geographic locations (in this case India).