Skip to content

API Reference

/

Values

CustomDuration

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

type CustomDuration struct {
	time.Duration
}

CustomDuration embeds time.Duration, so every method of time.Duration is available on it.

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

Returns the duration in the syntax of SurrealDB, such as 2h30m, 1d12h or 500ms.

Syntax
s := dur.String()

Returns: string

Converts the duration to a CustomDurationString.

Syntax
ds := dur.ToCustomDurationString()

Returns: 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 string
  • .String() returns the text

  • .ToDuration() parses it into a time.Duration

  • .ToCustomDuration() converts it to a CustomDuration

Formats nanoseconds in the syntax of SurrealDB.

Syntax
s := models.FormatDuration(ns)

Returns: string

Parses a duration in the syntax of SurrealDB into nanoseconds.

Syntax
ns, err := models.ParseDuration("1d2h30m")

Returns: (int64, error)

The units are y, w, d, h, m, s, ms, us or µs, and ns.

import "github.com/surrealdb/surrealdb.go/v2/pkg/models"

dur := models.CustomDuration{Duration: 2*time.Hour + 30*time.Minute}
fmt.Println(dur.String())
Output
2h30m

Was this page helpful?

Last updated