亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何計算時區差異

如何計算時區差異

Go
jeck貓 2022-08-15 10:02:39
我認為最好從目標開始: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]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//////        } 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 gmtNSYE opens at 09:30:00  - It opens in 00:01:00 minutes.LSE opens at 08:00:00 GMT - It is currently open.我正確地獲取了用戶的時間區,它返回一個包含偏移量的時間對象。我的解決方案可以完全改變。去時區
查看完整描述

1 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

您可以轉換為今天/tomo日期并與當前時間進行比較


package main


import (

    "fmt"

    "strconv"

    "strings"

    "time"

)


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

            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()

}


查看完整回答
反對 回復 2022-08-15
  • 1 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號