Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
fix utc
Browse files Browse the repository at this point in the history
  • Loading branch information
glorv committed Jun 24, 2021
1 parent 205d0b1 commit 3bef1a6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
56 changes: 27 additions & 29 deletions pkg/lightning/mydump/parquet_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,42 +479,40 @@ func setDatumByInt(d *types.Datum, v int64, meta *parquet.SchemaElement) error {
d.SetString(dateStr, "")
case logicalType.TIMESTAMP != nil:
// convert all timestamp types (datetime/timestamp) to string
var sec, nsec int64
units := logicalType.TIMESTAMP.Unit
if units.MICROS != nil {
sec = v / 1e6
nsec = (v % 1e6) * 1e3
} else if units.MILLIS != nil {
sec = v / 1e3
nsec = (v % 1e3) * 1e6
} else {
// nano
sec = v / 1e9
nsec = v % 1e9
}
// TODO: how to deal with TimeZone if `IsAdjustedToUTC = false`
dateStr := time.Unix(sec, nsec).Format("2006-01-02 15:04:05.999")
d.SetString(dateStr, "")
timeStr := formatTime(v, logicalType.TIME.Unit, "2006-01-02 15:04:05.999999",
"2006-01-02 15:04:05.999999Z", logicalType.TIME.IsAdjustedToUTC)
d.SetString(timeStr, "")
case logicalType.TIME != nil:
units := logicalType.TIME.Unit
if units.NANOS != nil {
v /= 1e6
} else if units.MICROS != nil {
v /= 1e3
}
millis := v % 1e3
v /= 1e3
sec := v % 60
v /= 60
min := v % 60
v /= 60
d.SetString(fmt.Sprintf("%d:%d:%d.%3d", v, min, sec, millis), "")
// convert all timestamp types (datetime/timestamp) to string
timeStr := formatTime(v, logicalType.TIME.Unit, "15:04:05.999999", "15:04:05.999999Z",
logicalType.TIME.IsAdjustedToUTC)
d.SetString(timeStr, "")
default:
d.SetInt64(v)
}
return nil
}

func formatTime(v int64, units *parquet.TimeUnit, format, utcFormat string, utc bool) string {
var sec, nsec int64
if units.MICROS != nil {
sec = v / 1e6
nsec = (v % 1e6) * 1e3
} else if units.MILLIS != nil {
sec = v / 1e3
nsec = (v % 1e3) * 1e6
} else {
// nano
sec = v / 1e9
nsec = v % 1e9
}
t := time.Unix(sec, nsec)
if utc {
return t.UTC().Format(utcFormat)
}
return t.Format(format)
}

func (pp *ParquetParser) LastRow() Row {
return pp.lastRow
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/lightning/mydump/parquet_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (s testParquetParserSuite) TestParquetVariousTypes(c *C) {

c.Assert(reader.ReadRow(), IsNil)
rowValue := []string{
"2020-10-29", "17:26:15.123", "17:26:15.123", "2020-10-29 17:27:52.356", "2020-10-29 17:27:52.356",
"2020-10-29", "17:26:15.123", "17:26:15.123", "2020-10-29 17:27:52.356000Z", "2020-10-29 17:27:52.356956Z",
"-123456.78", "0.0456", "1234567890123456.78", "-0.0001",
}
row := reader.lastRow.Row
Expand Down

0 comments on commit 3bef1a6

Please sign in to comment.