CustomDuration wraps Go's time.Duration for the SurrealDB duration kind, and formats it in the syntax of SurrealDB, such as 1d2h30m. You can send a time.Duration as it is, and the SDK sends it as a duration in the same way. A duration decodes into a time.Duration field, or into a CustomDuration when the target is any.
Package: github.com/surrealdb/surrealdb.go/v2/pkg/models
Source: pkg/models/duration.go
Definition
type CustomDuration struct {
time.Duration
}CustomDuration embeds time.Duration, so every method of time.Duration is available on it.
How durations convert
The SDK sends a time.Duration and a CustomDuration as a CBOR duration that holds seconds and nanoseconds. Version 1 of the SDK sent a time.Duration as an int of nanoseconds. Such integers still decode into a time.Duration field.
A CustomDuration also decodes a SurrealDB 4.0 time as the duration since midnight. This is how plain CBOR carries a time. For a time of day, use PlainTime instead.
type Job struct {
ID *models.RecordID `json:"id,omitempty"`
Timeout time.Duration `json:"timeout"`
}
_, err := surrealdb.Create[Job](ctx, db, "job", Job{Timeout: 90 * time.Second})Methods
.String()
Returns the duration in the syntax of SurrealDB, such as 2h30m, 1d12h or 500ms.
s := dur.String()Returns: string
.ToCustomDurationString()
Converts the duration to a CustomDurationString.
ds := dur.ToCustomDurationString()Returns: CustomDurationString
Related types
CustomDurationString
A duration as text in the syntax of SurrealDB, such as "1d2h30m". The SDK sends it as a duration. Encoding a CustomDurationString that is not a valid duration fails with an error, such as invalid duration "nonsense".
type CustomDurationString stringMethods
.String()returns the text.ToDuration()parses it into atime.Duration.ToCustomDuration()converts it to aCustomDuration
Helper functions
FormatDuration
Formats nanoseconds in the syntax of SurrealDB.
s := models.FormatDuration(ns)Returns: string
ParseDuration
Parses a duration in the syntax of SurrealDB into nanoseconds.
ns, err := models.ParseDuration("1d2h30m")Returns: (int64, error)
The units are y, w, d, h, m, s, ms, us or µs, and ns.
Usage
import "github.com/surrealdb/surrealdb.go/v2/pkg/models"
dur := models.CustomDuration{Duration: 2*time.Hour + 30*time.Minute}
fmt.Println(dur.String())2h30mSee also
CustomDateTime for instants
Dates, times and intervals for calendar periods, which a duration cannot hold
Value types for how values convert
SurrealQL duration type for the underlying data model