浏览代码

fix GetGMTLocation to avoid panic

wenzuochao 6 年之前
父节点
当前提交
7b979bcd41
共有 2 个文件被更改,包括 2 次插入53 次删除
  1. 2 20
      sdk/utils/utils.go
  2. 0 33
      sdk/utils/utils_test.go

+ 2 - 20
sdk/utils/utils.go

@@ -26,10 +26,6 @@ import (
 	"github.com/satori/go.uuid"
 )
 
-// if you use go 1.10 or higher, you can hack this util by these to avoid "TimeZone.zip not found" on Windows
-var LoadLocationFromTZData func(name string, data []byte) (*time.Location, error) = nil
-var TZData []byte = nil
-
 func GetUUIDV4() (uuidHex string) {
 	uuidV4 := uuid.NewV4()
 	uuidHex = hex.EncodeToString(uuidV4.Bytes())
@@ -44,29 +40,15 @@ func GetMD5Base64(bytes []byte) (base64Value string) {
 	return
 }
 
-func GetGMTLocation() (*time.Location, error) {
-	if LoadLocationFromTZData != nil && TZData != nil {
-		return LoadLocationFromTZData("GMT", TZData)
-	} else {
-		return time.LoadLocation("GMT")
-	}
-}
-
 func GetTimeInFormatISO8601() (timeStr string) {
-	gmt, err := GetGMTLocation()
+	gmt := time.FixedZone("GMT", 0)
 
-	if err != nil {
-		panic(err)
-	}
 	return time.Now().In(gmt).Format("2006-01-02T15:04:05Z")
 }
 
 func GetTimeInFormatRFC2616() (timeStr string) {
-	gmt, err := GetGMTLocation()
+	gmt := time.FixedZone("GMT", 0)
 
-	if err != nil {
-		panic(err)
-	}
 	return time.Now().In(gmt).Format("Mon, 02 Jan 2006 15:04:05 GMT")
 }
 

+ 0 - 33
sdk/utils/utils_test.go

@@ -1,9 +1,7 @@
 package utils
 
 import (
-	"fmt"
 	"regexp"
-	"strings"
 	"testing"
 	"time"
 
@@ -64,34 +62,3 @@ func TestGetUrlFormedMap(t *testing.T) {
 	s2 := GetUrlFormedMap(m)
 	assert.Equal(t, "key=value&key2=http%3A%2F%2Fdomain%2F%3Fkey%3Dvalue%26key2%3Dvalue2", s2)
 }
-
-func TestGetTimeInFormatISO8601WithTZData(t *testing.T) {
-	TZData = []byte(`"GMT"`)
-	LoadLocationFromTZData = func(name string, data []byte) (location *time.Location, e error) {
-		if strings.Contains(string(data), name) {
-			location, _ = time.LoadLocation(name)
-		}
-		e = fmt.Errorf("There is a error in test.")
-		return location, e
-	}
-	defer func() {
-		err := recover()
-		assert.NotNil(t, err)
-	}()
-	s := GetTimeInFormatISO8601()
-	assert.Equal(t, 20, len(s))
-	// 2006-01-02T15:04:05Z
-	re := regexp.MustCompile(`^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$`)
-	assert.True(t, re.MatchString(s))
-}
-
-func TestGetTimeInFormatRFC2616WithTZData(t *testing.T) {
-	defer func() {
-		err := recover()
-		assert.NotNil(t, err)
-	}()
-	s := GetTimeInFormatRFC2616()
-	assert.Equal(t, 29, len(s))
-	re := regexp.MustCompile(`^[A-Z][a-z]{2}, [0-9]{2} [A-Z][a-z]{2} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} GMT$`)
-	assert.True(t, re.MatchString(s))
-}