Hanlde trading212 inconsistent date formats (#31)
Badges / coveralls (push) Successful in 30s

Reviewed-on: #31
Co-authored-by: Natercio Moniz <[email protected]>
This commit was merged in pull request #31.
This commit is contained in:
2026-09-06 18:44:22 +01:00
committed by natercio
parent 3c404699e5
commit ccef829832
2 changed files with 129 additions and 1 deletions
+25 -1
View File
@@ -114,7 +114,7 @@ func (rr RecordReader) ReadRecord(ctx context.Context) (internal.Record, error)
return Record{}, fmt.Errorf("parse record price: %w", err)
}
ts, err := time.Parse(time.DateTime, raw[1])
ts, err := parseTimestamp(raw[1])
if err != nil {
return Record{}, fmt.Errorf("parse record timestamp: %w", err)
}
@@ -225,6 +225,30 @@ func parseOptionalDecimal(s string) (decimal.Decimal, error) {
return parseDecimal(s)
}
// parseTimestamp parses a Trading212 timestamp string. Trading212 has shipped
// rows both without a timezone (treated here as UTC) and with an explicit
// numeric offset (e.g. "+00:00", "-05:00", "+01:00") or a "Z" UTC marker.
// The layouts are tried in order; the first match wins.
func parseTimestamp(s string) (time.Time, error) {
layouts := [3]string{
"2006-01-02 15:04:05-07:00", // numeric offset
time.DateTime, // "2006-01-02 15:04:05"
"2006-01-02 15:04:05Z", // explicit UTC marker
}
var (
lastErr error
ts time.Time
)
for _, layout := range layouts {
ts, lastErr = time.Parse(layout, s)
if lastErr == nil {
return ts, nil
}
}
return time.Time{}, lastErr
}
type splitRecord struct {
Record