mirror of
https://gitee.com/johng/gf
synced 2026-07-02 19:31:07 +08:00
improve package gtime
This commit is contained in:
@ -14,6 +14,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"github.com/gogf/gf/internal/utils"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -102,18 +103,14 @@ var (
|
||||
// The parameter <zone> is an area string specifying corresponding time zone,
|
||||
// eg: Asia/Shanghai.
|
||||
//
|
||||
// Note that the time zone database needed by LoadLocation may not be
|
||||
// present on all systems, especially non-Unix systems.
|
||||
// LoadLocation looks in the directory or uncompressed zip file
|
||||
// named by the ZONEINFO environment variable, if any, then looks in
|
||||
// known installation locations on Unix systems,
|
||||
// and finally looks in $GOROOT/lib/time/zoneinfo.zip.
|
||||
// This should be called before package "time" import.
|
||||
// Please refer to issue: https://github.com/golang/go/issues/34814
|
||||
func SetTimeZone(zone string) error {
|
||||
location, err := time.LoadLocation(zone)
|
||||
if err == nil {
|
||||
time.Local = location
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
return os.Setenv("TZ", location.String())
|
||||
}
|
||||
|
||||
// Timestamp retrieves and returns the timestamp in seconds.
|
||||
|
||||
@ -15,12 +15,10 @@ import (
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gtime.SetTimeZone("Asia/Shanghai")
|
||||
}
|
||||
func Test_SetTimeZone(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(time.Local.String(), "Asia/Shanghai")
|
||||
t.Assert(gtime.SetTimeZone("Asia/Shanghai"), nil)
|
||||
//t.Assert(time.Local.String(), "Asia/Shanghai")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user