Skip to content

Commit

Permalink
Merge pull request #73 from puerco/unifytz
Browse files Browse the repository at this point in the history
Normalize timezones in JSON output
  • Loading branch information
puerco authored Dec 5, 2023
2 parents 5bf7fa5 + 8c5f7ca commit cc8ea23
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/vex/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SPDX-License-Identifier: Apache-2.0
package vex

import (
"encoding/json"
"fmt"
"sort"
"strings"
Expand Down Expand Up @@ -206,3 +207,27 @@ func (stmt *Statement) MatchesProduct(identifier, subidentifier string) bool {
}
return false
}

// MarshalJSON the document object overrides its marshaling function to normalize
// the timezones in all dates to Zulu.
func (stmt *Statement) MarshalJSON() ([]byte, error) {
type alias Statement
var ts, lu string

if stmt.Timestamp != nil {
ts = stmt.Timestamp.UTC().Format(time.RFC3339Nano)
}
if stmt.LastUpdated != nil {
lu = stmt.LastUpdated.UTC().Format(time.RFC3339Nano)
}

return json.Marshal(&struct {
*alias
TimeZonedTimestamp string `json:"timestamp"`
TimeZonedLastUpdated string `json:"last_updated,omitempty"`
}{
alias: (*alias)(stmt),
TimeZonedTimestamp: ts,
TimeZonedLastUpdated: lu,
})
}
24 changes: 24 additions & 0 deletions pkg/vex/vex.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,30 @@ func (vexDoc *VEX) ToJSON(w io.Writer) error {
return nil
}

// MarshalJSON the document object overrides its marshaling function to normalize
// the timezones in all dates to Zulu.
func (vexDoc *VEX) MarshalJSON() ([]byte, error) {
type alias VEX
var ts, lu string

if vexDoc.Timestamp != nil {
ts = vexDoc.Timestamp.UTC().Format(time.RFC3339)
}
if vexDoc.LastUpdated != nil {
lu = vexDoc.LastUpdated.UTC().Format(time.RFC3339)
}

return json.Marshal(&struct {
*alias
TimeZonedTimestamp string `json:"timestamp"`
TimeZonedLastUpdated string `json:"last_updated,omitempty"`
}{
TimeZonedTimestamp: ts,
TimeZonedLastUpdated: lu,
alias: (*alias)(vexDoc),
})
}

// EffectiveStatement returns the latest VEX statement for a given product and
// vulnerability, that is the statement that contains the latest data about
// impact to a given product.
Expand Down

0 comments on commit cc8ea23

Please sign in to comment.