package edgeutils import ( "fmt" "regexp" "time" ) func IsValidTimestamp(time string) error { // ISO 8601 regex validRegex := "^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])(?:T|\\s)(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])?(Z)?$" compiledRegex := regexp.MustCompile(validRegex) if !compiledRegex.MatchString(time) { return fmt.Errorf("Timestamp %s is not a valid Timestamp", time) } return nil } func TimeSequenceCheck(start string, end string) error { // EX: 2024-04-25T15:34:12Z timeInputStart, err := time.Parse(time.RFC3339, start) if err != nil { return fmt.Errorf("Start Timestamp %s was not able to be parsed as a Time Object", start) } timeInputEnd, err := time.Parse(time.RFC3339, end) if err != nil { return fmt.Errorf("End Timestamp %s was not able to be parsed as a Time Object", end) } if timeInputEnd.Before(timeInputStart) { return fmt.Errorf("End Timestamp %s comes before the start time %s", end, start) } return nil } func ConvertToRFC3339(timeInput string) (string, error) { // input := "2024-05-01T08:04:12-04:00" loc, _ := time.LoadLocation("UTC") expected, err := time.Parse(time.RFC3339, timeInput) if err != nil { return "", fmt.Errorf("Date Formatting Error: %w", err) } res := expected.In(loc) newRes := res.Format(time.RFC3339) return newRes, nil }