B4A Library [B4X] SNTP class

Discussion in 'Additional libraries, classes and official updates' started by Erel, Nov 14, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This class sends a request to a SNTP server (time server) and finds the offset between the local clock and the remote clock.

    Usage example:
    Code:
    SntpClient.Initialize 'global variable. Put in Starter service in B4A
    Wait For(SntpClient.UpdateOffsets) Complete (Result As Boolean)
    If Result Then
       
    Log("Time updated successfully.")
       
    Log($"$Time{SntpClient.Now}"$)
    End If
    You only need to calculate the offsets once. Once calculated you can call Sntp.Now to get the current time.

    This class will work in B4A and B4J. It should be simple to port it to B4i if needed.

    Depends on: Network, RandomAccessFile and JavaObject (for shift left operation on Long variables).

    It is based on Android source code - Apache license 2.0.
     

    Attached Files:

  2. Filippo

    Filippo Expert Licensed User

    Thank you! :)
    That was faster than the police allowed. :D
     
    Claudio Oliveira likes this.
  3. Filippo

    Filippo Expert Licensed User

    I have done a few tests with this module and the library.
    There are always different results between the two methods, and now I do not know what should be the right one. :(

    b4a-SNTP.JPG
     

    Attached Files:

    Claudio Oliveira likes this.
  4. Erel

    Erel Administrator Staff Member Licensed User

    In the first one the difference is about 250ms. In the two other cases it is very small.
    There will be differences even if you use the same implementation multiple times.
     
    Claudio Oliveira likes this.
  5. Filippo

    Filippo Expert Licensed User

    But I think your routine is better than the library, it takes less time compared to the library. ;)
    And besides, I do not need those two objects anymore.
    Code:
    'Objekte für die Atomzeit
        Private Thread1 As Thread
        
    Private Thread2 As Thread
     
    Claudio Oliveira likes this.
Loading...
  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