I'm working on a little side project of mine. I want to create a rule set that says, "Do this task daily before this time." For example, if it's in the morning, the deadline could be 09:00 or 10:00. For the afternoon, it could be 13:00 or 14:29. You get the idea.
So I was getting the time as 09:00 or 15:28. Here came the interesting part. I wanted to compare the times. If I submitted the report late, I wanted to know how many minutes or hours late it was.
What I was doing was parsing both the expected time and the submitted time.
t, err := time.Parse("15:04", input.Time)
if err != nil {
log.Fatal(err)
}
deadline := "15:34"
want, err := time.Parse("15:04", deadline)
if err != nil {
log.Fatal(err)
}
if t.Hour() > want.Hour() && t.Minute() > want.Minute() {
lateMinute := want.Minute() - t.Minute()
lateHour := want.Hour() - t.Hour()
msg := fmt.Sprintf("Late submission by %d hour %d minutes", lateHour, lateMinute)
fmt.Println(msg)
}
The code above doesn't work because I was comparing the times incorrectly.
Then I asked ChatGPT what I was doing wrong. It pointed out this line:
if t.Hour() > want.Hour() && t.Minute() > want.Minute() {
// Code omitted of brevity
}
This only returns true if both the hour and the minute are greater.
It also told me that since time.Parse returns a time.Time, I could compare the times directly. It gave me the following code:
t, _ := time.Parse("15:04", input.Time)
want, _ := time.Parse("15:04", "15:34")
if t.After(want) {
diff := t.Sub(want)
hours := int(diff.Hours())
minutes := int(diff.Minutes()) % 60
msg := fmt.Sprintf(
"Late submission by %d hour(s) %d minute(s)",
hours,
minutes,
)
fmt.Println(msg)
}
Example output:
15:35 -> Late by 0 hour(s) 1 minute(s)
16:34 -> Late by 1 hour(s) 0 minute(s)
17:45 -> Late by 2 hour(s) 11 minute(s)
Everything worked, but ChatGPT didn't explain what t.Sub(want) actually does.
I could have easily asked ChatGPT another question, but there was a better way.
I could check it directly from the terminal without using any external tools. Since I already have Go installed, I can simply run:
go doc time.Sub
Pretty sweet, right?
go doc time.Sub shows the documentation for that specific function. But if you want to learn more about the package itself, including its documentation, constants, variables, types, and other exported functions, just run:
go doc time
Replace time with any package name you're interested in. I find this especially useful when I'm exploring a package I haven't used before. Instead of opening my browser or searching online, I can quickly get an overview of everything the package provides without leaving my terminal.
It gets even better.
If you want to view the source code of the function, just run:
go doc --src time.Sub
I think that's awesome.
There's another handy command as well. If you want to browse Go's standard library documentation locally, just run:
go doc -http
It's pretty awesome. Another reason why I love Go.
Today's post wasn't really about comparing times. It was about another handy Go feature that I enjoy using. I just wanted to share it with you all.