31 lines
811 B
Go
31 lines
811 B
Go
// Package datetime centralizes the application's Beijing time policy.
|
|
package datetime
|
|
|
|
import "time"
|
|
|
|
const Zone = "Asia/Shanghai"
|
|
|
|
var Location = func() *time.Location {
|
|
location, err := time.LoadLocation(Zone)
|
|
if err != nil {
|
|
return time.FixedZone("CST", 8*60*60)
|
|
}
|
|
return location
|
|
}()
|
|
|
|
func Now() time.Time { return time.Now().In(Location) }
|
|
|
|
// Normalize converts any source instant (including COS metadata) to Beijing time.
|
|
func Normalize(value time.Time) time.Time {
|
|
if value.IsZero() { return value }
|
|
return value.In(Location)
|
|
}
|
|
|
|
func Parse(value string) (time.Time, error) {
|
|
parsed, err := time.Parse(time.RFC3339Nano, value)
|
|
if err != nil { return time.Time{}, err }
|
|
return Normalize(parsed), nil
|
|
}
|
|
|
|
func Format(value time.Time) string { return Normalize(value).Format(time.RFC3339Nano) }
|