Skip to content

Commit

Permalink
feat: add Format() to timestamp (#30)
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zheng <patrickzheng@microsoft.com>
  • Loading branch information
Two-Hearts authored Jul 15, 2024
1 parent 80d3869 commit df25ef8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
13 changes: 12 additions & 1 deletion timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

package tspclient

import "time"
import (
"fmt"
"time"
)

// Timestamp denotes the time at which the timestamp token was created by the TSA
//
Expand Down Expand Up @@ -43,3 +46,11 @@ func (t *Timestamp) BoundedAfter(u time.Time) bool {
timestampLowerLimit := t.Value.Add(-t.Accuracy)
return timestampLowerLimit.After(u) || timestampLowerLimit.Equal(u)
}

// Format returns a string of t in format layout.
// The output is a timestamp range calculated with its accuracy.
func (t *Timestamp) Format(layout string) string {
lowerBound := t.Value.Add(-t.Accuracy)
upperBound := t.Value.Add(t.Accuracy)
return fmt.Sprintf("[%s, %s]", lowerBound.Format(layout), upperBound.Format(layout))
}
15 changes: 15 additions & 0 deletions timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,18 @@ func TestTimestamp(t *testing.T) {
t.Fatal("timestamp.BoundedAfter expected false, but got true")
}
}

func TestString(t *testing.T) {
// timestamp range:
// [time.Date(2021, time.September, 17, 14, 9, 8, 0, time.UTC),
// time.Date(2021, time.September, 17, 14, 9, 12, 0, time.UTC)]
timestamp := Timestamp{
Value: time.Date(2021, time.September, 17, 14, 9, 10, 0, time.UTC),
Accuracy: 2 * time.Second,
}

expectedStr := "[2021-09-17T14:09:08Z, 2021-09-17T14:09:12Z]"
if timestamp.Format(time.RFC3339) != expectedStr {
t.Fatalf("expected %s, but got %s", expectedStr, timestamp.Format(time.RFC3339))
}
}

0 comments on commit df25ef8

Please sign in to comment.