flokati/modules/fuzzytime.go

82 lines
1.8 KiB
Go
Raw Normal View History

2016-02-15 23:17:42 +00:00
// vi:ts=4:sts=4:sw=4:noet:tw=72
package modules
import (
"fmt"
"log"
"strings"
"time"
"github.com/sorcix/irc"
)
func init() {
MsgHandlers["fuzzytime"] = fuzzytimeHandleMessage
2016-02-15 23:17:42 +00:00
log.Println("Initializing fuzzytime module")
}
func fuzzytimeHandleMessage(m *irc.Message) {
tok := strings.Split(m.Trailing, " ")
if len(tok) < 1 {
return
}
switch tok[0] {
case "!time":
fuzzytimeShow()
//case "!q":
// show()
default:
}
}
func fuzzytimeShow() {
t := time.Now()
h := t.Hour()
tzcorrect := 1
h = h + tzcorrect // XXX: This should not be hardcoded
m := t.Minute()
s := "Es ist "
switch {
case m < 3:
s += fmt.Sprintf("%s Uhr\n", fuzzytimeSayHour(h))
case m < 8:
s += fmt.Sprintf("fünf nach %s\n", fuzzytimeSayHour(h))
case m < 13:
s += fmt.Sprintf("zehn nach %s\n", fuzzytimeSayHour(h))
case m < 18:
s += fmt.Sprintf("viertel nach %s\n", fuzzytimeSayHour(h))
case m < 23:
s += fmt.Sprintf("zwanzig nach %s\n", fuzzytimeSayHour(h))
case m < 28:
s += fmt.Sprintf("fünf vor halb %s\n", fuzzytimeSayHour(h+1))
case m < 33:
s += fmt.Sprintf("halb %s\n", fuzzytimeSayHour(h+1))
case m < 38:
s += fmt.Sprintf("fünf nach halb %s\n", fuzzytimeSayHour(h+1))
case m < 43:
s += fmt.Sprintf("zehn nach halb %s\n", fuzzytimeSayHour(h+1))
case m < 48:
s += fmt.Sprintf("viertel vor %s\n", fuzzytimeSayHour(h+1))
case m < 53:
s += fmt.Sprintf("zehn vor %s\n", fuzzytimeSayHour(h+1))
case m < 58:
s += fmt.Sprintf("fünf vor %s\n", fuzzytimeSayHour(h+1))
default:
s += fmt.Sprintf("%s Uhr\n", fuzzytimeSayHour(h+1))
}
SayCh <- fmt.Sprintf("*\n%s", s)
2016-02-15 23:17:42 +00:00
}
func fuzzytimeSayHour(h int) string {
words := [...]string{"zwölf", "eins", "zwei", "drei", "vier", "fünf", "sechs", "sieben", "acht", "neun", "zehn", "elf"}
for {
if h > 11 {
h = h - 12
} else {
break
}
}
return words[h]
}