Time travel trip verification

Report a typo

Imagine you are a Go developer at a time travel agency and need to create a program that schedules time travel trips for clients. Each trip has three time points: departure date, historical event date, and return date. However, the program must validate if the trip is valid based on the following rules:

  • The historicalEventDate must be in the past relative to the departureDate.

  • The returnDate must be in the future relative to the departureDate.

  • All travels are only to the past!

Your task is to write the additional required code for the readDate() function to create a Date object from the input dates, and the code for the conditional statements to check if the trip is valid.

If the trip is valid, your program should print "Valid trip!" and if the trip is not valid it should print "Invalid trip!".

Sample Input 1:

2023 01 01
1969 07 20
2023 01 02

Sample Output 1:

Valid trip!

Sample Input 2:

2023 01 01
2024 07 20
2023 01 02

Sample Output 2:

Invalid trip!
Write a program in Go
package main

import (
"fmt"
"time"
)

func readDate() time.Time {
// DO NOT MODIFY the code block below!
var year, month, day int
fmt.Scan(&year, &month, &day)

// Write the code here to create a time.Time object from the input
date := time.Date(?, time.Month(?), ?, 0, 0, 0, 0, time.UTC)

return ?
}

func main() {
departureDate := readDate()
historicalEventDate := readDate()
returnDate := readDate()

// Write the code here to check if the trip is valid using the `Before()` and `After()` methods:
?
}

Create a free account to access the full topic