Time distance

Report a typo

Below is a Go program that takes a year, month and day values as input, creates a date using those values, and uses the now variable to represent the current time.

Your task is to compare the date and now variables using the After() and Sub() functions and print a string according to the following conditions:

  • "This date will be in the future" — if the input date is any date after the current time (now);

  • "This date was in the past hundred years" — if the time difference between now and date is less than the current centuryDuration;

  • "It was a long time ago" — if none of the above conditions are met.

Sample Input 1:

2151 6 16

Sample Output 1:

This date will be in the future

Sample Input 2:

2010 12 4

Sample Output 2:

This date was in the past hundred years

Sample Input 3:

1 1 1

Sample Output 3:

It was a long time ago...
Write a program in Go
package main

import (
"fmt"
"time"
)

const (
HoursPerDay = 24
DaysPerYear = 365
Century = 100
)

func main() {
var year, month, day int
fmt.Scan(&year, &month, &day)

// Create the `date` using the input `year`, `month`, and `day`:
date := time.Date(?, ?, ?, 0, 0, 0, 0, time.UTC)

yearDuration := time.Hour * HoursPerDay * DaysPerYear
centuryDuration := yearDuration * Century

now := time.Now()

// Write the missing code below to compare the `date` and `now` variables:
switch {
case ?.After(?):
fmt.Println("This date will be in the future")
case now.?(?) < centuryDuration:
fmt.Println("This date was in the past hundred years")
default:
fmt.Println("It was a long time ago...")
}
}
___

Create a free account to access the full topic