diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ae3f4cb --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Launch", + "type": "go", + "request": "launch", + "mode": "test", + "remotePath": "", + "port": 2345, + "host": "127.0.0.1", + "program": "${workspaceRoot}", + "env": {}, + "args": [], + "showLog": true + } + ] +} \ No newline at end of file diff --git a/_examples/request_timings/main.go b/_examples/request_timings/main.go index 47c29f9..1542e68 100644 --- a/_examples/request_timings/main.go +++ b/_examples/request_timings/main.go @@ -14,11 +14,11 @@ func readData() ([]time.Time, []float64) { var yvalues []float64 util.ReadFileByLines("requests.csv", func(line string) { parts := strings.Split(line, ",") - year := util.ParseInt(parts[0]) - month := util.ParseInt(parts[1]) - day := util.ParseInt(parts[2]) - hour := util.ParseInt(parts[3]) - elapsedMillis := util.ParseFloat64(parts[4]) + year := util.String.ParseInt(parts[0]) + month := util.String.ParseInt(parts[1]) + day := util.String.ParseInt(parts[2]) + hour := util.String.ParseInt(parts[3]) + elapsedMillis := util.String.ParseFloat64(parts[4]) xvalues = append(xvalues, time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.UTC)) yvalues = append(yvalues, elapsedMillis) }) diff --git a/go-chart.test b/go-chart.test new file mode 100755 index 0000000..20cf1fd Binary files /dev/null and b/go-chart.test differ diff --git a/market_hours_range.go b/market_hours_range.go index 3b7c570..265cffa 100644 --- a/market_hours_range.go +++ b/market_hours_range.go @@ -33,12 +33,12 @@ func (mhr MarketHoursRange) IsZero() bool { // GetMin returns the min value. func (mhr MarketHoursRange) GetMin() float64 { - return TimeToFloat64(mhr.Min) + return Time.ToFloat64(mhr.Min) } // GetMax returns the max value. func (mhr MarketHoursRange) GetMax() float64 { - return TimeToFloat64(mhr.GetEffectiveMax()) + return Time.ToFloat64(mhr.GetEffectiveMax()) } // GetEffectiveMax gets either the close on the max, or the max itself. @@ -52,13 +52,13 @@ func (mhr MarketHoursRange) GetEffectiveMax() time.Time { // SetMin sets the min value. func (mhr *MarketHoursRange) SetMin(min float64) { - mhr.Min = Float64ToTime(min) + mhr.Min = Time.FromFloat64(min) mhr.Min = mhr.Min.In(mhr.GetTimezone()) } // SetMax sets the max value. func (mhr *MarketHoursRange) SetMax(max float64) { - mhr.Max = Float64ToTime(max) + mhr.Max = Time.FromFloat64(max) mhr.Max = mhr.Max.In(mhr.GetTimezone()) } @@ -159,7 +159,7 @@ func (mhr *MarketHoursRange) makeTicks(vf ValueFormatter, times []time.Time) []T ticks := make([]Tick, len(times)) for index, t := range times { ticks[index] = Tick{ - Value: TimeToFloat64(t), + Value: Time.ToFloat64(t), Label: vf(t), } } @@ -172,7 +172,7 @@ func (mhr MarketHoursRange) String() string { // Translate maps a given value into the ContinuousRange space. func (mhr MarketHoursRange) Translate(value float64) int { - valueTime := Float64ToTime(value) + valueTime := Time.FromFloat64(value) valueTimeEastern := valueTime.In(Date.Eastern()) totalSeconds := Date.CalculateMarketSecondsBetween(mhr.Min, mhr.GetEffectiveMax(), mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.HolidayProvider) valueDelta := Date.CalculateMarketSecondsBetween(mhr.Min, valueTimeEastern, mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.HolidayProvider) diff --git a/market_hours_range_test.go b/market_hours_range_test.go index 14b20fb..45458c4 100644 --- a/market_hours_range_test.go +++ b/market_hours_range_test.go @@ -35,9 +35,9 @@ func TestMarketHoursRangeTranslate(t *testing.T) { weds := time.Date(2016, 07, 20, 9, 30, 0, 0, Date.Eastern()) - assert.Equal(0, r.Translate(TimeToFloat64(r.Min))) - assert.Equal(400, r.Translate(TimeToFloat64(weds))) - assert.Equal(1000, r.Translate(TimeToFloat64(r.Max))) + assert.Equal(0, r.Translate(Time.ToFloat64(r.Min))) + assert.Equal(400, r.Translate(Time.ToFloat64(weds))) + assert.Equal(1000, r.Translate(Time.ToFloat64(r.Max))) } func TestMarketHoursRangeGetTicks(t *testing.T) { @@ -67,6 +67,6 @@ func TestMarketHoursRangeGetTicks(t *testing.T) { ticks := ra.GetTicks(r, defaults, TimeValueFormatter) assert.NotEmpty(ticks) assert.Len(ticks, 5) - assert.NotEqual(TimeToFloat64(ra.Min), ticks[0].Value) + assert.NotEqual(Time.ToFloat64(ra.Min), ticks[0].Value) assert.NotEmpty(ticks[0].Label) } diff --git a/math.go b/math_util.go similarity index 95% rename from math.go rename to math_util.go index f280c17..4121647 100644 --- a/math.go +++ b/math_util.go @@ -19,16 +19,6 @@ const ( _r2d = (180.0 / math.Pi) ) -// TimeToFloat64 returns a float64 representation of a time. -func TimeToFloat64(t time.Time) float64 { - return float64(t.UnixNano()) -} - -// Float64ToTime returns a time from a float64. -func Float64ToTime(tf float64) time.Time { - return time.Unix(0, int64(tf)) -} - var ( // Math contains helper methods for common math operations. Math = &mathUtil{} diff --git a/math_test.go b/math_util_test.go similarity index 100% rename from math_test.go rename to math_util_test.go diff --git a/time_series.go b/time_series.go index df779eb..025e86a 100644 --- a/time_series.go +++ b/time_series.go @@ -30,14 +30,14 @@ func (ts TimeSeries) Len() int { // GetValue gets a value at a given index. func (ts TimeSeries) GetValue(index int) (x, y float64) { - x = TimeToFloat64(ts.XValues[index]) + x = Time.ToFloat64(ts.XValues[index]) y = ts.YValues[index] return } // GetLastValue gets the last value. func (ts TimeSeries) GetLastValue() (x, y float64) { - x = TimeToFloat64(ts.XValues[len(ts.XValues)-1]) + x = Time.ToFloat64(ts.XValues[len(ts.XValues)-1]) y = ts.YValues[len(ts.YValues)-1] return } diff --git a/time_util.go b/time_util.go new file mode 100644 index 0000000..8937546 --- /dev/null +++ b/time_util.go @@ -0,0 +1,20 @@ +package chart + +import "time" + +var ( + // Time contains time utility functions. + Time = timeUtil{} +) + +type timeUtil struct{} + +// TimeToFloat64 returns a float64 representation of a time. +func (tu timeUtil) ToFloat64(t time.Time) float64 { + return float64(t.UnixNano()) +} + +// Float64ToTime returns a time from a float64. +func (tu timeUtil) FromFloat64(tf float64) time.Time { + return time.Unix(0, int64(tf)) +} diff --git a/value_formatter_test.go b/value_formatter_test.go index 0675914..3bd8409 100644 --- a/value_formatter_test.go +++ b/value_formatter_test.go @@ -11,7 +11,7 @@ func TestTimeValueFormatterWithFormat(t *testing.T) { assert := assert.New(t) d := time.Now() - di := TimeToFloat64(d) + di := Time.ToFloat64(d) df := float64(di) s := TimeValueFormatterWithFormat(d, DefaultDateFormat)