Last active
October 16, 2023 09:59
-
-
Save msadakov/cdbbd979140ef7341fcfac970fc8a95b to your computer and use it in GitHub Desktop.
Golang. Round up the time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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