Hanlde trading212 inconsistent date formats
Generate check / check-changes (pull_request) Successful in 2s
Generate check / verify-generate (pull_request) Skipped
Quality / check-changes (pull_request) Successful in 3s
Quality / static-checks (pull_request) Successful in 21s
Quality / run-tests (pull_request) Successful in 52s
Generate check / check-changes (pull_request) Successful in 2s
Generate check / verify-generate (pull_request) Skipped
Quality / check-changes (pull_request) Successful in 3s
Quality / static-checks (pull_request) Successful in 21s
Quality / run-tests (pull_request) Successful in 52s
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user