timelint
timelint checks that every call to time.Now() in Go source is normalized
to UTC via .UTC(). Timestamps that carry the host's local timezone can
drift depending on the TZ environment variable, so all wall-clock time in
the program must be UTC.
It follows an aliased import of time, and accepts only time.Now().UTC()
chained directly: time.Now().Add(d).UTC() still fails. Test files are
skipped, since fixtures may build time.Time values whose location is
irrelevant to the assertion. So are generated files, with the
Code generated ... DO NOT EDIT. header, and the .git and vendor
directories.
A call that measures elapsed time should keep time.Now() as it is:
.UTC() strips the monotonic clock reading Go uses for durations, so a
step of the wall clock, as NTP makes, would skew them. Mark such a call
with a directive and the reason, at the end of its line or alone on the
line above:
start := time.Now() //timelint:monotonic times the request
A directive without a reason is reported as a problem itself.
timelint prints each call it finds as timelint: file:line:col: ... and
exits 1 if there are any.
Install
go get -tool evantann.com/timelint@latest
Use
go tool timelint [dir ...]
It lints every Go file under each dir, the current directory by default. Reads stay within each dir, so a symlink can't lead the walk outside it. It takes no flags.
Makefile
lint-time:
go tool timelint
.PHONY: lint-time
License
GNU Affero General Public License v3.0 or later. See LICENSE.
