mirror of
https://github.com/tobiasehlert/teslamateapi.git
synced 2026-02-27 09:54:18 +08:00
formatting time of database in UTC to user-set timezone
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user