formatting time of database in UTC to user-set timezone

This commit is contained in:
Tobias Lindberg
2021-02-09 20:30:10 +01:00
parent d209ada0b2
commit 28c9175dbe
5 changed files with 43 additions and 0 deletions
+4
View File
@@ -137,6 +137,10 @@ func TeslaMateAPICars() (string, bool) {
&car.TeslaMateStats.TotalUpdates,
)
// adjusting to timezone differences from UTC to be userspecific
car.TeslaMateDetails.InsertedAt = getTimeInTimeZone(car.TeslaMateDetails.InsertedAt)
car.TeslaMateDetails.UpdatedAt = getTimeInTimeZone(car.TeslaMateDetails.UpdatedAt)
// checking for errors after scanning
if err != nil {
log.Fatal(err)
+4
View File
@@ -543,6 +543,10 @@ func TeslaMateAPICarsStatus(CarID int) (string, bool) {
MQTTInformationData.ClimateDetails.OutsideTemp = celsiusToFahrenheit(MQTTInformationData.ClimateDetails.OutsideTemp)
}
// adjusting to timezone differences from UTC to be userspecific
MQTTInformationData.StateSince = getTimeInTimeZone(MQTTInformationData.StateSince)
MQTTInformationData.ChargingDetails.ScheduledChargingStartTime = getTimeInTimeZone(MQTTInformationData.ChargingDetails.ScheduledChargingStartTime)
// setting response as valid
ValidResponse = true
}
+4
View File
@@ -90,6 +90,10 @@ func TeslaMateAPICarsUpdates(CarID int, ResultPage int, ResultShow int) (string,
log.Fatal(err)
}
// adjusting to timezone differences from UTC to be userspecific
update.StartDate = getTimeInTimeZone(update.StartDate)
update.EndDate = getTimeInTimeZone(update.EndDate)
// appending update to UpdatesData
UpdatesData = append(UpdatesData, update)
CarData.CarID = CarID
+4
View File
@@ -101,6 +101,10 @@ func TeslaMateAPIGlobalSettings() (string, bool) {
log.Fatal(err)
}
// adjusting to timezone differences from UTC to be userspecific
GlobalSetting.AccountInfo.InsertedAt = getTimeInTimeZone(GlobalSetting.AccountInfo.InsertedAt)
GlobalSetting.AccountInfo.UpdatedAt = getTimeInTimeZone(GlobalSetting.AccountInfo.UpdatedAt)
// setting response to valid
GlobalSettingData = GlobalSetting
ValidResponse = true
+27
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"os"
"strconv"
"time"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
@@ -149,6 +150,32 @@ func initAPI() {
log.Println("successfully connected to postgres.")
}
func getTimeInTimeZone(datestring string) string {
// getting timezone from environment
UsersTimezone := getEnv("TM_TZ", "Europe/Berlin")
// format the dates are stored in postgres
TeslaMateDateFormat := "2006-01-02T15:04:05Z"
// parsing datestring into TeslaMateDateFormat
t, _ := time.Parse(TeslaMateDateFormat, datestring)
// logging UTC time to log
// log.Println("UTC: ", t.Format(time.RFC3339))
// loading timezone location
UserLocation, _ := time.LoadLocation(UsersTimezone)
// formatting in users location in RFC3339 format
ReturnDate := t.In(UserLocation).Format(time.RFC3339)
// logging Users location-converted time to log
// log.Println("Location at User: " + ReturnDate)
return ReturnDate
}
// getEnv func - read an environment or return a default value
func getEnv(key string, defaultVal string) string {
if value, exists := os.LookupEnv(key); exists {