Skip to content

Commit

Permalink
chore: optimizes time variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs committed Dec 9, 2024
1 parent f3d06a2 commit 7f46132
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
19 changes: 12 additions & 7 deletions internal/corazawaf/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -1612,15 +1612,20 @@ func (tx *Transaction) generateResponseBodyError(err error) {
// setTimeVariables sets all the time variables
func (tx *Transaction) setTimeVariables() {
timestamp := time.Unix(0, tx.Timestamp)
tx.variables.time.Set(timestamp.Format(time.TimeOnly))
tx.variables.timeDay.Set(strconv.Itoa(timestamp.Day()))
tx.variables.timeEpoch.Set(strconv.FormatInt(timestamp.Unix(), 10))
tx.variables.timeHour.Set(strconv.Itoa(timestamp.Hour()))
tx.variables.timeMin.Set(strconv.Itoa(timestamp.Minute()))
tx.variables.timeSec.Set(strconv.Itoa(timestamp.Second()))

timeOnly := timestamp.Format(time.TimeOnly)
tx.variables.time.Set(timeOnly)
tx.variables.timeHour.Set(timeOnly[0:2])
tx.variables.timeMin.Set(timeOnly[3:5])
tx.variables.timeSec.Set(timeOnly)

y, m, d := timestamp.Date()
tx.variables.timeDay.Set(strconv.Itoa(d))
tx.variables.timeMon.Set(strconv.Itoa(int(m)))
tx.variables.timeYear.Set(strconv.Itoa(y))

tx.variables.timeWday.Set(strconv.Itoa(int(timestamp.Weekday())))
tx.variables.timeMon.Set(strconv.Itoa(int(timestamp.Month())))
tx.variables.timeYear.Set(strconv.Itoa(timestamp.Year()))
}

// TransactionVariables has pointers to all the variables of the transaction
Expand Down
9 changes: 9 additions & 0 deletions internal/corazawaf/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,15 @@ func makeTransactionTimestamped(t testing.TB) *Transaction {
return tx
}

func BenchmarkTransactionTimestamped(b *testing.B) {
tx := NewWAF().NewTransaction()
tx.Timestamp = time.Now().Unix()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tx.setTimeVariables()
}
}

func makeTransactionMultipart(t *testing.T) *Transaction {
if t != nil {
t.Helper()
Expand Down

0 comments on commit 7f46132

Please sign in to comment.