Skip to content

Commit

Permalink
Use standard UnixMilli to convert time to ms and ms to time
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlo-v-chernykh committed Jun 8, 2022
1 parent 6ce51e2 commit e49ca8f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 13 deletions.
9 changes: 0 additions & 9 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keystore

import (
"encoding/binary"
"time"
)

const (
Expand Down Expand Up @@ -33,11 +32,3 @@ func zeroing(buf []byte) {
buf[i] = 0
}
}

func millisecondsToTime(ms int64) time.Time {
return time.Unix(0, ms*int64(time.Millisecond))
}

func timeToMilliseconds(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}
5 changes: 3 additions & 2 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"hash"
"io"
"time"
)

const defaultCertificateType = "X509"
Expand Down Expand Up @@ -127,7 +128,7 @@ func (d decoder) readPrivateKeyEntry(version uint32) (PrivateKeyEntry, error) {
chain = append(chain, cert)
}

creationDateTime := millisecondsToTime(int64(creationTimeStamp))
creationDateTime := time.UnixMilli(int64(creationTimeStamp))
privateKeyEntry := PrivateKeyEntry{
encryptedPrivateKey: encryptedPrivateKey,
CreationTime: creationDateTime,
Expand All @@ -148,7 +149,7 @@ func (d decoder) readTrustedCertificateEntry(version uint32) (TrustedCertificate
return TrustedCertificateEntry{}, fmt.Errorf("read certificate: %w", err)
}

creationDateTime := millisecondsToTime(int64(creationTimeStamp))
creationDateTime := time.UnixMilli(int64(creationTimeStamp))
trustedCertificateEntry := TrustedCertificateEntry{
CreationTime: creationDateTime,
Certificate: certificate,
Expand Down
4 changes: 2 additions & 2 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (e encoder) writePrivateKeyEntry(alias string, pke PrivateKeyEntry) error {
return fmt.Errorf("write alias: %w", err)
}

if err := e.writeUint64(uint64(timeToMilliseconds(pke.CreationTime))); err != nil {
if err := e.writeUint64(uint64(pke.CreationTime.UnixMilli())); err != nil {
return fmt.Errorf("write creation timestamp: %w", err)
}

Expand Down Expand Up @@ -140,7 +140,7 @@ func (e encoder) writeTrustedCertificateEntry(alias string, tce TrustedCertifica
return fmt.Errorf("write alias: %w", err)
}

if err := e.writeUint64(uint64(timeToMilliseconds(tce.CreationTime))); err != nil {
if err := e.writeUint64(uint64(tce.CreationTime.UnixMilli())); err != nil {
return fmt.Errorf("write creation timestamp: %w", err)
}

Expand Down

0 comments on commit e49ca8f

Please sign in to comment.