为了账号安全,请及时绑定邮箱和手机立即绑定

如何计算时区差异

如何计算时区差异

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 关注
  • 89 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信