setTime

Posted on
  • I am trying to set the time on my Espruino loaded esp8266. I am using an http get request and getting a string like "2016-01-25T12:33:20+00:00" back from the server. When I try to set the time it never sets it correctly.

    Using...
    setTime(Date.parse("2016-01-25T12:33:20+­00:00"));

    If I do Date.parse("2016-01-25T12:33:20+00:00");­, the ms returned is wrong. If this is not a proper format date string then what is?

  • Moving this to the ESP8266 section...

    I'm not sure setTime is implemented on ESP8266, but if it is, it takes the seconds since 1970 - and Date returns the milliseconds.

  • I got it to work.

    var timeS;
    require("http").get("http://www.timeapi.­org/utc/7+hours+ago?\\s", function(res) {
      res.on('data', function(data) {
        console.log("HTTP> "+data);
        timeS=Number(data);
      });
      res.on('close', function(data) {
        console.log("Connection closed");
        setTime(timeS);
      });
    });
    
  • Neat!

  • I have some things here that do this, and it works fine for me:

            require("http").get("http://192.168.1.10­1", function(res) {
              console.log("Response: ",res);
              if ("Date" in res.headers) {
                console.log("Got Date ",res.headers.Date); 
                var date = new Date(res.headers.Date);
                var d = date.getTime();
                if (date.getMonth()>2/*Mar*/ && date.getMonth()<=9/*Oct*/)
                  d += 1000*3600; // 1 hour daylight saving
                if (d>0) setTime(d/1000);
                else console.log("Unable to parse date!");
              }
            });
    

    It just pulls the date field right off the HTTP headers, so all you need is a standard HTTP server.

  • No local server in my home. Plan to create a http server that has current time with weather info etc. Will see if it runs out of memory before I get it done.

  • Just to add that literally any HTTP server will do - including google.com, or in some cases even your broadband router (if it keeps an accurate date).

  • @Gordon capitalize your last line, it's simply brilliant :)

    Altho not exactly correct if you are other timezone than the UK, and want to get your local time.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

setTime

Posted by Avatar for cwilt @cwilt

Actions