Date() and time() issues

I’m using the following function in the device side of my Imp but I’m finding two issues

  1. To get an accurate .month from the date table I need to add 1 for some reason
  2. .usec is always 000

const TIMEZONE_OFFSET = 36000; // UTC+10:00 in seconds

function getDatetime()
local localImpDatetimeTable = date(time() + TIMEZONE_OFFSET);
local year = localImpDatetimeTable.year;
local month = format("%02u", localImpDatetimeTable.month + 1);
local day = format("%02u",;
local hours = format("%02u", localImpDatetimeTable.hour);
local minutes = format("%02u", localImpDatetimeTable.min);
local seconds = format("%02u", localImpDatetimeTable.sec);
local usec = format("%03u", localImpDatetimeTable.usec);

return (year+"-"+month+"-"+day+"T"+hours+":"+minutes+":"+seconds+"."+usec);


As per the documentation, the usec key is only available to agents. For that level of timing, you’re better off using hardware.micros().

All the values provided by date() begin at zero (ie. January = 0 not 1; December = 11 not 12). This is easy to adapt to in code. Yes, it’s odd for months, but it matches hours (0-23), mins (0-59), secs (0-59). Weekdays run from 0 to 6.

usec is microseconds, so it’s not always 000, it’s always 000000 :wink:


The reason month is zero-based is because that’s how the C standard library does it. Anything else would confuse us C programmers :wink: