Search code examples
azureazure-iot-hub

How can I get datetime on my device from Azure IoT Hub?


I need to update date and time in some devices that are connected to Azure via Azure IoT Hub.

Is there a way to get datetime from IoT Hub?

How to manage time zones?

What time zone will provide Azure IoT Hub?

Are there other alternatives?


Solution

  • Not aware of such API surface being present in Azure IoT Hub, however, your options include -

    • Use NTP against a public time server pool (pool.ntp.org). I would use a host from NIST or other better-funded-than-ntp-org entity - https://tf.nist.gov/tf-cgi/servers.cgi
    • Write an Azure Function that returns a date/time string based on a Location query parameter that you send from the device (GET or POST). For example http://somethingSomething.azurewebsites.net/api/now?location=NewYork
    • Use a ready-made public REST API, i.e. http://worldtimeapi.org/
    • "Abuse" your IoT Hub endpoint over HTTPS which returns date/time even for a 400 Bad Request, for example:

      $ curl https://poorlyfundedskynet.azure-devices.net -i
      
      HTTP/1.1 400 Bad Request
      Content-Length: 157
      ..snip other headers..
      Date: Wed, 11 Dec 2019 16:16:51 GMT
      

      Note: HTTPS only. Upside: you talk to the same host for everything. Downside: No guarantee timestamp is accurate, but then again so is every other service. Embrace a little risk! :)

    Edit:
    Linking to this relevant GitHub issue (under Azure IoT Hub client SDK for C).