From 392f71c9f0f3c1c54157fdbd3e31bc391653ddda Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 14 Feb 2023 13:36:28 +0100 Subject: [PATCH] Add current time to broadcast matching data --- broadcasts/data.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/broadcasts/data.go b/broadcasts/data.go index 73da1bb9..c9cbcfd5 100644 --- a/broadcasts/data.go +++ b/broadcasts/data.go @@ -1,6 +1,7 @@ package broadcasts import ( + "strconv" "time" "github.com/safing/portbase/config" @@ -69,13 +70,21 @@ func collectData() interface{} { data["Account"] = &Account{ UserRecord: userRecord, UpToDate: userRecord.Meta().Modified > time.Now().Add(-7*24*time.Hour).Unix(), - MayUseUSP: userRecord.MayUseSPN(), + MayUseSPN: userRecord.MayUseSPN(), } } // Time running. data["UptimeHours"] = int(time.Since(portmasterStarted).Hours()) + // Get current time and date. + now := time.Now() + data["Current"] = &Current{ + UnixTime: now.Unix(), + UTC: makeDateTimeInfo(now.UTC()), + Local: makeDateTimeInfo(now), + } + return data } @@ -93,10 +102,33 @@ type Location struct { type Account struct { *access.UserRecord UpToDate bool - MayUseUSP bool + MayUseSPN bool } // DataError represents an error getting some matching data. type DataError struct { Error error } + +// Current holds current date and time data. +type Current struct { + UnixTime int64 + UTC *DateTime + Local *DateTime +} + +// DateTime holds date and time data in different formats. +type DateTime struct { + NumericDateTime int64 + NumericDate int64 + NumericTime int64 +} + +func makeDateTimeInfo(t time.Time) *DateTime { + info := &DateTime{} + info.NumericDateTime, _ = strconv.ParseInt(t.Format("20060102150405"), 10, 64) + info.NumericDate, _ = strconv.ParseInt(t.Format("20060102"), 10, 64) + info.NumericTime, _ = strconv.ParseInt(t.Format("150405"), 10, 64) + + return info +}