如何计算时区差异

我认为最好从目标开始:


NSYE opens at 09:30:00  - It opens in X minutes/It closes in Y minutes.

LSE opens at 08:00:00 GMT - It opens in X minutes/It closes in Y minutes.

当前代码:


type StockExchanges map[string]string


func 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//////

        } 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

}

因此,对于其时区的用户,我想看看所有股票交易所的开盘时间之前需要多长时间。currentTime


例如:假设您在GMT,纽约证券交易所直到,所以假设当前时间是,我希望输出是:14:30:00 GMT14:29:00 gmt


NSYE opens at 09:30:00  - It opens in 00:01:00 minutes.

LSE opens at 08:00:00 GMT - It is currently open.

我正确地获取了用户的时间区,它返回一个包含偏移量的时间对象。我的解决方案可以完全改变。


时区


jeck猫
浏览 90回答 1
1回答

慕运维8079593

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

相关分类

Go