Skip to content

Instantly share code, notes, and snippets.

@zeddee
Created August 22, 2018 06:25
Show Gist options
  • Select an option

  • Save zeddee/fd0cca7745898aa19b2d1d021941eccd to your computer and use it in GitHub Desktop.

Select an option

Save zeddee/fd0cca7745898aa19b2d1d021941eccd to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"time"
)
// func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
func main() {
d, _ := time.ParseDuration("3h15s")
todayYear := time.Now().Year()
todayMonth := time.Now().Month()
todayDay := time.Now().Day()
loc, _ := time.LoadLocation("Asia/Singapore")
fmt.Printf("Three hours before now: %v", time.Now().In(loc).Add(-d))
openingTime := time.Date(todayYear, todayMonth, todayDay, 9, 0, 0, 0, loc)
closingTime := time.Date(todayYear, todayMonth, todayDay, 18, 0, 0, 0, loc)
now := time.Now().In(loc)
bookingTime, _ := time.ParseInLocation("2006-01-02 15:04", "2018-08-22 05:57", loc)
timeBeforeBooking := bookingTime.Sub(now)
bookingBuffer, _ := time.ParseDuration("3h15s")
log.Println(timeBeforeBooking, bookingTime, openingTime, closingTime, now, time.Now().In(loc))
switch {
case bookingTime.Before(now):
log.Println("failed: attempt to time travel")
case bookingTime.Before(openingTime):
log.Println("before opening time")
case bookingTime.After(closingTime):
log.Println("after closing time")
case timeBeforeBooking < bookingBuffer:
log.Println("please book more than 3 hours in advance.")
default:
log.Println("nil")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment