Add current time to broadcast matching data

This commit is contained in:
Daniel
2023-02-14 13:36:28 +01:00
parent 0ee078d911
commit 392f71c9f0

View File

@@ -1,6 +1,7 @@
package broadcasts package broadcasts
import ( import (
"strconv"
"time" "time"
"github.com/safing/portbase/config" "github.com/safing/portbase/config"
@@ -69,13 +70,21 @@ func collectData() interface{} {
data["Account"] = &Account{ data["Account"] = &Account{
UserRecord: userRecord, UserRecord: userRecord,
UpToDate: userRecord.Meta().Modified > time.Now().Add(-7*24*time.Hour).Unix(), UpToDate: userRecord.Meta().Modified > time.Now().Add(-7*24*time.Hour).Unix(),
MayUseUSP: userRecord.MayUseSPN(), MayUseSPN: userRecord.MayUseSPN(),
} }
} }
// Time running. // Time running.
data["UptimeHours"] = int(time.Since(portmasterStarted).Hours()) 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 return data
} }
@@ -93,10 +102,33 @@ type Location struct {
type Account struct { type Account struct {
*access.UserRecord *access.UserRecord
UpToDate bool UpToDate bool
MayUseUSP bool MayUseSPN bool
} }
// DataError represents an error getting some matching data. // DataError represents an error getting some matching data.
type DataError struct { type DataError struct {
Error error 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
}