Sobes.tech
Senior

How to verify the correctness of a booking time range (from < to)?

sobes.tech AI

Answer from AI

To verify the correctness of a booking time range in Go, you need to ensure that the start time (from) is strictly less than the end time (to). Typically, the time.Time type is used for this.

Example check:

package main

import (
    "fmt"
    "time"
)

func isValidBookingRange(from, to time.Time) bool {
    return from.Before(to)
}

func main() {
    from := time.Date(2024, 6, 1, 14, 0, 0, 0, time.UTC)
    to := time.Date(2024, 6, 1, 16, 0, 0, 0, time.UTC)

    if isValidBookingRange(from, to) {
        fmt.Println("The time range is valid")
    } else {
        fmt.Println("The time range is invalid")
    }
}

If from is not less than to, then the range is invalid and the booking should be rejected or corrected.