clock

package
v0.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package clock provides type-safe time abstractions for SRT.

SRT uses microsecond-precision timestamps. On the wire, timestamps are 32-bit unsigned integers that wrap every ~71.6 minutes (2^32 microseconds). This package provides a Clock interface to enable deterministic testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Now() Timestamp
}

Clock provides the current time. Use RealClock for production and MockClock for testing.

type Microseconds

type Microseconds int64

Microseconds represents a duration in microseconds.

const (
	Millisecond Microseconds = 1000
	Second      Microseconds = 1000 * Millisecond
	Minute      Microseconds = 60 * Second
)

Common duration constants.

func FromDuration

func FromDuration(d time.Duration) Microseconds

FromDuration converts a time.Duration to Microseconds.

func SRTTimestampWrapPeriod

func SRTTimestampWrapPeriod() Microseconds

SRTTimestampWrapPeriod returns the wrap period for 32-bit SRT timestamps.

func (Microseconds) Abs

func (d Microseconds) Abs() Microseconds

Abs returns the absolute value of d.

func (Microseconds) Duration

func (d Microseconds) Duration() time.Duration

Duration converts to a standard time.Duration.

type MockClock

type MockClock struct {
	// contains filtered or unexported fields
}

MockClock is a clock for testing that advances only when told to.

func NewMockClock

func NewMockClock() *MockClock

NewMockClock returns a mock clock starting at time 0.

func (*MockClock) Advance

func (c *MockClock) Advance(d Microseconds)

Advance moves the mock clock forward by d microseconds.

func (*MockClock) Now

func (c *MockClock) Now() Timestamp

func (*MockClock) Set

func (c *MockClock) Set(t Timestamp)

Set sets the mock clock to the given timestamp.

type RealClock

type RealClock struct {
	// contains filtered or unexported fields
}

RealClock uses the system clock.

func NewRealClock

func NewRealClock() *RealClock

NewRealClock returns a Clock based on the system clock. The epoch is set to the time of creation; all timestamps are relative to this epoch.

func (*RealClock) Now

func (c *RealClock) Now() Timestamp

type Timestamp

type Timestamp int64

Timestamp represents an absolute point in time in microseconds since an arbitrary epoch (typically connection start).

func FromSRTTimestamp

func FromSRTTimestamp(srtTS uint32, reference Timestamp) Timestamp

FromSRTTimestamp reconstructs a full Timestamp from a 32-bit SRT timestamp and a reference timestamp. It picks the interpretation closest to the reference (handling wrap-around).

func (Timestamp) Add

func (t Timestamp) Add(d Microseconds) Timestamp

Add returns t + d.

func (Timestamp) After

func (t Timestamp) After(other Timestamp) bool

After reports whether t is after other.

func (Timestamp) Before

func (t Timestamp) Before(other Timestamp) bool

Before reports whether t is before other.

func (Timestamp) IsZero

func (t Timestamp) IsZero() bool

IsZero reports whether t is the zero timestamp.

func (Timestamp) SRTTimestamp

func (t Timestamp) SRTTimestamp() uint32

SRTTimestamp returns the lower 32 bits of the timestamp for the SRT wire format. SRT timestamps are 32-bit unsigned microsecond values that wrap every ~71.6 minutes (2^32 microseconds ≈ 4295 seconds).

func (Timestamp) Sub

func (t Timestamp) Sub(other Timestamp) Microseconds

Sub returns the duration between t and other (t - other).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL