Created
September 23, 2019 10:13
-
-
Save Azhovan/98e880b1979dacead612d01640b46236 to your computer and use it in GitHub Desktop.
golang time utility functions
This file contains hidden or 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 util | |
import ( | |
"time" | |
) | |
// convert hour to timestamp | |
// this function assume the parameter as an hour in a day | |
// for example 5 means today 5 AM | |
// or 16 mean today 16 PM | |
func (s *Snapshots) Clock(i int) int { | |
t, _ := timezone() | |
clk := time.Date(year(), month(), day(), i, Minute, Second, NanoSecond, t).Unix() | |
return int(clk) | |
} | |
// convert seconds to hour | |
// I don't use math.Floor, since technically 100.x > 100.0 if and if x > 0 | |
func Hours(seconds int) float64 { | |
return float64(seconds) / 3600 | |
} | |
// get the timezone | |
func timezone() (*time.Location, error) { | |
return time.LoadLocation(TimeZone) | |
} | |
// return year | |
func year() int { | |
return time.Now().Year() | |
} | |
// return month | |
func month() time.Month { | |
return time.Now().Month() | |
} | |
// return day | |
func day() int { | |
return time.Now().Day() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment