B4A Library [B4X] SNTP class

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:
B4X:
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, B4J and B4i.

Depends on: Network and RandomAccessFile.

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

Attachments

  • SNTP.bas
    2 KB · Views: 330
Last edited:
D

Deleted member 103

Guest
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
 

Attachments

  • B4a-SNTP.zip
    8.3 KB · Views: 308
D

Deleted member 103

Guest
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.
B4X:
    'Objekte für die Atomzeit
    Private Thread1 As Thread
    Private Thread2 As Thread
 

Lakhtin_V

Active Member
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.
B4X:
    'Objekte für die Atomzeit
    Private Thread1 As Thread
    Private Thread2 As Thread
Hello! I am getting an error when loading this code. It is not clear how to solve this problem. I use B4A
1666769476767.png
 

Lakhtin_V

Active Member
Licensed User
Delete this code. The important code is in the class in the first post.
You advised to disable some lines. I corrected the code in the example, but the result is no LOG does not appear, the code does not work.
Get_Current_Time_whit_b4amodul:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    'Objekte für die Atomzeit

    'Objekte für die Atomzeit
'    Private Thread1 As Thread
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    'Library und Objekte für die Atomzeit
    Dim sntplib As SntpTime
    Dim sntpticks As Long

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    'Objekte für die Atomzeit
'    Thread1.Initialise("Thread1")

'    GetAtomTime
    Get_Current_Time_whit_b4amodul(10000)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

'Sub GetAtomTime
'    Thread1.Start(Me, "Get_Current_Time_whit_Library", Null)
'End Sub

Sub Get_Current_Time_whit_Library
    sntplib.NtpServerName = "0.pool.ntp.org"    ' only needed if you don't want to use the default server "0.us.pool.ntp.org"
    sntplib.TimeOut = 10000                         ' only needed if you don't want to use the default timeout value of 30000 milliseconds (30 seconds)
    sntpticks = sntplib.getGmtNtpTime
    If sntpticks > -1 Then
        Starter.timoffset = DateTime.Now - sntpticks
    Else
        Starter.timoffset = 0
    End If
   
    Log("Get_Current_Time_whit_Library:")
    Log("ticks=" & sntpticks)
    Log("timoffset=" & Starter.timoffset)
    Log(" ")
End Sub

Public Sub Get_Current_Time_whit_b4amodul(ticks As Long)
    If ticks > -1 Then
        Starter.timoffset = DateTime.Now - ticks
    Else
        Starter.timoffset = 0
    End If

    Log("Get_Current_Time_whit_b4amodul:")
    Log("ticks=" & ticks)
    Log("timoffset=" & Starter.timoffset)
    Log(" ")

End Sub
In the manifest I changed the line
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="30"/>
1666776603803.png

When installing on a smartphone -
"the application is not installed message appears. The package is probably damaged."
 
Last edited:

Lakhtin_V

Active Member
Licensed User
1. Don't waste your time with activities. Switch to B4XPages.
2. It is very simple to use this class. It is explained in the first post.
3. There is no relation between your code and this class. It is completely wrong.
Erel, you are always right, I figured out the problem, now everything is working, I will check it on different devices.
 
Top