lorenzodonini / ocpp-go

Open Charge Point Protocol implementation in Go
MIT License
262 stars 125 forks source link

Problems saving DateTime type to database #166

Closed gioacchinopoletto closed 1 year ago

gioacchinopoletto commented 1 year ago

Hi, many thanks for this wonderful package. I'm using it for a test project and for learning Go.

I have some trouble to save DateTime formats to a PGSQL database, for example Timestamp on core.StatusNotificationRequest.

On my project I'm using GORM as ORM, and as described on some Google solutions, setting my Timestamp as time.Time don't work.
I don't know if setting database field as timestamptz is correct.

Many thanks in advance for your help, and excuse me if the question seems very simple.

lorenzodonini commented 1 year ago

Hey there,

the DateTime is just a wrapper around the time.Time type, to facilitate json un/marshaling and enforce specific formatting within the library. It shouldn't be used for DB operations, since most ORMs won't be able to handle it.

You should stick with time.Time on the Golang side for DB operations, discarding any wrappers. As for the DB, dpending on what you want to store, you can either use timestamp or timestamptz PostgrSQL types (or the equivalent for other DBs).

I hope this answers your question, otherwise please provide a snippet so I can understand what isn't working exactly.

gioacchinopoletto commented 1 year ago

Hi @lorenzodonini, many thanks for your quick reply.

I have try to use only Time.time but I receive this error:

cannot use *transaction.startTime (variable of type types.DateTime) as time.Time value in argument to dbpostgresql.CreateStartChargeSession

when I pass transaction data to my database package, and I'm unable to resolve this problem (excuse me, I'm newbie to Go)

lorenzodonini commented 1 year ago
// DateTime wraps a time.Time struct, allowing for improved dateTime JSON compatibility.
type DateTime struct {
    time.Time
}

The actual time struct is embedded inside the DateTime. To access it directly, you'll need to use transaction.startTime.Time and pass that to your DB.

gioacchinopoletto commented 1 year ago

You are right... is very simple. All goes well. Many thanks for your time!