You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
crate/timehelper/time_test.go

35 lines
1.0 KiB
Go

2 years ago
package timehelper_test
import (
"fmt"
"time"
)
// Javascript 只能精确到毫秒:
// new Date('2023-01-15T15:16:17.123456789+08:00').toISOString()
// Output: '2023-01-15T07:16:17.123Z'
func ExampleRFC3339Nano() {
t, _ := time.Parse(time.RFC3339Nano, "2023-01-15T15:16:17.123456789+08:00")
fmt.Println(t.Format(time.RFC3339Nano))
// Output: 2023-01-15T15:16:17.123456789+08:00
}
func ExampleRFC3339_nano2Sec() {
2 years ago
t, _ := time.Parse(time.RFC3339Nano, "2023-01-15T15:16:17.123456789+08:00")
fmt.Println(t.Format(time.RFC3339))
// Output: 2023-01-15T15:16:17+08:00
}
// 使用 RFC3339 没有丢失纳秒精度
func ExampleRFC3339_sec2Nano() {
2 years ago
t, _ := time.Parse(time.RFC3339, "2023-01-15T15:16:17.123456789+08:00")
fmt.Println(t.Format(time.RFC3339Nano))
// Output: 2023-01-15T15:16:17.123456789+08:00
}
func ExampleRFC3339_nanoOtherZone() {
2 years ago
t, _ := time.Parse(time.RFC3339Nano, "2023-01-15T15:16:17.123456789+09:00")
fmt.Println(t.Format(time.RFC3339Nano))
// Output: 2023-01-15T15:16:17.123456789+09:00
}