慕运维8079593
您可以转换为今天/tomo日期并与当前时间进行比较package mainimport ( "fmt" "strconv" "strings" "time")type StockExchanges map[string]stringfunc showOpeningTime() { for stockExchanges, stockExchange := range map[string]StockExchanges{ "NYSE": { "open": "09:30:00", "close": "16:00:00", "location": "America/New_York", }, "LSE": { "open": "08:00:00", "close": "16:00:00", "location": "Europe/London", }, } { currentTime, err := TimeIn(time.Now(), stockExchange) if err == nil { ///////CODE HERE////// hour, min, sec := getTime(stockExchange["open"]) TodaysStartTime := time.Date( currentTime.Year() ,currentTime.Month(),currentTime.Day() , hour,min,sec,0,currentTime.Location()) hour, min, sec = getTime(stockExchange["close"]) TodaysEndTime := time.Date( currentTime.Year() ,currentTime.Month(),currentTime.Day() , hour,min,sec,0,currentTime.Location()) if currentTime.Before(TodaysStartTime) { // NSYE opens at 09:30:00 - It opens in 00:01:00 minutes. fmt.Println(stockExchanges + " opens at ",stockExchange["open"] ," - It opens in ", TodaysStartTime.Sub(currentTime).String() ) }else if currentTime.Before(TodaysEndTime) { //opens at 08:00:00 GMT - It is currently open. fmt.Println(stockExchanges + " opens at ",stockExchange["open"] , " It is currently open" ) }else { // opens tomo hour, min, sec = getTime(stockExchange["open"]) tomo := currentTime.Add(24 * time.Hour) TomoStartTime := time.Date( tomo.Year() ,tomo.Month(),tomo.Day() , hour,min,sec,0,currentTime.Location()) // NSYE opens at 09:30:00 - It opens in 00:01:00 minutes. fmt.Println(stockExchanges + " opens at ",stockExchange["open"] ," - It opens in ", TomoStartTime.Sub(currentTime).String() ) } } else { fmt.Println(stockExchange, "<time unknown>") } } return}func TimeIn(t time.Time, stockExchanges StockExchanges) (time.Time, error) { loc, err := time.LoadLocation(stockExchanges["location"]) if err == nil { t = t.In(loc) } return t, err}func getTime(time string) (int,int,int){ t:= strings.Split(time,":") hour ,_ := strconv.ParseInt(t[0],10,64) min ,_ := strconv.ParseInt(t[1],10,64) sec ,_ := strconv.ParseInt(t[2],10,64) return int(hour),int(min),int(sec)}func main () { showOpeningTime()}