From e2b6ec92921a2d43f77015f0e75c362a03430e76 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 6 Feb 2017 19:24:36 -0500 Subject: Move some go stuff into a shared 'util'. --- util/date.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 util/date.go (limited to 'util') diff --git a/util/date.go b/util/date.go new file mode 100644 index 0000000..3b5c457 --- /dev/null +++ b/util/date.go @@ -0,0 +1,51 @@ +package util + +import ( + "html/template" + "time" +) + +func Date2HTML(t time.Time) template.HTML { + return template.HTML(t.Local().Format("")) +} + +func DateTime2HTML(t time.Time) template.HTML { + return template.HTML(t.Local().Format("")) +} + +func DateTime2ColorHTML(t time.Time) template.HTML { + return template.HTML(t.Local().Format("")) +} + +type TimeRange struct { + A, B time.Time +} + +func (tr TimeRange) ToPct(point time.Time) float64 { + dur_ab := tr.B.Sub(tr.A) + dur_ap := point.Sub(tr.A) + return float64(dur_ap) / float64(dur_ab) +} + +type ByteRange struct { + A, B byte +} + +func (br ByteRange) FromPct(pct float64) byte { + ab := int16(br.B) - int16(br.A) + ap := int16(pct * float64(ab)) + return byte(int16(br.A) + ap) +} + +func PctCap(pct float64) float64 { + if pct < 0 { + pct = 0 + } else if pct > 1 { + pct = 1 + } + return pct +} + +func MapRange(tr TimeRange, br ByteRange, t time.Time) byte { + return br.FromPct(PctCap(tr.ToPct(t))) +} -- cgit v1.2.3-2-g168b