package marketdate import ( "reflect" "strings" "time" ) // HandleMarketDatetime 根据市场和时间字段处理datetime格式 func HandleMarketDatetimeEx(data map[string]interface{}, datetimeField string) string { marketField := "market" format := "2006-01-02 15:04:05" market, ok := data[marketField].(string) if !ok || market == "" { if datetimeStr, ok := data[datetimeField].(string); ok { return datetimeStr } return "" } datetimeStr, ok := data[datetimeField].(string) if !ok || datetimeStr == "" { return "" } if strings.Contains(strings.ToUpper(market), "HK") { loc, _ := time.LoadLocation("Asia/Hong_Kong") t, _ := time.ParseInLocation("2006-01-02 15:04:05", datetimeStr, time.Local) return t.In(loc).Format(format) } return datetimeStr } func HandleMarketDatetime(data any) string { format := "2006-01-02 15:04:05" dataVal := reflect.ValueOf(data) dataType := dataVal.Type() var datetimeField, marketField reflect.Value // 通过反射找到标签为"datetimeField"和"market"的字段 for i := 0; i < dataType.NumField(); i++ { field := dataType.Field(i) jsonTag := field.Tag.Get("json") if jsonTag == "datetimeField" { datetimeField = dataVal.Field(i) } else if jsonTag == "market" { marketField = dataVal.Field(i) } } // 检查字段是否为期望的类型 if datetimeField.Kind() != reflect.Ptr || datetimeField.Type().Elem().Kind() != reflect.Struct || datetimeField.IsNil() { return "" } if marketField.Kind() != reflect.Ptr || marketField.Type().Elem().Kind() != reflect.String || marketField.IsNil() { return "" } // 获取字段值 datetimeVal := datetimeField.Elem().Interface().(*time.Time) marketVal := marketField.Elem().Interface().(*string) // 判断市场和格式化时间 if strings.Contains(strings.ToUpper(*marketVal), "HK") { loc, _ := time.LoadLocation("Asia/Hong_Kong") return datetimeVal.In(loc).Format(format) } return datetimeVal.Format(format) }