Skip to main content

Time

NTP-synchronized timestamps that keep your device's clock consistent with server time — even if the device clock drifts. All telemetry and event readings are automatically timestamped using this module.

Overview

On initialization, the SDK syncs with an NTP server (time.google.com) and calculates an offset between the device clock and the real time. This offset is applied whenever you call now() and whenever the SDK timestamps telemetry or event payloads. The offset is refreshed automatically every 3 hours.

Access via device.time.

API

now()

device.time.now()

Returns the current NTP-corrected Unix timestamp in milliseconds

Does not require an active connection — uses the cached offset from the last sync.

toDate()

device.time.toDate(timestamp)
ParameterTypeDescription
timestampnumberUnix timestamp in milliseconds

Returns a Date object (JS) or datetime object (Python)

toTimestamp()

device.time.toTimestamp(date)
ParameterTypeDescription
date / dtDate / datetimeA Date (JS) or datetime (Python) object

Returns Unix timestamp in milliseconds

setTimezone()

device.time.setTimezone(tz)
ParameterTypeDescription
tzstringIANA timezone string (e.g., "America/New_York", "Europe/London", "Asia/Tokyo")

Get Synchronized Time

const timestamp = device.time.now();
console.log("Current time (ms):", timestamp);

Convert Between Formats

// timestamp to Date
const timestamp = device.time.now();
const date = device.time.toDate(timestamp);
console.log(date.toISOString());

// Date back to timestamp
const ts = device.time.toTimestamp(new Date());
console.log(ts);

Set Timezone

device.time.setTimezone("America/New_York");

Uses IANA timezone identifiers. Common examples:

TimezoneIdentifier
US EasternAmerica/New_York
US PacificAmerica/Los_Angeles
UKEurope/London
Central EuropeEurope/Berlin
IndiaAsia/Kolkata
JapanAsia/Tokyo
Australia EasternAustralia/Sydney

Auto-Resync

The offset is refreshed automatically every 3 hours while the device is connected. If the NTP sync fails silently (e.g., network issue), the SDK keeps using the previous offset — now() never breaks.