B4R Tutorial Getting local date & time (easy and free)

Discussion in 'B4R Tutorials' started by Kevin, Jun 2, 2018.

  1. Kevin

    Kevin Well-Known Member Licensed User

    I saw a post or two about how to get the local date & time with no easy answers. I need this myself for my first Arduino project so I had a look around and found a free API that does just this!

    Although they also have a premium API, their free one is generous, allowing 1 request per second. I intend to only check it once or twice a day, myself. I should add that I found this through a 2-year-old post online where one person said it isn't reliable. I've only tested it a few times but so far, so good.

    Info and how to sign up: https://timezonedb.com/

    Sample URL: http://api.timezonedb.com/v2/get-time-zone?key=YOURAPIKEY&format=xml&by=zone&zone=America/Chicago
    (Link will not work as it requires a working API key which I am not sharing. :p

    Sample response:

    Hope this helps someone!

    Now a question for Erel or anyone else who would know:

    I've read about the need to be efficient in B4R, so how would be the best way to process this response into something usable for updating our internal "clock" on the board?
    jgmdavies likes this.
  2. jgmdavies

    jgmdavies Member Licensed User

    Thanks Kevin - looks useful.
    What sort of Arduino clock are you using - is it from http://playground.arduino.cc/Code/Time or similar, or are you using an add-on hardware RTC board/shield?
  3. jgmdavies

    jgmdavies Member Licensed User

    Kevin likes this.
  4. derez

    derez Expert Licensed User

    See this example https://www.b4x.com/android/forum/threads/esp8266-requesting-time-from-a-server.68636/#post-435534, it does not need a special API.
    I use time from Italy :
    Public Sub Connect(u As  Byte)
    Log("Connecting to Italian server...")
    If socket.ConnectHost("time.ien.it",  13Then
    socket.Stream, "astream_NewData""astream_Error")
    Log("Failed to connect to Italian server")
    End If
    End Sub
    I calculate the offset by the dates.
    Kevin likes this.
  5. Kevin

    Kevin Well-Known Member Licensed User


    As I understood it, people were having issues converting the UTC to local while also accounting for Daylight Savings Time which starts to complicate things. Perhaps I'm wrong though - it wouldn't be the first time! :D
  6. Kevin

    Kevin Well-Known Member Licensed User

    I was looking at https://www.b4x.com/android/forum/threads/resp8266rtc-a-real-time-clock.72783/ but I don't know if there are better options or not. I'm very new to this stuff.

    I was planning on using this on an 8266. Either an 8266-01 or 12E. I am monitoring the weather (via forecast) and controlling a pool heater based on that information.

    Edit: Now I am not sure that is the thread I was looking at before. It may have been an older post before better solutions came about. So confused! :confused:
    Last edited: Jun 2, 2018
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice