Skip to content

Commit

Permalink
Update time.go
Browse files Browse the repository at this point in the history
  • Loading branch information
shenghui0779 committed Jun 1, 2024
1 parent 24e0254 commit df956d2
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ var GMT8 = time.FixedZone("CST", 8*3600)

// TimeToStr 时间戳格式化为时间字符串
// 若 timestamp < 0,则使用 `time.Now()`
func TimeToStr(layout string, timestamp int64) string {
func TimeToStr(layout string, timestamp int64, loc *time.Location) string {
if timestamp < 0 {
return time.Now().In(time.Local).Format(layout)
}
return time.Unix(timestamp, 0).In(time.Local).Format(layout)
return time.Unix(timestamp, 0).In(loc).Format(layout)
}

// StrToTime 时间字符串解析为时间戳
func StrToTime(layout, datetime string) time.Time {
t, _ := time.ParseInLocation(layout, datetime, time.Local)
func StrToTime(layout, datetime string, loc *time.Location) time.Time {
t, _ := time.ParseInLocation(layout, datetime, loc)
return t
}

Expand Down

0 comments on commit df956d2

Please sign in to comment.