mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-16 02:32:28 +08:00
feat: add timezone support for FormatTimeToStr and FormatStrToTime
This commit is contained in:
@@ -124,19 +124,35 @@ func GetNightTimestamp() int64 {
|
|||||||
|
|
||||||
// FormatTimeToStr convert time to string.
|
// FormatTimeToStr convert time to string.
|
||||||
// Play: https://go.dev/play/p/_Ia7M8H_OvE
|
// 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])
|
return t.Format(timeFormat[format])
|
||||||
}
|
}
|
||||||
|
|
||||||
// FormatStrToTime convert string to time.
|
// FormatStrToTime convert string to time.
|
||||||
// Play: https://go.dev/play/p/1h9FwdU8ql4
|
// Play: https://go.dev/play/p/1h9FwdU8ql4
|
||||||
func FormatStrToTime(str, format string) (time.Time, error) {
|
func FormatStrToTime(str, format string, timezone ...string) (time.Time, error) {
|
||||||
v, ok := timeFormat[format]
|
tf, ok := timeFormat[format]
|
||||||
if !ok {
|
if !ok {
|
||||||
return time.Time{}, fmt.Errorf("format %s not found", format)
|
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.
|
// BeginOfMinute return beginning minute time of day.
|
||||||
|
|||||||
@@ -147,6 +147,9 @@ func TestFormatTimeToStr(t *testing.T) {
|
|||||||
actual := FormatTimeToStr(datetime, cases[i])
|
actual := FormatTimeToStr(datetime, cases[i])
|
||||||
assert.Equal(expected[i], actual)
|
assert.Equal(expected[i], actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ds := FormatTimeToStr(datetime, "yyyy-mm-dd hh:mm:ss", "EST")
|
||||||
|
t.Log(ds)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFormatStrToTime(t *testing.T) {
|
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",
|
"dd-mm-yy hh:mm:ss", "yyyy/mm/dd hh:mm:ss",
|
||||||
"yyyy/mm"}
|
"yyyy/mm"}
|
||||||
|
|
||||||
datetimeStr := []string{
|
expected := []string{
|
||||||
"2021-01-02 16:04:08", "2021-01-02",
|
"2021-01-02 16:04:08", "2021-01-02",
|
||||||
"02-01-21 16:04:08", "2021/01/02 16:04:08",
|
"02-01-21 16:04:08", "2021/01/02 16:04:08",
|
||||||
"2021/01"}
|
"2021/01"}
|
||||||
|
|
||||||
for i := 0; i < len(cases); i++ {
|
for i := 0; i < len(cases); i++ {
|
||||||
actual, err := FormatStrToTime(datetimeStr[i], cases[i])
|
actual, err := FormatStrToTime(expected[i], cases[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
expected, _ := time.Parse(formats[i], datetimeStr[i])
|
expected, _ := time.Parse(formats[i], expected[i])
|
||||||
assert.Equal(expected, actual)
|
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) {
|
func TestBeginOfMinute(t *testing.T) {
|
||||||
|
|||||||
@@ -838,7 +838,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func FormatTimeToStr(t time.Time, format string) string
|
func FormatTimeToStr(t time.Time, format string, timezone ...string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
@@ -877,7 +877,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func FormatStrToTime(str, format string) (time.Time, error)
|
func FormatStrToTime(str, format string, timezone ...string) (time.Time, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|||||||
@@ -838,7 +838,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func FormatTimeToStr(t time.Time, format string) string
|
func FormatTimeToStr(t time.Time, format string, timezone ...string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
@@ -877,7 +877,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func FormatStrToTime(str, format string) (time.Time, error)
|
func FormatStrToTime(str, format string, timezone ...string) (time.Time, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:</b>
|
||||||
|
|||||||
Reference in New Issue
Block a user