tz.go 763 Bytes
Newer Older
1 2
package timezone_e

3 4 5 6 7
import (
	"regexp"
	"strconv"
	"time"
)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

type Timezone int

const (
	TimezoneBeijing Timezone = 0
	TimezoneKSA     Timezone = 1
)

var Timezones = []Timezone{TimezoneBeijing, TimezoneKSA}
var (
	BeijingTimezoneLoc = time.FixedZone("CST", 8*3600) // 东八
	KSATimezoneLoc     = time.FixedZone("CST", 3*3600) // 东三
)

var TimezoneLocMap = map[Timezone]*time.Location{
	TimezoneBeijing: BeijingTimezoneLoc,
	TimezoneKSA:     KSATimezoneLoc,
}
26 27 28 29 30 31 32 33 34 35 36 37

// 根据header的Timezone获取时区
// return nil: 无法解析
func GetFixedTimezone(tz string) *time.Location {
	re := regexp.MustCompile(`\d+`)
	timeZone := re.FindString(tz)
	offset, _ := strconv.Atoi(timeZone)
	if offset <= 0 {
		return nil
	}
	return time.FixedZone("CST", offset*3600)
}