Skip to content

Commit

Permalink
Store CreationTime from header when reading Palm database
Browse files Browse the repository at this point in the history
Arguably, this is a crutch because the current in-memory PalmDB
representation only stores one date and writes that to multiple
non-equivalent header fields and records.  Still, this should be
better than just ignoring the stored dates.
  • Loading branch information
leotaku committed Oct 31, 2023
1 parent aba409b commit a67b952
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pdb/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type PalmDBHeader struct {
func NewPalmDBHeader(name string, dateTime time.Time, numRecords uint16, lastRecordUID uint32) PalmDBHeader {
nameBytes := [32]byte{}
copy(nameBytes[:31], name)
palmTime := calculatePalmTime(dateTime)
palmTime := convertToPalmTime(dateTime)

return PalmDBHeader{
Name: nameBytes,
Expand Down
6 changes: 4 additions & 2 deletions pdb/pdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func ReadDatabase(r io.Reader) (*Database, error) {
return nil, err
}

name := trimZeroes(string(palmDBHeader.Name[:]))
date := convertFromPalmTime(palmDBHeader.CreationTime)

records := make([]Record, 0)
for i := 1; i < len(offsets); i++ {
curr := offsets[i].Offset
Expand All @@ -130,10 +133,9 @@ func ReadDatabase(r io.Reader) (*Database, error) {
last := offsets[len(offsets)-1].Offset
records = append(records, RawRecord(data[last:]))

name := trimZeroes(string(palmDBHeader.Name[:]))

return &Database{
Name: name,
Date: date,
Records: records,
}, nil
}
7 changes: 6 additions & 1 deletion pdb/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ func trimZeroes(s string) string {
return strings.TrimRight(s, "\x00")
}

func calculatePalmTime(t time.Time) uint32 {
func convertToPalmTime(t time.Time) uint32 {
delta := t.Sub(time.Date(1904, 1, 1, 0, 0, 0, 0, time.UTC))
return uint32(delta.Seconds())
}

func convertFromPalmTime(t uint32) time.Time {
start := time.Date(1904, 1, 1, 0, 0, 0, 0, time.UTC)
return start.Add(time.Duration(t) * time.Second)
}

0 comments on commit a67b952

Please sign in to comment.