如何计算时差
How to calculate timezone differences
我认为最好从目标开始:
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
在他们的时区,我想看看所有证券交易所开市前还有多长时间。
例如:假设您处于格林威治标准时间,纽约证券交易所直到 14:30:00 GMT
才开市,所以假设当前时间是 14: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.
我正确地得到了用户时区,它 returns 一个包含偏移量的时间对象。
我的解决方案可以彻底改变。
您可以转换为today/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()
}
我认为最好从目标开始:
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
在他们的时区,我想看看所有证券交易所开市前还有多长时间。
例如:假设您处于格林威治标准时间,纽约证券交易所直到 14:30:00 GMT
才开市,所以假设当前时间是 14: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.
我正确地得到了用户时区,它 returns 一个包含偏移量的时间对象。 我的解决方案可以彻底改变。
您可以转换为today/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()
}