diff --git a/datetime/datetime.go b/datetime/datetime.go index 5a23954..d3a24a5 100644 --- a/datetime/datetime.go +++ b/datetime/datetime.go @@ -124,19 +124,35 @@ func GetNightTimestamp() int64 { // FormatTimeToStr convert time to string. // Play: https://go.dev/play/p/_Ia7M8H_OvE -func FormatTimeToStr(t time.Time, format string) string { +func FormatTimeToStr(t time.Time, format string, timezone ...string) string { + if timezone != nil && timezone[0] != "" { + loc, err := time.LoadLocation(timezone[0]) + if err != nil { + return "" + } + return t.In(loc).Format(timeFormat[format]) + } return t.Format(timeFormat[format]) } // FormatStrToTime convert string to time. // Play: https://go.dev/play/p/1h9FwdU8ql4 -func FormatStrToTime(str, format string) (time.Time, error) { - v, ok := timeFormat[format] +func FormatStrToTime(str, format string, timezone ...string) (time.Time, error) { + tf, ok := timeFormat[format] if !ok { return time.Time{}, fmt.Errorf("format %s not found", format) } - return time.Parse(v, str) + if timezone != nil && timezone[0] != "" { + loc, err := time.LoadLocation(timezone[0]) + if err != nil { + return time.Time{}, err + } + + return time.ParseInLocation(tf, str, loc) + } + + return time.Parse(tf, str) } // BeginOfMinute return beginning minute time of day. diff --git a/datetime/datetime_test.go b/datetime/datetime_test.go index 803c177..ebedfa9 100644 --- a/datetime/datetime_test.go +++ b/datetime/datetime_test.go @@ -147,6 +147,9 @@ func TestFormatTimeToStr(t *testing.T) { actual := FormatTimeToStr(datetime, cases[i]) assert.Equal(expected[i], actual) } + + ds := FormatTimeToStr(datetime, "yyyy-mm-dd hh:mm:ss", "EST") + t.Log(ds) } func TestFormatStrToTime(t *testing.T) { @@ -163,19 +166,23 @@ func TestFormatStrToTime(t *testing.T) { "dd-mm-yy hh:mm:ss", "yyyy/mm/dd hh:mm:ss", "yyyy/mm"} - datetimeStr := []string{ + expected := []string{ "2021-01-02 16:04:08", "2021-01-02", "02-01-21 16:04:08", "2021/01/02 16:04:08", "2021/01"} for i := 0; i < len(cases); i++ { - actual, err := FormatStrToTime(datetimeStr[i], cases[i]) + actual, err := FormatStrToTime(expected[i], cases[i]) if err != nil { t.Fatal(err) } - expected, _ := time.Parse(formats[i], datetimeStr[i]) + expected, _ := time.Parse(formats[i], expected[i]) assert.Equal(expected, actual) } + + estTime, err := FormatStrToTime("2021-01-02 16:04:08", "yyyy-mm-dd hh:mm:ss", "EST") + t.Log(estTime) + assert.IsNil(err) } func TestBeginOfMinute(t *testing.T) { diff --git a/docs/datetime.md b/docs/datetime.md index 6d29666..4f03994 100644 --- a/docs/datetime.md +++ b/docs/datetime.md @@ -838,7 +838,7 @@ func main() { Signature: ```go -func FormatTimeToStr(t time.Time, format string) string +func FormatTimeToStr(t time.Time, format string, timezone ...string) string ``` Example: @@ -877,7 +877,7 @@ func main() { Signature: ```go -func FormatStrToTime(str, format string) (time.Time, error) +func FormatStrToTime(str, format string, timezone ...string) (time.Time, error) ``` Example: diff --git a/docs/datetime_zh-CN.md b/docs/datetime_zh-CN.md index e2572c6..1669d0e 100644 --- a/docs/datetime_zh-CN.md +++ b/docs/datetime_zh-CN.md @@ -838,7 +838,7 @@ func main() { 函数签名: ```go -func FormatTimeToStr(t time.Time, format string) string +func FormatTimeToStr(t time.Time, format string, timezone ...string) string ``` 示例: @@ -877,7 +877,7 @@ func main() { 函数签名: ```go -func FormatStrToTime(str, format string) (time.Time, error) +func FormatStrToTime(str, format string, timezone ...string) (time.Time, error) ``` 示例: