Unix time reset using Timer.now()

#1

I am wanting to get a timestamp with the current time (from inside a timer function), and am using Timer.now() combined with Timer.fmt() see code below. But it looks like the Unix time is reset whenever the code is flashed, as I get dates from 1970 (or Unix timestamps starting from 0). I don’t know it this has to do at all with Mongoose OS, but I haven’t found info anywhere about the problem. Have looked at this thread for instance, but it didn’t help me as I have sntp in my libs already. I’m very grateful for any help thanks.

Timer.set(interval_pH, Timer.REPEAT, function() {
  let pH = get_pH();
  let timestamp_pH = Timer.now();
  let datetime_pH = Timer.fmt("%Y-%m-%d %H:%M:%S", timestamp_pH);
  print('pH from mJS: ', pH); 
  print('datetime pH: ', timestamp_pH);
  let json_str = '{pH: ' + JSON.stringify(pH) +'datetime_pH: ' + JSON.stringify(datetime_pH) +'}';
  // Publish data to pub/sub
  if (pH !== 0) {
    MQTT.pub(eventsTopic, json_str, 1);
  }  
}, null);
#2

Ah, it seems like when the device connects to the wifi, the date becomes correct!

#3

If you need formatted local time, you can add sys.tz_spec in mos.yml

#4

you may also want to check this thread too
and btw, unless you have an RTC running, time will start at 0, which is Unix’ epoch (jan 1st, 1970) and sync at sntp event, which you can check for validation.

#5

Ok, thanks! Will look at this!