grain/server/utils/adjustTimeContraints.go

43 lines
1.7 KiB
Go
Raw Permalink Normal View History

2024-10-18 15:22:01 +00:00
package utils
import (
"fmt"
config "grain/config/types"
"strings"
"time"
)
// Adjusts the event time constraints based on the configuration
func AdjustEventTimeConstraints(cfg *config.ServerConfig) {
now := time.Now()
2024-10-18 15:33:23 +00:00
// Adjust min_created_at based on string value or default to January 1, 2020
if strings.HasPrefix(cfg.EventTimeConstraints.MinCreatedAtString, "now") {
offset := strings.TrimPrefix(cfg.EventTimeConstraints.MinCreatedAtString, "now")
duration, err := time.ParseDuration(offset)
if err != nil {
fmt.Printf("Invalid time offset for min_created_at: %s\n", offset)
cfg.EventTimeConstraints.MinCreatedAt = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
} else {
cfg.EventTimeConstraints.MinCreatedAt = now.Add(duration).Unix()
}
} else if cfg.EventTimeConstraints.MinCreatedAt == 0 {
// Default to January 1, 2020, if not set
2024-10-18 15:22:01 +00:00
cfg.EventTimeConstraints.MinCreatedAt = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
}
2024-10-18 15:33:23 +00:00
// Adjust max_created_at based on string value or default to current time
2024-10-18 15:22:01 +00:00
if strings.HasPrefix(cfg.EventTimeConstraints.MaxCreatedAtString, "now") {
offset := strings.TrimPrefix(cfg.EventTimeConstraints.MaxCreatedAtString, "now")
duration, err := time.ParseDuration(offset)
if err != nil {
fmt.Printf("Invalid time offset for max_created_at: %s\n", offset)
cfg.EventTimeConstraints.MaxCreatedAt = now.Unix() // Default to now if parsing fails
} else {
cfg.EventTimeConstraints.MaxCreatedAt = now.Add(duration).Unix()
}
} else if cfg.EventTimeConstraints.MaxCreatedAt == 0 {
// Default to the current time if it's set to zero and no "now" keyword is used
cfg.EventTimeConstraints.MaxCreatedAt = now.Unix()
}
}