Skip to content

API Reference

/

Values

CustomDateTime

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

type CustomDateTime struct {
	time.Time
}

CustomDateTime embeds time.Time, so every method of time.Time is available on it.

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.

Returns the datetime in UTC, formatted as 2006-01-02T15:04:05Z.

Syntax
s := dt.String()

Returns: string

Returns the datetime as a SurrealQL value: <datetime> '2006-01-02T15:04:05Z'.

Syntax
s := dt.SurrealString()

Returns: string

Reports whether the datetime is nil or the zero time.

Syntax
zero := dt.IsZero()

Returns: bool

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,
})

Was this page helpful?

Last updated