summaryrefslogtreecommitdiff
path: root/status/04-date.go
blob: fba68d6dc91ea26b2431406448879407ba1a6d23 (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
package status // import "hhvn.uk/hbspbar/status"

import (
	"time"

	"hhvn.uk/hbspbar/config"
)

var datewait = false

// TODO: seasons
func date(b *Block) error {
	// opposite of sync.Once
	if !datewait {
		datewait = true
	} else {
		// Sleep til the next day comes around
		tmrw := time.Now().AddDate(0, 0, 1)
		tmrw =  time.Date(tmrw.Year(), tmrw.Month(), tmrw.Day(), 0, 0, 0, 0, time.Local)

		var until time.Duration
		until = time.Until(tmrw)

		// Previously this status would sleep all the way until the next day.
		// This worked fine until the device entered sleep/hibernation
		//
		// Therefore a limit was added. Not the ideal solution,
		// as if the device is woken within config.MaxSleep,
		// then the date will not update until some extra time has elapsed.
		//
		// TODO: a better solution
		if (until > config.MaxSleep) {
			until = config.MaxSleep
		}

		time.Sleep(until)
	}

	_, err := b.drawText(0, config.Fg, time.Now().Local().Format("Mon 02/01"))
	if err != nil { return err }

	return nil
}

func init() {
	register("date", date, 0)
}