#536·jwt

`NumericDate` does not round-trip through JSON at millisecond `TimePrecision`

Author: DRMacIverCreated Sep 23, 2026Updated Sep 24, 2026

With TimePrecision set to time.Millisecond, a NumericDate the library marshals and then unmarshals comes back one millisecond early for about half of all values: 1700000000.123 reads back as 1700000000.122, while 1700000000.125 reads back exactly:

go
package main

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/golang-jwt/jwt/v5"
)

func main() {
	jwt.TimePrecision = time.Millisecond

	for _, ms := range []int64{123, 125, 100, 1} {
		t := time.Unix(1700000000, ms*int64(time.Millisecond))
		b, _ := json.Marshal(jwt.NewNumericDate(t))
		var back jwt.NumericDate
		_ = json.Unmarshal(b, &back)
		fmt.Printf("%s -> %d.%03d\n", b, back.Unix(), back.Nanosecond()/1e6)
	}

	bad := 0
	for ms := int64(0); ms < 1000; ms++ {
		t := time.Unix(1700000000, ms*int64(time.Millisecond))
		b, _ := json.Marshal(jwt.NewNumericDate(t))
		var back jwt.NumericDate
		_ = json.Unmarshal(b, &back)
		if !back.Equal(t) {
			bad++
		}
	}
	fmt.Printf("%d of 1000 millisecond values do not round-trip\n", bad)
}

Output:

1700000000.123 -> 1700000000.122
1700000000.125 -> 1700000000.125
1700000000.100 -> 1700000000.099
1700000000.001 -> 1700000000.000
496 of 1000 millisecond values do not round-trip

The marshalled text is the expected one in each case; it is reading that text back that lands one millisecond earlier, for 496 of the 1000 millisecond fractions of this second (.001, .100 and .123 among them; .000 and .125 read back exactly). I expected NewNumericDate(t), written and read at the same TimePrecision, to equal t.

Tested on v5.3.1 and on current main (73c870b).

BTW, this was found by an automated program that writes property-based tests for various open source projects using hegel (but it has been reviewed by hand before reporting). We've also potentially found (but not yet hand validated) 6 other bugs in golang-jwt. You can see the tests at https://github.com/hegeldev/hegel-zoo/tree/main/targets/go/golang-jwt. Let us know if you would like us to file the other bugs found and/or contribute the tests. NB the tests are currently LLM generated and probably not yet suitable for inclusion as is, but we're happy to help get them into a better state if you want them.