tailscale/tstime/tstime_test.go
Joe Tsai 7e6c5a2db4
tstime: rely on stdlib parse functionality (#7482)
The time.Parse function has been optimized to the point
where it is faster than our custom implementation.
See upstream changes in:

* https://go.dev/cl/429862
* https://go.dev/cl/425197
* https://go.dev/cl/425116

Performance:

	BenchmarkGoParse3339/Z     38.75 ns/op    0 B/op    0 allocs/op
	BenchmarkGoParse3339/TZ    54.02 ns/op    0 B/op    0 allocs/op
	BenchmarkParse3339/Z       40.17 ns/op    0 B/op    0 allocs/op
	BenchmarkParse3339/TZ      87.06 ns/op    0 B/op    0 allocs/op

We can see that the stdlib implementation is now faster.

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2023-03-06 18:05:51 -08:00

37 lines
804 B
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package tstime
import (
"testing"
"time"
)
func TestParseDuration(t *testing.T) {
tests := []struct {
in string
want time.Duration
}{
{"1h", time.Hour},
{"1d", 24 * time.Hour},
{"365d", 365 * 24 * time.Hour},
{"12345d", 12345 * 24 * time.Hour},
{"67890d", 67890 * 24 * time.Hour},
{"100d", 100 * 24 * time.Hour},
{"1d1d", 48 * time.Hour},
{"1h1d", 25 * time.Hour},
{"1d1h", 25 * time.Hour},
{"1w", 7 * 24 * time.Hour},
{"1w1d1h", 8*24*time.Hour + time.Hour},
{"1w1d1h", 8*24*time.Hour + time.Hour},
{"1y", 0},
{"", 0},
}
for _, tt := range tests {
if got, _ := ParseDuration(tt.in); got != tt.want {
t.Errorf("ParseDuration(%q) = %d; want %d", tt.in, got, tt.want)
}
}
}