Adventures in programming: WSL and time drift

I went to try to install golang on my WSL install today, but my packages lists for apt-get were apparently out of date, so I got an error:

E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/l/linux/linux-libc-dev_5.4.0-52.57_amd64.deb  404  Not Found [IP: 91.189.88.152 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

Theoretically, this should be easy to solve with apt-get update, as recommended right there in the error message, but I got an error there too:

$ sudo apt-get update
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [109 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Reading package lists... Done
E: Release file for http://security.ubuntu.com/ubuntu/dists/focal-security/InRelease is not valid yet (invalid for another 22h 43min 18s). Updates for this repository will not be applied.
E: Release file for http://archive.ubuntu.com/ubuntu/dists/focal-updates/InRelease is not valid yet (invalid for another 22h 43min 31s). Updates for this repository will not be applied.
E: Release file for http://archive.ubuntu.com/ubuntu/dists/focal-backports/InRelease is not valid yet (invalid for another 22h 43min 55s). Updates for this repository will not be applied.

According to https://askubuntu.com/questions/1096930/sudo-apt-update-error-release-file-is-not-yet-valid, this happens if your clock is incorrect. My laptop clock looked OK, but the clock inside WSL was ~11 minutes behind. It turns out this is a known issue that the clock in WSL can get out of sync with the actual time clock: https://github.com/microsoft/WSL2-Linux-Kernel/issues/16 Luckily, the fix was quite simple:

$ date
Fri Jan  1 15:15:58 PST 2021
$ sudo hwclock --hctosys
$ date
Sat Jan  2 15:26:40 PST 2021

Running sudo hwclock --hctosys told WSL to sync its clock with the underlying hardware clock. Since that clock was already correct in my case, my problem was fixed and I was able to run apt-get update successfully, then install golang. And now that I’m documenting the solution for myself, it should be faster to figure out what’s going on next time!