iOS Question Get date time from jServer in b4i client and convert to local date and time

Binary01

Active Member
Licensed User
Longtime User
Hi,

I run jServer on vps.
I want to get current date and time of jServer from jRDC Client and convert to local date/time.
My code is as follow:


B4X:
Public Sub GetServerDateTime As String
Try
        Dim j As HttpJob
        Dim str1 as String

        j.Initialize("", Me)
        j.Download(Main.rdCheck)   'rdCheck is  http://myip:port/test
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            str1  = j.GetString
            If str1.Contains("successful") Then
                Dim f,l As Int
                f=mydb23.FindString(str1,"(")
                l=mydb23.FindString(str1,")")
                str1 =str1.SubString2(f+1,l)
                DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
                Dim ServerTick As Long = DateTime.DateParse(str1)
             
                j.Release
             
                Dim addTimeZoneOffsetTicks As Long

                addTimeZoneOffsetTicks = 7.5 * (60 * 60 * 1000)
                DateTime.DateFormat = "MM/dd/yyy HH:mm:ss"
                Dim ntz As Long
                ntz=ServerTick+ addTimeZoneOffsetTicks
             
                DateTime.DateFormat="dd-MMM-yyyy KK:mm:ss a"
                str1 = DateTime.Date(ntz)   'ServerTick
             

             End If
        End If

        j.Release
        return str1
     
    Catch
        Msgbox (LastException.Message,"Error")
        return str1
    End Try
end Sub


Public Sub FindString(s As String, SearchFor As String) As Int
    Try
        Dim res As Int = -1
        Dim n As Int = s.Length - 1
        For i = 0 To n
            res = s.IndexOf2(SearchFor, res + 1)
            If res = -1 Then
                Return -1
            Else
                i = n
            End If
        Next
        Return res
    Catch
        Log(LastException)
        Return res
    End Try
 
End Sub

Is there alternative code?

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is there alternative code?
Yes.

Lets start with the unneeded FindString. You can replace it with:
B4X:
s.LastIndexOf(SearchFor)

However all the code you posted is not needed and incorrect.
You control the server, just return the ticks values instead of the string (best option) or add the time zone to the date format.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
got the return message from server after runing rdCheck = "http://139.60.190.200:27280/test" as follow:
This is a creative solution however it is not the best one :)

Add a new ServerHandler class to your project named TimeHandler.

Put this code in the Handle sub:
B4X:
resp.Write(DateTime.Now)

Register the new handler in the Main module:
B4X:
srvr.AddHandler("/time", "TimeHandler", False)

Call the new handler.
 
Upvote 0
Top