From 7463bada9af43fba1e99cb80ce90f70d9a2dfe46 Mon Sep 17 00:00:00 2001 From: wood chen Date: Wed, 18 Sep 2024 03:10:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B8=81=E5=AE=89=E4=BB=B7?= =?UTF-8?q?=E6=A0=BC=E6=92=AD=E6=8A=A5=E7=9A=84=E5=AE=9A=E6=97=B6=E5=92=8C?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=92=A4=E5=9B=9E=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/binance.go | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/service/binance.go b/service/binance.go index 07569c2..383dec3 100644 --- a/service/binance.go +++ b/service/binance.go @@ -33,8 +33,12 @@ func init() { symbols[i] = strings.ReplaceAll(s, "/", "") } - // 初始化 singaporeTZ - singaporeTZ = time.FixedZone("Asia/Singapore", 8*60*60) // UTC+8 + singaporeTZ, err = time.LoadLocation("Asia/Singapore") + if err != nil { + log.Printf("Error loading Singapore time zone: %v", err) + log.Println("Falling back to UTC+8") + singaporeTZ = time.FixedZone("Asia/Singapore", 8*60*60) + } bot, err = tgbotapi.NewBotAPI(botToken) if err != nil { @@ -59,7 +63,6 @@ type tickerInfo struct { func getTickerInfo(symbol string) (tickerInfo, error) { client := binance.NewClient("", "") - // 获取当前价格 ticker, err := client.NewListPricesService().Symbol(symbol).Do(context.Background()) if err != nil { return tickerInfo{}, err @@ -67,13 +70,11 @@ func getTickerInfo(symbol string) (tickerInfo, error) { if len(ticker) == 0 { return tickerInfo{}, fmt.Errorf("no ticker found for symbol %s", symbol) } - // 在 getTickerInfo 函数中 last, err := strconv.ParseFloat(ticker[0].Price, 64) if err != nil { return tickerInfo{}, err } - // 获取24小时价格变化 stats, err := client.NewListPriceChangeStatsService().Symbol(symbol).Do(context.Background()) if err != nil { return tickerInfo{}, err @@ -103,13 +104,7 @@ func formatChange(changePercent float64) string { } func sendPriceUpdate() { - var now time.Time - if singaporeTZ != nil { - now = time.Now().In(singaporeTZ) - } else { - now = time.Now().UTC() - log.Println("Warning: singaporeTZ is nil, using UTC") - } + now := time.Now().In(singaporeTZ) message := fmt.Sprintf("市场更新 - %s (SGT)\n\n", now.Format("2006-01-02 15:04:05")) for _, symbol := range symbols { @@ -147,11 +142,24 @@ func sendPriceUpdate() { func RunBinance() { log.Println("Starting Binance service...") - for { - log.Println("Sending price update...") - sendPriceUpdate() - log.Println("Waiting for next update...") - time.Sleep(1 * time.Hour) + sendPriceUpdate() + + var scheduleNextUpdate func() + scheduleNextUpdate = func() { + now := time.Now().In(singaporeTZ) + nextHour := now.Add(time.Hour).Truncate(time.Hour) + duration := nextHour.Sub(now) + + time.AfterFunc(duration, func() { + log.Println("Sending hourly price update...") + sendPriceUpdate() + scheduleNextUpdate() + }) } + + scheduleNextUpdate() + + // 保持主 goroutine 运行 + select {} }