add timehelper;

develop
Ge Song 2 years ago
parent 170bdd0b45
commit 67cfd86cf5

@ -0,0 +1,3 @@
module git.blauwelle.com/go/crate/timehelper
go 1.20

@ -0,0 +1,8 @@
package timehelper
import "time"
// SetupCST 把本地时区固定到CST
func SetupCST() {
time.Local = time.FixedZone("CST", 8*3600)
}

@ -0,0 +1,34 @@
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 ExampleRFC3339Nano2Sec() {
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 ExampleRFC3339Sec2Nano() {
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 ExampleRFC3339NanoOtherZone() {
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
}
Loading…
Cancel
Save