Skip to content

Commit

Permalink
chore: optimizes time variables. (#1242)
Browse files Browse the repository at this point in the history
* chore: optimizes time variables.

* fix: test.
  • Loading branch information
jcchavezs authored Dec 9, 2024
1 parent f3d06a2 commit 8176c09
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[6:8])

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 8176c09

Please sign in to comment.