Skip to content

Instantly share code, notes, and snippets.

@msadakov
Last active October 16, 2023 09:59
Show Gist options
  • Save msadakov/cdbbd979140ef7341fcfac970fc8a95b to your computer and use it in GitHub Desktop.
Save msadakov/cdbbd979140ef7341fcfac970fc8a95b to your computer and use it in GitHub Desktop.
Golang. Round up the time
package main
import (
"fmt"
"time"
)
func roundUpTime(t time.Time, roundOn time.Duration) time.Time {
t = t.Round(roundOn)
if time.Since(t) >= 0 {
t = t.Add(roundOn)
}
return t
}
func main() {
t := time.Now()
fmt.Println(t)
interval := 60 * time.Minute
fmt.Println(roundUpTime(t, interval))
}
// https://goplay.space/#j3YHYs9uDD
// https://play.golang.org/p/j3YHYs9uDD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment