2023-05-08 23:14:44 +02:00
|
|
|
package promutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-05-14 05:12:06 +02:00
|
|
|
func TestParseTimeAtSuccess(t *testing.T) {
|
2024-06-04 00:59:25 +02:00
|
|
|
f := func(s string, currentTime, resultExpected int64) {
|
2023-05-08 23:14:44 +02:00
|
|
|
t.Helper()
|
2023-05-14 05:12:06 +02:00
|
|
|
result, err := ParseTimeAt(s, currentTime)
|
2023-05-08 23:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2023-05-14 05:12:06 +02:00
|
|
|
if result != resultExpected {
|
2023-05-08 23:14:44 +02:00
|
|
|
t.Fatalf("unexpected result; got %v; want %v", result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 00:59:25 +02:00
|
|
|
now := time.Now().UnixNano()
|
2023-05-11 22:23:32 +02:00
|
|
|
|
2023-06-20 06:21:27 +02:00
|
|
|
// unix timestamp in seconds
|
2024-06-04 00:59:25 +02:00
|
|
|
f("1562529662", now, 1562529662*1e9)
|
|
|
|
f("1562529662.678", now, 1562529662678*1e6)
|
2023-06-20 06:21:27 +02:00
|
|
|
|
|
|
|
// unix timestamp in milliseconds
|
2024-06-04 00:59:25 +02:00
|
|
|
f("1562529662678", now, 1562529662678*1e6)
|
2023-06-20 06:21:27 +02:00
|
|
|
|
2023-05-08 23:14:44 +02:00
|
|
|
// duration relative to the current time
|
2023-05-14 05:12:06 +02:00
|
|
|
f("now", now, now)
|
2024-06-04 00:59:25 +02:00
|
|
|
f("1h5s", now, now-3605*1e9)
|
2023-05-08 23:14:44 +02:00
|
|
|
|
|
|
|
// negative duration relative to the current time
|
2024-06-04 00:59:25 +02:00
|
|
|
f("-5m", now, now-5*60*1e9)
|
|
|
|
f("-123", now, now-123*1e9)
|
|
|
|
f("-123.456", now, now-123456*1e6)
|
|
|
|
f("now-1h5m", now, now-(3600+5*60)*1e9)
|
2023-05-08 23:14:44 +02:00
|
|
|
|
|
|
|
// Year
|
2024-06-04 00:59:25 +02:00
|
|
|
f("2023Z", now, 1.6725312e+09*1e9)
|
|
|
|
f("2023+02:00", now, 1.672524e+09*1e9)
|
|
|
|
f("2023-02:00", now, 1.6725384e+09*1e9)
|
2023-05-08 23:14:44 +02:00
|
|
|
|
|
|
|
// Year and month
|
2024-06-04 00:59:25 +02:00
|
|
|
f("2023-05Z", now, 1.6828992e+09*1e9)
|
|
|
|
f("2023-05+02:00", now, 1.682892e+09*1e9)
|
|
|
|
f("2023-05-02:00", now, 1.6829064e+09*1e9)
|
2023-05-08 23:14:44 +02:00
|
|
|
|
|
|
|
// Year, month and day
|
2024-06-04 00:59:25 +02:00
|
|
|
f("2023-05-20Z", now, 1.6845408e+09*1e9)
|
|
|
|
f("2023-05-20+02:30", now, 1.6845318e+09*1e9)
|
|
|
|
f("2023-05-20-02:30", now, 1.6845498e+09*1e9)
|
2023-05-08 23:14:44 +02:00
|
|
|
|
|
|
|
// Year, month, day and hour
|
2024-06-04 00:59:25 +02:00
|
|
|
f("2023-05-20T04Z", now, 1.6845552e+09*1e9)
|
|
|
|
f("2023-05-20T04+02:30", now, 1.6845462e+09*1e9)
|
|
|
|
f("2023-05-20T04-02:30", now, 1.6845642e+09*1e9)
|
2023-05-08 23:14:44 +02:00
|
|
|
|
|
|
|
// Year, month, day, hour and minute
|
2024-06-04 00:59:25 +02:00
|
|
|
f("2023-05-20T04:57Z", now, 1.68455862e+09*1e9)
|
|
|
|
f("2023-05-20T04:57+02:30", now, 1.68454962e+09*1e9)
|
|
|
|
f("2023-05-20T04:57-02:30", now, 1.68456762e+09*1e9)
|
2023-05-08 23:14:44 +02:00
|
|
|
|
|
|
|
// Year, month, day, hour, minute and second
|
2024-06-04 00:59:25 +02:00
|
|
|
f("2023-05-20T04:57:43Z", now, 1.684558663e+09*1e9)
|
|
|
|
f("2023-05-20T04:57:43+02:30", now, 1.684549663e+09*1e9)
|
|
|
|
f("2023-05-20T04:57:43-02:30", now, 1.684567663e+09*1e9)
|
2023-05-11 22:23:32 +02:00
|
|
|
|
|
|
|
// milliseconds
|
2024-06-04 00:59:25 +02:00
|
|
|
f("2023-05-20T04:57:43.123Z", now, 1684558663123000000)
|
|
|
|
f("2023-05-20T04:57:43.123456789+02:30", now, 1684549663123456789)
|
|
|
|
f("2023-05-20T04:57:43.123456789-02:30", now, 1684567663123456789)
|
2023-05-11 22:23:32 +02:00
|
|
|
}
|
|
|
|
|
2024-02-22 23:54:20 +01:00
|
|
|
func TestParseTimeMsecFailure(t *testing.T) {
|
2023-05-11 22:23:32 +02:00
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
2024-02-22 23:54:20 +01:00
|
|
|
msec, err := ParseTimeMsec(s)
|
|
|
|
if msec != 0 {
|
|
|
|
t.Fatalf("unexpected time parsed: %d; want 0", msec)
|
2023-05-11 22:23:32 +02:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f("")
|
2023-07-03 13:11:49 +02:00
|
|
|
f("2263")
|
2023-05-11 22:23:32 +02:00
|
|
|
f("23-45:50")
|
|
|
|
f("1223-fo:ba")
|
|
|
|
f("1223-12:ba")
|
|
|
|
f("23-45")
|
|
|
|
f("-123foobar")
|
|
|
|
f("2oo5")
|
|
|
|
f("2oob-a5")
|
|
|
|
f("2oob-ar-a5")
|
|
|
|
f("2oob-ar-azTx5")
|
|
|
|
f("2oob-ar-azTxx:y5")
|
|
|
|
f("2oob-ar-azTxx:yy:z5")
|
2023-05-08 23:14:44 +02:00
|
|
|
}
|