diff --git a/src/TeslaMateAPICars.go b/src/TeslaMateAPICars.go index 52d9efd..d2527bb 100644 --- a/src/TeslaMateAPICars.go +++ b/src/TeslaMateAPICars.go @@ -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) diff --git a/src/TeslaMateAPICarsStatus.go b/src/TeslaMateAPICarsStatus.go index eb460b8..ccfb364 100644 --- a/src/TeslaMateAPICarsStatus.go +++ b/src/TeslaMateAPICarsStatus.go @@ -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 } diff --git a/src/TeslaMateAPICarsUpdates.go b/src/TeslaMateAPICarsUpdates.go index 1260606..4df2c1c 100644 --- a/src/TeslaMateAPICarsUpdates.go +++ b/src/TeslaMateAPICarsUpdates.go @@ -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 diff --git a/src/TeslaMateAPIGlobalSettings.go b/src/TeslaMateAPIGlobalSettings.go index 74d1a3b..144cfc5 100644 --- a/src/TeslaMateAPIGlobalSettings.go +++ b/src/TeslaMateAPIGlobalSettings.go @@ -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 diff --git a/src/webserver.go b/src/webserver.go index 9ca35b2..2948a7e 100644 --- a/src/webserver.go +++ b/src/webserver.go @@ -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 {