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()
- JavaScript
- Python
device.time.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()
- JavaScript
- Python
device.time.toDate(timestamp)device.time.to_date(timestamp)| Parameter | Type | Description |
|---|---|---|
timestamp | number | Unix timestamp in milliseconds |
Returns a Date object (JS) or datetime object (Python)
toTimestamp()
- JavaScript
- Python
device.time.toTimestamp(date)device.time.to_timestamp(dt)| Parameter | Type | Description |
|---|---|---|
date / dt | Date / datetime | A Date (JS) or datetime (Python) object |
Returns Unix timestamp in milliseconds
setTimezone()
- JavaScript
- Python
device.time.setTimezone(tz)device.time.set_timezone(tz)| Parameter | Type | Description |
|---|---|---|
tz | string | IANA timezone string (e.g., "America/New_York", "Europe/London", "Asia/Tokyo") |
Get Synchronized Time
- JavaScript
- Python
const timestamp = device.time.now();
console.log("Current time (ms):", timestamp);
timestamp = device.time.now()
print("Current time (ms):", timestamp)
Convert Between Formats
- JavaScript
- Python
// 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);
from datetime import datetime
# timestamp to datetime
timestamp = device.time.now()
dt = device.time.to_date(timestamp)
print(dt.isoformat())
# datetime back to timestamp
ts = device.time.to_timestamp(datetime.now())
print(ts)
Set Timezone
- JavaScript
- Python
device.time.setTimezone("America/New_York");
device.time.set_timezone("America/New_York")
Uses IANA timezone identifiers. Common examples:
| Timezone | Identifier |
|---|---|
| US Eastern | America/New_York |
| US Pacific | America/Los_Angeles |
| UK | Europe/London |
| Central Europe | Europe/Berlin |
| India | Asia/Kolkata |
| Japan | Asia/Tokyo |
| Australia Eastern | Australia/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.