The following PHP code that allows a process to only run between certain times. How would this be done in GoLang?
$curdate = date('Y-m-d');
$mydate=getdate(strtotime($curdate));
if ( $mydate['wday'] === 0 ) {
if ( date('H') < 15 ) { exit; }; // This is for 0 Sunday!!!
}
if ( $mydate['wday'] === 5 ) {
if ( date('H') > 19 ) { exit; }; // This is for 5 Friday!!!
}
if ( $mydate['wday'] === 6 ) {
exit; // This is for 6 Saturday //
}
This should do the same thing:
now := time.Now()
day := now.Weekday()
hr := now.Hour()
if day == 0 {
if hr < 15 { os.Exit(0) }
}
if day == 5 {
if hr > 19 { os.Exit(0) }
}
if day == 6 {
os.Exit(0)
}
Where similarly, each day can be represented by an integer (0 - 6).
Note that to use time
and os
you will need to call
import "time"
import "os"
See the documentation for more about Golang time.
Don't write PHP code as Go code. Write Go code. For example,
package main
import (
"os"
"time"
)
func main() {
now := time.Now()
hour := now.Hour()
switch now.Weekday() {
case time.Sunday:
if hour < 15 {
os.Exit(0)
}
case time.Friday:
if hour > 19 {
os.Exit(0)
}
case time.Saturday:
os.Exit(0)
}
// Do Something
}