Skip to content
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.

refactor: Update TimeToFloat64/TimeFromFloat64 functions, cover case … #220

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion timeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,17 @@ func TimeMinMax(times ...time.Time) (min, max time.Time) {

// TimeToFloat64 returns a float64 representation of a time.
func TimeToFloat64(t time.Time) float64 {
if t.IsZero() {
return 0
}
return float64(t.UnixNano())
}

// TimeFromFloat64 returns a time from a float64.
// TimeFromFloat64 returns a time in nanosecond from a float64.
func TimeFromFloat64(tf float64) time.Time {
if tf == 0 {
return time.Time{}
}
return time.Unix(0, int64(tf))
}

Expand Down
60 changes: 60 additions & 0 deletions timeutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package chart

import (
"testing"
"time"
)

func TestTimeMinMax(t *testing.T) {
// empty input
min, max := TimeMinMax()
if !min.IsZero() || !max.IsZero() {
t.Errorf("Expected minimum and maximum to be zero time for empty input, but got min=%s, max=%s", min, max)
}

// non-empty input
times := []time.Time{
time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2022, 2, 1, 0, 0, 0, 0, time.UTC),
time.Date(2022, 3, 1, 0, 0, 0, 0, time.UTC),
}
expectedMin := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
expectedMax := time.Date(2022, 3, 1, 0, 0, 0, 0, time.UTC)
min, max = TimeMinMax(times...)
if min != expectedMin || max != expectedMax {
t.Errorf("Expected minimum=%s, maximum=%s for non-empty input, but got min=%s, max=%s", expectedMin, expectedMax, min, max)
}
}

func TestTimeToFloat64(t *testing.T) {
// zero time
tf := TimeToFloat64(time.Time{})
if tf != 0 {
t.Errorf("Expected float64 representation of zero time to be 0, but got %f", tf)
}

// non-zero time
tm := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
expectedTF := float64(tm.UnixNano())
tf = TimeToFloat64(tm)
if tf != expectedTF {
t.Errorf("Expected float64 representation of time %s to be %f, but got %f", tm, expectedTF, tf)
}
}

func TestTimeFromFloat64(t *testing.T) {
// zero float64
expectedT := time.Time{}
actualT := TimeFromFloat64(0)
if actualT != expectedT {
t.Errorf("Expected time from float64 representation of 0 to be zero time, but got %s", actualT)
}

// non-zero float64 represent nanoseconds
expectedT = time.Date(2022, 1, 1, 0, 0, 0, 123456789, time.Local)
nanosecondsFloat := float64(expectedT.UnixNano())
actualT = TimeFromFloat64(nanosecondsFloat)
if actualT.Equal(expectedT) {
t.Errorf("Expected time from float64 representation %f to be %s, but got %s", nanosecondsFloat, expectedT, actualT)
}
}