From dd50bf614cc9282ff1c3a2fd408e902912c00ab6 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 17 Jul 2015 02:50:47 +0200 Subject: week calculation works now... diff --git a/src/helsinki.at/rhrdtime/rhrdtime.go b/src/helsinki.at/rhrdtime/rhrdtime.go index 5a9f0d0..a5b47f5 100644 --- a/src/helsinki.at/rhrdtime/rhrdtime.go +++ b/src/helsinki.at/rhrdtime/rhrdtime.go @@ -27,6 +27,7 @@ package main import ( "fmt" "time" + "math" "flag" "net/http" "encoding/json" @@ -38,13 +39,41 @@ import ( type timeUpdate struct { Timestamp int64 `json:"timestamp"` + TZOffset int `json:"tz-offset"` Week uint8 `json:"week"` } func getTimeUpdate() []byte { - now := time.Now() + // + // This computes the current Rivendell Week based on the number + // of weeks since epoch. + // + // Explanation: + // epoch was at 01.01.1970 UTC which was a Thursday. + // Monday in that week is (s-from-epoch + 3*24*60*60) seconds ago. + // This needs to be adjusted by the timezone offset for Europe/Vienna + // which is of course not constant (damn you daylight savings time) + // Divide this by (7*24*60*60) and you get the number of + // weeks since the Monday in the week of epoch adjusted for timezone offsets. + // This week had week number 3 so add an offset of 2 and + // get the modulo of 4. This rounded down gives you the current week + // with 0 meaning Week 1. So add 1 to that number and you will get + // the current RD week. + // + + loc, err := time.LoadLocation("Europe/Vienna"); + if err != nil { + fmt.Println(err) + return []byte{'{', '}'} + } + + now := time.Now().In(loc); + _, offset := now.Zone() + + sEpoch := now.Unix() + int64(offset) + week := uint8(math.Floor(math.Mod((((float64(sEpoch) + 259200)/604800) + 2), 4)) + 1) - update := timeUpdate{now.Unix(), 1} + update := timeUpdate{now.Unix(), offset, week} update_json, err := json.Marshal(update) if err != nil { fmt.Println(err) -- cgit v0.10.2