B4A Library SPSntp - getting time from the internet

Hi again,

i took this sample code as a base to build this small library.

The aim was to receive the actual time from the internet in ticks (timezone GMT). I skipped the string conversion to EST so only the tick values are returned.

Usage:
B4X:
Dim sntp As SntpTime
Dim ticks As Long
sntp.NtpServerName = "0.de.pool.ntp.org"  ' only needed if you don't want to use the default server "0.us.pool.ntp.org"
sntp.TimeOut = 10000 ' only needed if you don't want to use the default timeout value of 30000 milliseconds (30 seconds)
ticks = sntp.getGmtNtpTime
Msgbox(ticks, "GMT Result in Ticks")
Msgbox(DateTime.Date(ticks), "GMT Date")
Msgbox(DateTime.Time(ticks), "GMT Time")
Msgbox(DateTime.Time(sntp.NtpTimeReference), "NtpTimeReference")
Msgbox(sntp.RoundTripTime, "RoundTripTime")
If an error occurrs during the time request (e.g. if a wrong server name was set), all time values return -1.


EDIT: Version 1.01 - added option to set an an own/preffered time server (default = "0.us.pool.ntp.org")
EDIT: Version 1.02 - the "android.permission.INTERNET" is now added automatically
EDIT: Version 1.03 - added option to set an own/preferred timeout value (default = 30000ms)

Caution: The way of handling network connections has changed since android 3 (you have to do all network connections within an non-gui background process) and this library does not support it directly. So if you wan't to use it with android 3 an above you have to handle the necessary threading on on your own.
Look at the attached sample "Sntp-Sample-V1.04".

specci48
 

Attachments

  • SPSntp_V1.03.zip
    5.6 KB · Views: 854
  • SPSntp-Sample-1.04.zip
    7.5 KB · Views: 814
Last edited:

AscySoft

Active Member
Licensed User
Longtime User
wow, this is perfect. Just what I needed.
now all I have to do is to convert from GMT to my local time.
Any suggestion?

And another thing, is there any possibility to be able to set (my) sntp server from code?
 
Last edited:

specci48

Well-Known Member
Licensed User
Longtime User
specci48 you can add the permission declaration to your library. Add this annotation:
B4X:
@Permissions(values={"android.permission.INTERNET"})

Thanks for the tip Erel. :) Version 1.02 posted.
 

diego

Member
Licensed User
Longtime User
Great! I needed this kind of library because I can't access to GPS time, so thanks a lot!

Just one doubt:
"RoundTripTime" is the gap between server time and "reception" time?

If so, should I add this value to NTPtime to get accurate real time?

EDIT: wrong, it seems getGmtNtpTime already takes into account transmission time.
I've tried it but I get different time if doing by WIFI or cellular (almost 1 second).


thanks again, I wish I could manage with Java and create libs... :sign0188:
 
Last edited:

luke2012

Well-Known Member
Licensed User
Longtime User
Very interesting

Only a question.
How about the today date?
In my app I need to obtain the today date from a NTP server. It's possible ?
 

specci48

Well-Known Member
Licensed User
Longtime User
Very interesting
Only a question.
How about the today date?
In my app I need to obtain the today date from a NTP server. It's possible ?
With this lib you receieve the full time information so the date is included. See first post or SntpSample.zip.
Remember that you have to "correct" the result to your current timezone.
 

slugger

Member
Licensed User
Longtime User
I am trying this library right now.

I need a real-time time and I am wandering around the place to find weak Wifi spots :) to test my application.

The result is if I don't have a Wifi signal the
B4X:
ticks=sntp.getGmtNtpTime
waits a lot of seconds, apparently about 30.

Is there a way to pass a number of seconds (or milliseconds) to the function call?

Something like this would be great:

B4X:
ticks=sntp.getGmtNtpTime(500)  'half a second timeout

If I don't get the ticks after the supplied number of milliseconds then return -1.
 

slugger

Member
Licensed User
Longtime User
Hello,

I am using this library to retrieve date and time via some NTP servers.

I have a problem at runtime, it appears in my program and in your SNTP SAMPLE project as well.

If I select "Debug" or "Release" in the B4A IDE and then run it in my tablet both programs work fine.

On the other hand if I select "Release (obfuscated)" in the IDE and run the example in my tablet I get the following error as soon as I click the GetTime button.

B4X:
main_btngettime_click (java line: 286)

java.lang.RuntimeException: Thread.Start : Sub getcurrenttime not found!

    at anywheresoftware.b4a.agraham.threading.Threading.Start(Threading.java:207)
    at b4a.example.main._btngettime_click(main.java:286)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:63)
    at android.view.View.performClick(View.java:3511)
    at android.view.View$PerformClick.run(View.java:14105)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Thread.Start : Sub getcurrenttime not found!

I get the very same error in my program.

Any hints on how to get it working when compiled using the obfuscation option?
 

slugger

Member
Licensed User
Longtime User
I have found a solution, I renamed GetCurrentTime and ViewResults to

Get_CurrentTime
View_Results

and now it is working.



steve
 

specci48

Well-Known Member
Licensed User
Longtime User
Yes, the sub names get obfuscated, too. So the sub names provided as parameters in the functions Start and RunOnGuiThread cannot find their targets any more. The underscore (preventing the obfuscation of the string) is the right solution for this.

I've updated the sample to prevent other user from getting the same problem.

Sorry for this little confusion. :(

specci48
 

Del

Member
Licensed User
Longtime User
This probably sounds like a newbie question but I am having trouble with this line and subsequent references to the Thread library in Sntp-Sample-V1.04.

Dim Thread1 As Thread 'ignore => ignore warning of this line

There may be a clue in the 'ignore => remark but putting it simply :- after a two hour search I cannot find the Threading library.

Clearly I am missing something obvious. I desperately need to get the right time in my app. Please enlighten me.

Many thanks
 

specci48

Well-Known Member
Licensed User
Longtime User
The threading library can be found here.

And here is a decription of the warnings engine and how to handle compiler warnings.
 

Del

Member
Licensed User
Longtime User
Many thanks specci48 for this quick reply, I am most grateful.

However I am now very embarrassed to have to ask : - where can I find the library that holds the Sntp object ?

And more generally (OK new thread coming), how can I avoid having to ask questions like this in the future ?

Many many thanks
 

johndb

Active Member
Licensed User
Longtime User
Many thanks specci48 for this quick reply, I am most grateful.

However I am now very embarrassed to have to ask : - where can I find the library that holds the Sntp object ?

And more generally (OK new thread coming), how can I avoid having to ask questions like this in the future ?

Many many thanks
The library is available as a download in the first post.
It's okay to post questions of any kind but you should search the forum first as the majority of questions have already been answered. ;)
 
Top