Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.73 KB | None | 0 0
  1. //get duration between 0:00:00 UTC and start shift by UTC
  2. func getUTCDiffStartShift(loc string, startShift time.Duration) (time.Duration, error) {
  3.     locDiff, err := getLocationDiff(loc)
  4.     if err != nil {
  5.         return time.Duration(0), fmt.Errorf("can not diff between 0:00:00 and start shift: %v", err)
  6.     }
  7.     return startShift - locDiff, nil
  8. }
  9.  
  10. //get different between UTC and location as duration
  11. func getLocationDiff(loc string) (time.Duration, error) {
  12.     location, err := time.LoadLocation(loc)
  13.     if err != nil {
  14.         return time.Duration(0), err
  15.     }
  16.     in := time.Now().In(location)
  17.     locDiff, err := strconv.ParseFloat(in.Format("-07"), 64)
  18.     if err != nil {
  19.         return time.Duration(0), err
  20.     }
  21.     return time.Duration(locDiff) * time.Hour, nil
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement