summaryrefslogtreecommitdiff
path: root/bin-src/util/date.go
blob: 3b5c4570913b739f6bc46acecee4876f408603d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package util

import (
	"html/template"
	"time"
)

func Date2HTML(t time.Time) template.HTML {
	return template.HTML(t.Local().Format("<time datetime=\"2006-01-02 15:04:05\" title=\"2006-01-02 15:04:05\">2006-01-02 <span class=time>15:04:05</span></time>"))
}

func DateTime2HTML(t time.Time) template.HTML {
	return template.HTML(t.Local().Format("<time datetime=\"2006-01-02 15:04:05\">2006-01-02 15:04:05</time>"))
}

func DateTime2ColorHTML(t time.Time) template.HTML {
	return template.HTML(t.Local().Format("<time class=daily datetime=\"2006-01-02 15:04:05\">2006-01-02 15:04:05</time>"))
}

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)))
}