Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Set actual date/time

Is there any way to set actual date/time globally?
For example, after rebooting, I can get current time from non-volatile real time clock module via I2C bus. So, for more convenient usage I'd like to set actual time in the operating system and then just call Date.now() in JS code. Now, it returns 1970-01-01.
My platform is ESP8266.

Comments

  • edited August 2016

    Hi!
    What do you mean by Date.now()?
    MG IoT (except POSIX) compiled w/out Date object support.

  • What port (ESP, POSIX, CC3200) do you use?

  • edited August 2016

    Hi! ESP port. I've built firmware with -DV7_BUILD_PROFILE=3 feature. So, now I have Date object support in JS API. But when I tried to call console.log(new Date()); in command prompt, I've got something like 1970-01-01 00:01:00. It seems like system clock starts from 1970-01-01. So, I'd like to setup actual time... I can get time via I2C RTC or NTP, but I don't know how to set it up globally.

  • On the first approach, I could store difference between actual time and wrong system time. And the next time just sum the difference and system time.
    But maybe there is a better way to solve the problem?

  • Got it.
    Unfortunately, now MG IoT doesn't provide any API to set time.
    But this (or similar) feature is in feature short-list and we are working on it right now.

  • aab1986aab1986 México
    edited August 2016

    Hi, don't know if this will help you, but here is the solution I actually use to get the current time of my city.

    var httpRequest = function(hostname, path, method) {
      var response = "";
      var client = Http.request({
       hostname: hostname,
       port: 80,
       path: path,
       method: method,
       headers: { }
      }, function(res) {
        try {
          response = res.body;
        } catch(err) {
          response = "error";
        }     
      }).end().setTimeout(3000, function() {
        // this.abort();
      });
      return {
        "response": function() {
          return response;
        }
      }
    }
    
    var timeRequest = function() {
      var refGeonames = "";
      function postHttpRequest() {
        if (refGeonames != "") {
          var vBarPosition = refGeonames.indexOf("|"),
          refTime = refGeonames.slice(vBarPosition + 13),
          refTimeInSec = refTime.slice(0, 2) * 3600 + refTime.slice(3) * 60,
          sysTimeOld = refGeonames.slice(0, vBarPosition - 1),
          CurrTimeInSec = refTimeInSec + Sys.time() - sysTimeOld,
          h = parseInt(CurrTimeInSec / 3600) % 24,
          m = parseInt(CurrTimeInSec / 60) % 60,
          time = (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m);
          return time;
        }
      }
      function httpRequestCall() {
        var request, path = "";
        // need your own username so sign up at geonames.org and change values lat and lng
        path = "/timezoneJSON?lat=19.432608&lng=-99.133208&username=xxxxx";
        request = httpRequest("api.geonames.org", path, "GET");
        var interval = setInterval(function() {
          var content = request.response();
          switch(content) {
            case "":
              break;
            case "error":
              break;
            default:
              clearInterval(interval);
              refGeonames = Sys.time() + " | " + JSON.parse(content).time;
          } 
        }, 100);
      }
      httpRequestCall();
      return postHttpRequest;
    }
    
    var timeNow = timeRequest();
    

    then you can poll for timeNow() or whatever

  • Is there any update on this? Is it possible to set time in the current version?

  • nliviunliviu Romania

    If the device is connected to internet, the sntp library synchronizes the date/time of your device with an ntp server.
    To use it, just add

     - origin: https://github.com/mongoose-os-libs/sntp
    

    to the libs section of mos.yml.

Sign In or Register to comment.