This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
Replies: 1 comment 27 replies
-
Hi @manson , package main
import (
"fmt"
"time"
"github.com/kamalshkeir/klog"
"github.com/kamalshkeir/korm"
"github.com/kamalshkeir/sqlitedriver"
)
type TestModel struct {
Id uint `korm:"pk"`
Content string
TimeField time.Time
}
func main() {
sqlitedriver.Use()
err := korm.New(korm.SQLITE, "db")
if klog.CheckError(err) {
return
}
defer korm.Shutdown()
err = korm.AutoMigrate[TestModel]("test_model")
if klog.CheckError(err) {
return
}
klog.Printfs("mgrunning on http://localhost:9313\n")
m := TestModel{
Content: "test3",
TimeField: time.Now().UTC(),
}
fmt.Println("before insert:", m.TimeField)
_, err = korm.Model[TestModel]().Insert(&m)
klog.CheckError(err)
time.Sleep(5 * time.Second)
//update
_, err = korm.Model[TestModel]().Where("content = ?", m.Content).Set("time_field = ?", time.Now().UTC())
klog.CheckError(err)
// after update
data, err := korm.Model[TestModel]().Where("content = ?", m.Content).One()
if klog.CheckError(err) {
return
}
fmt.Println("after update:", data.TimeField)
} OUTPUT: before insert: 2023-02-05 15:55:49.8155274 +0000 UTC
after update: 2023-02-05 15:55:54 +0000 UTC |
Beta Was this translation helpful? Give feedback.
27 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi!
New small issue in ORM
I write this note as I remember I faced, because when struggling with it, I have rewritten code many times and may be wrong in some nuances, but all in all something like that exists:
I have a time field, say, LockStart. My local time is not a UTC time. When I save this field in model like time.Now() it is converted to UTC and saves in database as UTC. It's ok for me, although someone would argue that he sees the time in the table, not he wrote. But to be clear how it works and stores, I insert model with this field like
LockStart: time.Now().UTC(),
. It's ok. it shifts time to a UTC time and is ok. But, when I update record and set this file like.Set("lock_start", time.Now().UTC())
it behaves weird. The time is intact (not shifted to a valid UTC) but extra info, when printing this field to console, says that this is UTC time, although it is already local.In other words (I skip here dates to accent the problem part) when I insert record as time.Now().UTC() (my local time 17:00 is being converted to 14:00 UTC) it shows 14:00 UTC as expected. But when I updated it the same way (Set field with time.Now().UTC()), when I read this field back I gets 17:00 UTC. But I expected 14:00 UTC.
I hope I didn't mess up anything here. Currently I have worked around this like having this field as int64 and saving Time as Unix value. It is even better for me like that in this situation, but anyway, something is wrong with this field type when saving, updating and reading. I know, time fields always was pain.
Beta Was this translation helpful? Give feedback.
All reactions