improve package gtime

This commit is contained in:
jflyfox
2021-01-04 15:32:55 +08:00
parent b3b1418e11
commit 9d865e4ac6
2 changed files with 8 additions and 13 deletions

View File

@ -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.

View File

@ -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")
})
}