时间戳改成string可读型
This commit is contained in:
parent
092009080b
commit
64fe4a18b0
@ -9,6 +9,15 @@ import (
|
|||||||
"time"
|
"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数组并提供序列化方法
|
// CandleList 封装Candle数组并提供序列化方法
|
||||||
type CandleList struct {
|
type CandleList struct {
|
||||||
Candles []*Candle
|
Candles []*Candle
|
||||||
@ -46,13 +55,35 @@ func MakeCandleList(instId string, period string, startTime time.Time, endTime t
|
|||||||
|
|
||||||
// ToJson 将Candle数组序列化为JSON字符串
|
// ToJson 将Candle数组序列化为JSON字符串
|
||||||
func (cl *CandleList) ToJson() (string, error) {
|
func (cl *CandleList) ToJson() (string, error) {
|
||||||
// 调试信息
|
// 创建临时结构体用于格式化时间戳
|
||||||
fmt.Printf("Number of candles: %d\n", len(cl.Candles))
|
type FormattedCandle struct {
|
||||||
// for i, candle := range cl.Candles {
|
Timestamp string `json:"timestamp"`
|
||||||
// fmt.Printf("Writing candle %d: %+v\n", i, candle)
|
Open string `json:"open"`
|
||||||
// }
|
High string `json:"high"`
|
||||||
//
|
Low string `json:"low"`
|
||||||
jsonData, err := json.Marshal(cl.Candles)
|
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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -74,9 +105,12 @@ func (cl *CandleList) ToCsv() (string, error) {
|
|||||||
fmt.Printf("Number of candles: %d\n", len(cl.Candles))
|
fmt.Printf("Number of candles: %d\n", len(cl.Candles))
|
||||||
|
|
||||||
for _, candle := range 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{
|
record := []string{
|
||||||
candle.Timestamp,
|
formattedTs,
|
||||||
candle.Open,
|
candle.Open,
|
||||||
candle.High,
|
candle.High,
|
||||||
candle.Low,
|
candle.Low,
|
||||||
@ -87,8 +121,6 @@ func (cl *CandleList) ToCsv() (string, error) {
|
|||||||
if err := writer.Write(record); err != nil {
|
if err := writer.Write(record); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
// 调试信息
|
|
||||||
// fmt.Printf("Writing candle %d: %+v\n", i, candle)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.Flush()
|
writer.Flush()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user