mirror of
https://github.com/xinliangnote/go-gin-api.git
synced 2024-04-21 12:31:46 +00:00
feature(1.2.8): 使用 embed 打包静态资源
- go version 升级为 1.16 - 使用 embed 特性,将静态资源打包进二进制文件 - 优化代码
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package timeutil
|
||||
|
||||
import (
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
cst *time.Location
|
||||
)
|
||||
|
||||
// CSTLayout China Standard Time Layout
|
||||
const CSTLayout = "2006-01-02 15:04:05"
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
if cst, err = time.LoadLocation("Asia/Shanghai"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 默认设置为中国时区
|
||||
time.Local = cst
|
||||
}
|
||||
|
||||
// RFC3339ToCSTLayout convert rfc3339 value to china standard time layout
|
||||
// 2020-11-08T08:18:46+08:00 => 2020-11-08 08:18:46
|
||||
func RFC3339ToCSTLayout(value string) (string, error) {
|
||||
ts, err := time.Parse(time.RFC3339, value)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return ts.In(cst).Format(CSTLayout), nil
|
||||
}
|
||||
|
||||
// CSTLayoutString 格式化时间
|
||||
// 返回 "2006-01-02 15:04:05" 格式的时间
|
||||
func CSTLayoutString() string {
|
||||
ts := time.Now()
|
||||
return ts.In(cst).Format(CSTLayout)
|
||||
}
|
||||
|
||||
// ParseCSTInLocation 格式化时间
|
||||
func ParseCSTInLocation(date string) (time.Time, error) {
|
||||
return time.ParseInLocation(CSTLayout, date, cst)
|
||||
}
|
||||
|
||||
// CSTLayoutStringToUnix 返回 unix 时间戳
|
||||
// 2020-01-24 21:11:11 => 1579871471
|
||||
func CSTLayoutStringToUnix(cstLayoutString string) (int64, error) {
|
||||
stamp, err := time.ParseInLocation(CSTLayout, cstLayoutString, cst)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return stamp.Unix(), nil
|
||||
}
|
||||
|
||||
// GMTLayoutString 格式化时间
|
||||
// 返回 "Mon, 02 Jan 2006 15:04:05 GMT" 格式的时间
|
||||
func GMTLayoutString() string {
|
||||
return time.Now().In(cst).Format(http.TimeFormat)
|
||||
}
|
||||
|
||||
// ParseGMTInLocation 格式化时间
|
||||
func ParseGMTInLocation(date string) (time.Time, error) {
|
||||
return time.ParseInLocation(http.TimeFormat, date, cst)
|
||||
}
|
||||
|
||||
// SubInLocation 计算时间差
|
||||
func SubInLocation(ts time.Time) float64 {
|
||||
return math.Abs(time.Now().In(cst).Sub(ts).Seconds())
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package timeutil
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestRFC3339ToCSTLayout(t *testing.T) {
|
||||
t.Log(RFC3339ToCSTLayout("2020-11-08T08:18:46+08:00"))
|
||||
}
|
||||
|
||||
func TestCSTLayoutString(t *testing.T) {
|
||||
t.Log(CSTLayoutString())
|
||||
}
|
||||
|
||||
func TestCSTLayoutStringToUnix(t *testing.T) {
|
||||
t.Log(CSTLayoutStringToUnix("2020-01-24 21:11:11"))
|
||||
}
|
||||
|
||||
func TestGMTLayoutString(t *testing.T) {
|
||||
t.Log(GMTLayoutString())
|
||||
}
|
||||
Reference in New Issue
Block a user