Skip to content

sntp

ts
import sntp from 'mikrojs/sntp'

Synchronize the system clock with an NTP server. The default export is an Sntp singleton.

Usage

ts
import sntp from 'mikrojs/sntp'

const result = await sntp.sync({
  timezone: 'CET-1CEST,M3.5.0,M10.5.0/3',
})

if (result.ok) {
  console.log('Time synced:', result.value.time.toISOString())
}

Methods

sntp.sync(options?)

ts
sync(options?: SntpOptions): Promise<Result<SntpSyncResult, SntpError>>
sync(options: SntpOptions & {background: true}): Promise<Result<SntpSyncResultWithStop, SntpError>>

Synchronize the system clock. By default, syncs once and returns. With background: true, keeps syncing periodically and returns a stop() function.

ts
// One-shot sync
const result = await sntp.sync()

// Background sync (keeps running)
const result = await sntp.sync({background: true})
if (result.ok) {
  console.log('Initial sync:', result.value.time)
  // Later, stop background sync:
  result.value.stop()
}

sntp.setTimezone(tz)

ts
setTimezone(tz: string): void

Set the POSIX timezone string without syncing. Affects how Date objects display local time.

ts
sntp.setTimezone('CET-1CEST,M3.5.0,M10.5.0/3') // Central European Time

Types

SntpOptions

ts
interface SntpOptions {
  servers?: string[] // NTP servers (default: ['pool.ntp.org'])
  timezone?: string // POSIX TZ string (default: 'UTC0')
  timeout?: number // ms before timeout (default: 60000)
}

SntpSyncResult

ts
interface SntpSyncResult {
  time: Date
}

SntpSyncResultWithStop

ts
interface SntpSyncResultWithStop extends SntpSyncResult {
  stop: () => void
}

Errors

SntpError

VariantFieldsDescription
InitFailedmessageFailed to initialize SNTP
TimeoutmsSync timed out after ms milliseconds
CancelledSync was cancelled