I make call to date() and parse out hours and minutes. Creating minutes since midnight Recently, every so often date returns the hour exactly 1 hour back. This is pre tz offset, This code has run for years wo change and now started exhibit this issue
Any changes to date? I make the call often, a few times a minute. Once it changes it is stuck an hour back until I restart
Can you please DM a device id or mac address and an example timestamp for when it is misbehaving?
Working on the “when it happens now”. Hard to catch without watching debugging for 24 hours. But as an example, it will now send an email if the time jumps back, not flagging for midnight yet. So, a mail was sent this morning catching the hour jump back.
“Time moved back, was : 463 Now 404”
463 in min 07:24 ( 7 *60 + 24), I save that, the next call to date(); gave an hour earlier. Same code has run for 8 or so years!!!
tz = -6, this is from openweather and is NOT changing in the debug log.
Device is an IMP002, 8 plus years old.
function getTime()
{
local now = date();
local min_count = now.min;
local hours_count = now.hour;
// Apply World Time shift if set
//server.log("hours_count " + hours_count);
hours_count = hours_count + tz;
// server.log("hours_count " + hours_count);
// Catch wacked out clock
if (hours_count >= 24) {
hours_count = hours_count - 24;
server.log("hours_count >24 " + hours_count);
}
// Adding TZ to hours can put the hours negative, so pull them back. 02:00 - 6 (tz) would make negative hour
if (hours_count < 0) {
hours_count = 24 + hours_count;
server.log("hours_count <0 " + hours_count);
}
local time_in_mins = hours_count * 60 + min_count;
server.log("Time is " + hours_count + ":" + min_count + " in mins "
+ time_in_mins);
server.log("Now is : " + time_in_mins + " lastTime : " + lastTime);
if ( lastTime > time_in_mins ){. // Catch time going backward
if (time_in_mins != 0 ){ // Catch midnight
local a = format("Time moved back, was : %d Now %d" ,lastTime, hours_count*60+min_count);
server.log(a);
sendMail("TIME CHANGE", a); // send an email with the time going back.
}
}//
lastTime = time_in_mins;
return (time_in_mins);
}
Thanks for the additional info.
I’ve checked the server logs and cannot see any out of order times being sent to the device over the last 48 hours, so it looks like a local issue.
Your device has been running impOS 42.9 since March 2023, so that also hasn’t changed recently.
Are you able to narrow down when it first started happening? Is it always exactly one hour out?
Can you recover the correct time by disconnect/connect instead of a full restart?
This baby runs on autopilot, but my best guess is less than a month ago and really 5-7 days now. I can look at adding a disconnect and connect. Any way to force an NTP call?
Reconnecting will update device time from the imp server (it’s not NTP though).
So, set a flag that time jumped, timeJump = TRUE; Code stolen from example: The example in the docs calls loop, do I need that or is the imp still running loop?
imp.onidle(function() {
// Action the disconnection
if (timeJump ){
timeJump = FALSE;
server.log("Going offline");
server.flush(10);
server.disconnect();
// loop(); Do I need this.....Seems I should not?
// Start a timer to bring the connection back up after 1 minutes
reconnect();
});
// Start a timer to bring the connection back up after 60 seconds
function reconnect() {
imp.wakeup(60, function() {
server.connect(connectHandler);
});
}
// Handle the outcome of reconnection events
function connectHandler(connectStatus) {
if (connectStatus != SERVER_CONNECTED) {
// Device couldn't reconnect, so try again shortly
reconnect();
} else {
// Device is connected
server.log("Server disconnect, reconnect complete");
}
}
imp.onidle()
is one-shot, so you probably want to call something like this where you set timeJump=TRUE
function onTimeJump() {
imp.onidle(function() {
server.log("Reconnecting");
server.flush(10);
server.disconnect();
server.connect();
});
}
Thanks, i will put in and run a test flag against it.
Okay, have the code in to disconnect, reconnect. I it will send an email after with times after it recovers the connection. I’ll post once I see it again.