MojoScale Studio Docs
API Reference

time

Real-world time, date, NTP synchronization, and DS3231 RTC support.

Studio Docs Runtime

import time

The time module provides wall-clock time for timestamps, logging, scheduled actions, telemetry, and offline devices. Unlike system.millis(), which measures time since boot, time represents actual UTC calendar time. ESP32 devices do not automatically know the current date and time after power up. Synchronize over Wi-Fi with NTP, set the clock manually, or load it from a DS3231 hardware RTC.

Quick example

import time
import wifi

wifi.connect(env.WIFI_SSID, env.WIFI_PASS)

def clock_ready():
    print(time.now())
    print(time.epoch())

time.on_sync(clock_ready)
wifi.on_connect(lambda: time.sync())

rtc = time.rtc(type="ds3231", sda=8, scl=9)
if rtc.is_valid():
    rtc.load()
    print(time.now())

importtime

Real-world time, date, NTP synchronization, and DS3231 RTC support.

The time module provides wall-clock time for timestamps, logging, scheduled actions, telemetry, and offline devices. Unlike system.millis(), which measures time since boot, time represents actual UTC calendar time. ESP32 devices do not automatically know the current date and time after power up. Synchronize over Wi-Fi with NTP, set the clock manually, or load it from a DS3231 hardware RTC.

API 19 available

time.sync() -> bool

Synchronize the system clock using NTP.

The device must already have network connectivity. Use wifi.on_connect() when synchronization should start after Wi-Fi receives an IP address.

Returns

bool bool True when SNTP was started. Use on_sync() to run code after the clock is actually synchronized.

time.on_sync(callback: callback) -> bool

Register a callback that runs after successful clock synchronization.

Parameters
Name Type Pass as Required Description
callback callback positional or keyword Yes Function called after NTP synchronization or another clock sync event completes.
Returns

bool bool True when the callback was registered.

time.is_synced() -> bool

Return whether the system has valid real-world time.

Returns

bool bool True when the clock was synchronized or explicitly set.

time.now() -> str

Return the current UTC date and time as an ISO 8601 string.

Returns

str str UTC timestamp in YYYY-MM-DDTHH:MM:SSZ form.

time.epoch() -> int

Return the current Unix timestamp.

Returns

int int Seconds since 1970-01-01T00:00:00Z.

time.datetime() -> dict

Return the current UTC date and time as a dictionary.

Returns

dict dict with year, month, day, hour, minute, second, and weekday fields.

time.date() -> str

Return the current UTC date.

Returns

str str Date in YYYY-MM-DD form.

time.clock() -> str

Return the current UTC time of day.

Returns

str str Time in HH:MM:SS form.

time.set(timestamp: int) -> bool

Set the system clock manually.

Parameters
Name Type Pass as Required Description
timestamp int positional or keyword Yes Unix timestamp in seconds.
Returns

bool bool True when the system clock was set.

time.timezone(offset_minutes: int) -> bool

Set the local UTC offset in minutes.

The internal clock remains UTC. This offset only affects local().

Parameters
Name Type Pass as Required Description
offset_minutes int positional or keyword Yes Fixed offset from UTC in minutes. For UTC-4 use -240. For UTC+5:30 use 330.
Returns

bool bool True when the local offset was stored.

time.local() -> str

Return local time using the configured fixed UTC offset.

Fixed offsets do not automatically handle daylight-saving-time transitions. Prefer UTC for telemetry, storage, and machine-to-machine messages.

Returns

str str Local ISO 8601 timestamp such as 2026-08-25T17:12:18+05:30.

time.rtc(type: str, sda: int, scl: int, port: int = None, address: int = None, frequency: int = None) -> time_rtc

Create a DS3231 hardware RTC object.

A real-time clock lets a device retain date and time while the ESP32 is powered off. The DS3231 connects over I2C and uses a backup battery.

Parameters
Name Type Pass as Required Description
type str positional or keyword Yes RTC device type. Use "ds3231".
sda int positional or keyword Yes I2C SDA GPIO connected to the RTC module.
scl int positional or keyword Yes I2C SCL GPIO connected to the RTC module.
port int positional or keyword No Optional I2C controller number. Defaults to 0.
address int positional or keyword No Optional I2C address. Defaults to 0x68.
frequency int positional or keyword No Optional I2C bus frequency in Hz. Defaults to 100000.
Returns

time_rtc time_rtc object.

time.rtc().now() -> str

Read current UTC time directly from the RTC as an ISO 8601 string.

time.rtc().epoch() -> int

Read RTC time as a Unix timestamp.

time.rtc().set(timestamp) -> bool

Set the RTC from a Unix timestamp.

Parameters
Name Type Pass as Required Description
timestamp int positional or keyword Yes Required value.

time.rtc().load() -> bool

Load RTC time into the system clock.

time.rtc().is_valid() -> bool

Return True when the RTC contains valid time.

time.rtc().info() -> dict

Return RTC type, I2C pins, address, frequency, and validity.

time.rtc().close() -> bool

Release the RTC I2C resources.