1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 05:40:11 +01:00

Add time range to checker report.

This commit is contained in:
Sascha L. Teichmann 2023-08-02 20:01:04 +02:00
parent b6e5af9b49
commit 873eb4879b
5 changed files with 54 additions and 24 deletions

View file

@ -10,6 +10,7 @@
package models
import (
"encoding/json"
"fmt"
"strings"
"time"
@ -27,10 +28,17 @@ func NewTimeInterval(a, b time.Time) TimeRange {
return TimeRange{a, b}
}
// NYears returns a time interval spanning the last years.
func NYears(years uint) TimeRange {
now := time.Now()
start := now.AddDate(-int(years), 0, 0)
return NewTimeInterval(start, now)
}
// guessDate tries to guess an RFC 3339 date time from a given string.
func guessDate(s string) (time.Time, bool) {
for _, layout := range []string{
"2006-01-02T15:04:05Z07:00",
time.RFC3339,
"2006-01-02T15:04:05",
"2006-01-02T15:04",
"2006-01-02T15",
@ -50,6 +58,15 @@ func (tr *TimeRange) UnmarshalText(text []byte) error {
return tr.UnmarshalFlag(string(text))
}
// MarshalJSON implements [encoding/json.Marshaler].
func (tr TimeRange) MarshalJSON() ([]byte, error) {
s := []string{
tr[0].Format(time.RFC3339),
tr[1].Format(time.RFC3339),
}
return json.Marshal(s)
}
// UnmarshalFlag implements [go-flags/Unmarshaler].
func (tr *TimeRange) UnmarshalFlag(s string) error {
s = strings.TrimSpace(s)