时间戳改成string可读型
This commit is contained in:
		
							parent
							
								
									092009080b
								
							
						
					
					
						commit
						64fe4a18b0
					
				| @ -9,6 +9,15 @@ import ( | ||||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| // formatTimestamp 将毫秒时间戳字符串转换为可读格式 | ||||
| func formatTimestamp(ts string) (string, error) { | ||||
| 	millis, err := strconv.ParseInt(ts, 10, 64) | ||||
| 	if err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	return time.UnixMilli(millis).Format("2006-01-02 15:04:05"), nil | ||||
| } | ||||
| 
 | ||||
| // CandleList 封装Candle数组并提供序列化方法 | ||||
| type CandleList struct { | ||||
| 	Candles []*Candle | ||||
| @ -46,13 +55,35 @@ func MakeCandleList(instId string, period string, startTime time.Time, endTime t | ||||
| 
 | ||||
| // ToJson 将Candle数组序列化为JSON字符串 | ||||
| func (cl *CandleList) ToJson() (string, error) { | ||||
| 	// 调试信息 | ||||
| 	fmt.Printf("Number of candles: %d\n", len(cl.Candles)) | ||||
| 	// for i, candle := range cl.Candles { | ||||
| 	// 	fmt.Printf("Writing candle %d: %+v\n", i, candle) | ||||
| 	// } | ||||
| 	// | ||||
| 	jsonData, err := json.Marshal(cl.Candles) | ||||
| 	// 创建临时结构体用于格式化时间戳 | ||||
| 	type FormattedCandle struct { | ||||
| 		Timestamp string `json:"timestamp"` | ||||
| 		Open      string `json:"open"` | ||||
| 		High      string `json:"high"` | ||||
| 		Low       string `json:"low"` | ||||
| 		Close     string `json:"close"` | ||||
| 		Volume    string `json:"volume"` | ||||
| 		VolumeCcy string `json:"volumeCcy"` | ||||
| 	} | ||||
| 
 | ||||
| 	var formattedCandles []FormattedCandle | ||||
| 	for _, candle := range cl.Candles { | ||||
| 		formattedTs, err := formatTimestamp(candle.Timestamp) | ||||
| 		if err != nil { | ||||
| 			return "", err | ||||
| 		} | ||||
| 		formattedCandles = append(formattedCandles, FormattedCandle{ | ||||
| 			Timestamp: formattedTs, | ||||
| 			Open:      candle.Open, | ||||
| 			High:      candle.High, | ||||
| 			Low:       candle.Low, | ||||
| 			Close:     candle.Close, | ||||
| 			Volume:    candle.Volume, | ||||
| 			VolumeCcy: candle.VolumeCcy, | ||||
| 		}) | ||||
| 	} | ||||
| 
 | ||||
| 	jsonData, err := json.Marshal(formattedCandles) | ||||
| 	if err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| @ -74,9 +105,12 @@ func (cl *CandleList) ToCsv() (string, error) { | ||||
| 	fmt.Printf("Number of candles: %d\n", len(cl.Candles)) | ||||
| 
 | ||||
| 	for _, candle := range cl.Candles { | ||||
| 		// fmt.Printf("Debug: Writing candle %d with Timestamp: %s\n", i, candle.Timestamp) | ||||
| 		formattedTs, err := formatTimestamp(candle.Timestamp) | ||||
| 		if err != nil { | ||||
| 			return "", err | ||||
| 		} | ||||
| 		record := []string{ | ||||
| 			candle.Timestamp, | ||||
| 			formattedTs, | ||||
| 			candle.Open, | ||||
| 			candle.High, | ||||
| 			candle.Low, | ||||
| @ -87,8 +121,6 @@ func (cl *CandleList) ToCsv() (string, error) { | ||||
| 		if err := writer.Write(record); err != nil { | ||||
| 			return "", err | ||||
| 		} | ||||
| 		// 调试信息 | ||||
| 		// fmt.Printf("Writing candle %d: %+v\n", i, candle) | ||||
| 	} | ||||
| 
 | ||||
| 	writer.Flush() | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zhangkun9038@dingtalk.com
						zhangkun9038@dingtalk.com