Properly set timezone
continuous-integration/drone/pr Build is failing Details

This commit is contained in:
kolaente 2020-06-27 00:47:19 +02:00
parent 58e044bb36
commit 85babcbbb3
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 42 additions and 24 deletions

View File

@ -152,6 +152,23 @@ func (k Key) GetStringSlice() []string {
return viper.GetStringSlice(string(k))
}
var timezone *time.Location
// GetTimeZone returns the time zone configured for vikunja
// It is a seperate function and not done through viper because that makes handling
// it way easier, especially when testing.
func GetTimeZone() *time.Location {
if timezone == nil {
loc, err := time.LoadLocation(ServiceTimeZone.GetString())
if err != nil {
fmt.Printf("Error parsing time zone: %s", err)
os.Exit(1)
}
timezone = loc
}
return timezone
}
// Set sets a value
func (k Key) Set(i interface{}) {
viper.Set(string(k), i)

View File

@ -72,11 +72,7 @@ func CreateDBEngine() (engine *xorm.Engine, err error) {
log.Fatalf("Unknown database type %s", config.DatabaseType.GetString())
}
loc, err := time.LoadLocation(config.ServiceTimeZone.GetString())
if err != nil {
log.Fatalf("Could not parse timezone '%s': %s", config.ServiceTimeZone.GetString(), err)
}
engine.SetTZLocation(loc)
engine.SetTZLocation(config.GetTimeZone())
engine.SetMapper(core.GonicMapper{})
logger := log.NewXormLogger("")
engine.SetLogger(logger)

View File

@ -21,7 +21,6 @@ import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log"
"os"
"time"
"xorm.io/core"
"xorm.io/xorm"
)
@ -50,11 +49,7 @@ func CreateTestEngine() (engine *xorm.Engine, err error) {
logger := log.NewXormLogger("DEBUG")
logger.ShowSQL(os.Getenv("UNIT_TESTS_VERBOSE") == "1")
engine.SetLogger(logger)
loc, err := time.LoadLocation(config.ServiceTimeZone.GetString())
if err != nil {
log.Fatalf("Could not parse timezone '%s': %s", config.ServiceTimeZone.GetString(), err)
}
engine.SetTZLocation(loc)
engine.SetTZLocation(config.GetTimeZone())
x = engine
return
}

View File

@ -50,6 +50,7 @@ func InitFixtures(tablenames ...string) (err error) {
testfixtures.Database(x.DB().DB),
testfixtures.Dialect(config.DatabaseType.GetString()),
testfixtures.DangerousSkipTestDatabaseCheck(),
testfixtures.Location(config.GetTimeZone()),
testfiles,
}

View File

@ -26,21 +26,30 @@ import (
"time"
)
func setupTime() {
var err error
loc, err := time.LoadLocation("GMT")
if err != nil {
fmt.Printf("Error setting up time: %s", err)
os.Exit(1)
}
testCreatedTime, err = time.ParseInLocation(time.RFC3339Nano, "2018-12-01T15:13:12.0+00:00", loc)
if err != nil {
fmt.Printf("Error setting up time: %s", err)
os.Exit(1)
}
testCreatedTime = testCreatedTime.In(loc)
testUpdatedTime, err = time.ParseInLocation(time.RFC3339Nano, "2018-12-02T15:13:12.0+00:00", loc)
if err != nil {
fmt.Printf("Error setting up time: %s", err)
os.Exit(1)
}
testUpdatedTime = testUpdatedTime.In(loc)
}
func TestMain(m *testing.M) {
var err error
testCreatedTime, err = time.ParseInLocation(time.RFC3339Nano, "2018-12-01T15:13:12.0+00:00", time.Local)
if err != nil {
fmt.Printf("Error setting up time: %s", err)
os.Exit(1)
}
testCreatedTime = testCreatedTime.Local()
testUpdatedTime, err = time.ParseInLocation(time.RFC3339Nano, "2018-12-02T15:13:12.0+00:00", time.Local)
if err != nil {
fmt.Printf("Error setting up time: %s", err)
os.Exit(1)
}
testUpdatedTime = testUpdatedTime.Local()
setupTime()
// Set default config
config.InitDefaultConfig()