Skip to content

Commit 7efa282

Browse files
committed
tests: add more tests for timestamp
1 parent 1c5dfc4 commit 7efa282

2 files changed

Lines changed: 158 additions & 169 deletions

File tree

dateutil.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,24 +214,11 @@ func (pattern *Pattern) Match(target string) (FormatResult, []int, bool) {
214214

215215
// StrToTime to timestamp
216216
func StrToTime(target interface{}) (int64, error) {
217-
switch t := target.(type) {
218-
case int64:
219-
return t, nil
220-
case int:
221-
return int64(t), nil
222-
case int32:
223-
return int64(t), nil
224-
case float64:
225-
return int64(t), nil
226-
case string:
227-
nowTime, err := DateTime(t)
228-
if err == nil {
229-
return nowTime.UTC().Unix(), nil
230-
}
231-
return 0, err
232-
default:
233-
return 0, fmt.Errorf("unsupport type %T for StrToTime func", t)
217+
nowTime, err := DateTime(target)
218+
if err == nil {
219+
return nowTime.Unix(), nil
234220
}
221+
return 0, err
235222
}
236223

237224
// DateTime func
@@ -321,8 +308,6 @@ func noEmptyField(target FormatResult, args ...string) string {
321308

322309
// translate result information to a time struct
323310
func makeFormatDateTime(result FormatResult) (time.Time, error) {
324-
// current time
325-
now := time.Now()
326311
// tz, tzcorrection
327312
var lastTime time.Time
328313
// set default timezone as 'Local'
@@ -352,8 +337,12 @@ func makeFormatDateTime(result FormatResult) (time.Time, error) {
352337
}
353338
// plain timezone, set hour/minute/second/nanoseconds to now time
354339
if (hasTimezone || needCorrection) && isResultTimezone(result) {
355-
lastTime = now.In(location)
340+
// use UTC time
341+
now := time.Now().UTC()
342+
lastTime = time.Date(now.Year(), time.Month(now.Month()), now.Day(), now.Hour(), now.Minute(), now.Second(), now.Nanosecond(), location)
356343
} else {
344+
// current time
345+
now := time.Now()
357346
// get full year of current
358347
year := now.Year()
359348
strYear := strconv.Itoa(year)

0 commit comments

Comments
 (0)