CustomDateTime wraps Go's time.Time for the SurrealDB datetime kind, an instant in UTC with nanosecond precision. You can send a time.Time as it is, and the SDK sends it as a datetime in the same way. A datetime decodes into a time.Time field, or into a CustomDateTime when the target is any.
Package: github.com/surrealdb/surrealdb.go/v2/pkg/models
Source: pkg/models/datetime.go
Definition
type CustomDateTime struct {
time.Time
}CustomDateTime embeds time.Time, so every method of time.Time is available on it.
How datetimes convert
The SDK sends a time.Time, a *time.Time and a CustomDateTime as a CBOR datetime that holds seconds and nanoseconds. It sends a zero time as NONE, and a nil *time.Time as NULL.
type Event struct {
ID *models.RecordID `json:"id,omitempty"`
StartedAt time.Time `json:"started_at"`
EndedAt *time.Time `json:"ended_at"`
}With this struct, the record has no started_at field while StartedAt holds the zero time, and ended_at is NULL while EndedAt is nil.
A CustomDateTime also decodes a SurrealDB 4.0 date as its midnight UTC, and a timestamp as its wall clock read as UTC. This is how plain CBOR carries these two kinds. For those kinds, use PlainDate and Timestamp instead.
Methods
.String()
Returns the datetime in UTC, formatted as 2006-01-02T15:04:05Z.
s := dt.String()Returns: string
.SurrealString()
Returns the datetime as a SurrealQL value: <datetime> '2006-01-02T15:04:05Z'.
s := dt.SurrealString()Returns: string
.IsZero()
Reports whether the datetime is nil or the zero time.
zero := dt.IsZero()Returns: bool
Usage
import "github.com/surrealdb/surrealdb.go/v2/pkg/models"
now := models.CustomDateTime{Time: time.Now()}
results, err := surrealdb.Query[[]any](ctx, db, "RETURN [$now, type::of($now)]", map[string]any{
"now": now,
})See also
Duration for fixed lengths of time
Dates, times and intervals for the calendar kinds of SurrealDB 4.0
Value types for how values convert
SurrealQL datetime type for the underlying data model