Skip to content

Commit

Permalink
round timestamps in time-related template functions used in godog tes…
Browse files Browse the repository at this point in the history
…ts when needed
  • Loading branch information
zenovich committed Aug 20, 2024
1 parent ad1e777 commit 3e47db6
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions testhelpers/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ func (ctx *TestContext) constructTemplateSet() *jet.Set {
})

set.AddGlobalFunc("currentTimeDB", func(a jet.Arguments) reflect.Value {
return reflect.ValueOf(time.Now().UTC().Format("2006-01-02 15:04:05.999999"))
return reflect.ValueOf(time.Now().UTC().Round(time.Microsecond).Format("2006-01-02 15:04:05.999999"))
})

set.AddGlobalFunc("currentTimeDBMs", func(a jet.Arguments) reflect.Value {
return reflect.ValueOf(time.Now().UTC().Format("2006-01-02 15:04:05.000"))
return reflect.ValueOf(time.Now().UTC().Round(time.Millisecond).Format("2006-01-02 15:04:05.000"))
})

set.AddGlobalFunc("generateToken", func(a jet.Arguments) reflect.Value {
Expand Down Expand Up @@ -148,7 +148,7 @@ func (ctx *TestContext) constructTemplateSet() *jet.Set {
if err != nil {
a.Panicf("can't parse duration: %s", err.Error())
}
return reflect.ValueOf(time.Now().UTC().Add(duration).Format("2006-01-02 15:04:05"))
return reflect.ValueOf(time.Now().UTC().Add(duration).Round(time.Second).Format("2006-01-02 15:04:05"))
})

addRelativeTimeDBMsFunction(set)
Expand All @@ -169,18 +169,19 @@ func addRelativeTimeDBMsFunction(set *jet.Set) *jet.Set {
if err != nil {
a.Panicf("can't parse duration: %s", err.Error())
}
return reflect.ValueOf(time.Now().UTC().Add(duration).Format("2006-01-02 15:04:05.000"))
return reflect.ValueOf(time.Now().UTC().Add(duration).Round(time.Millisecond).Format("2006-01-02 15:04:05.000"))
})
}

func addTimeDBToRFCFunction(set *jet.Set) {
set.AddGlobalFunc("timeDBToRFC", func(a jet.Arguments) reflect.Value {
a.RequireNumOfArguments("timeDBToRFC", 1, 1)
dbTime := a.Get(0).Interface().(string)
parsedTime, err := time.Parse("2006-01-02 15:04:05", dbTime)
parsedTime, err := time.Parse("2006-01-02 15:04:05.999999999", dbTime)
if err != nil {
a.Panicf("can't parse mysql datetime: %s", err.Error())
}
parsedTime = parsedTime.Round(time.Second)
return reflect.ValueOf(parsedTime.Format(time.RFC3339))
})
}
Expand Down

0 comments on commit 3e47db6

Please sign in to comment.