B4J Question [Solved] Elapsed Time starts from 01:00:00 not 0:00:00??

Mark Read

Well-Known Member
Licensed User
Longtime User
This has got to be simple but I cannot see it. This is the code:

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("PiLogger")                         'Load the layout file.
    MainForm.Show
    Timer1.Initialize("Timer1",1000)                                'prepare timer
   
    If File.IsDirectory(File.DirApp,"windsonic/") = False Then
        File.MakeDir(File.DirApp,"windsonic/")
        Log("Directory created!")
    End If
   
    FileDirectory=File.DirApp & "\windsonic\"
    Log(FileDirectory)
   
End Sub

Sub Timer1_Tick
    Dim t, String1 As String    
    DateTime.TimeFormat="HH:mm:ss"
    t = DateTime.Time(DateTime.Now)
    Log(t)
       
    ElapsedTime=DateTime.Time(DateTime.Now-StartTime)                'calculate elapsed time
    lbl_ElapsedTime.Text=ElapsedTime                                'update UI
   
                                                                    'This is one line in text file
    String1=t & "," & ElapsedTime & "," & lbl_Speed.Text & "," & lbl_Direction.Text & Chr(13)
   
    Writer.WriteLine(String1)                                        'write to file
End Sub

Sub btn_StartStop_MouseClicked (EventData As MouseEvent)
    If NewFileCreated=False Then
        Return
    End If
   
    If btn_StartStop.Tag="0" Then                                    'Logger is off, turn on
        btn_StartStop.Tag="1"                                        'set tag to on
        lbl_LoggerStatus.Style="-fx-background-color: green"        'change background color
        btn_StartStop.Text="Stop Logger"                            'change button text
        Timer1.Enabled=True                                            'start timer
        DateTime.TimeFormat="HH:mm:ss"
        StartTime=DateTime.Now
    Else
        btn_StartStop.Tag="0"
        lbl_LoggerStatus.Style="-fx-background-color: red"
        btn_StartStop.Text="Start Logger"
        Timer1.Enabled=False
        Writer.Close
    End If
End Sub

Output from text file:

B4X:
13:56:32,01:00:01,0,45,227,0
13:56:33,01:00:02,0,45,227,0
13:56:34,01:00:03,0,45,227,0
13:56:35,01:00:04,0,45,227,0
13:56:36,01:00:05,0,45,227,0
13:56:37,01:00:06,0,45,227,0
13:56:38,01:00:07,0,45,227,0
13:56:39,01:00:08,0,45,227,0
13:56:40,01:00:09,0,45,227,0
13:56:41,01:00:10,0,45,227,0

I click btn_StartStop to start. The elapsed time starts from 1 hour instead of 0. Where is my mistake?
 

stevel05

Expert
Licensed User
Longtime User
If I remember correctly it's something to do with the time zone, Try setting the TimeZone Offset to 0:

B4X:
DateTime.SetTimeZone(0)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
@Erel.

You mean, this line is wrong?

B4X:
ElapsedTime=DateTime.Time(DateTime.Now-StartTime)

DateTime.Now and StartTime are both long format but the DateTime.Time is the wrong command?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Using DateTime.Time to convert a "period" value to a string is wrong. DateTime.Time takes a time instance value. Time instance value = milliseconds from 1/1/1970 00:00 GMT.

Generally speaking it is also problematic to calculate the time difference with a simple subtraction. It is better to use DateUtils.PeriodBetween. However in this case it is fine as you are not dealing with days.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Thanks Erel. Changed this

B4X:
ElapsedTime=DateTime.Time(DateTime.Now-StartTime)

to this

B4X:
ElapsedTime=ConvertTicksToTimeString(DateTime.Now-StartTime)

and added this sub

B4X:
Sub ConvertTicksToTimeString(t As Long) As String
    Dim  hours, minutes, seconds As Int
    hours = t / DateTime.TicksPerHour
    minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
    seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
    Return NumberFormat(hours, 2, 0) & ":" _
        & NumberFormat(minutes, 2, 0) & ":" & NumberFormat(seconds, 2, 0)
End Sub

Output is now

B4X:
09:11:52,00:00:01,0.45,227.0
09:11:53,00:00:02,0.45,227.0
09:11:54,00:00:03,0.45,227.0
09:11:55,00:00:04,0.45,227.0
09:11:56,00:00:05,0.45,227.0
09:11:57,00:00:06,0.45,227.0
09:11:58,00:00:07,0.45,227.0
09:11:59,00:00:08,0.45,227.0
09:12:00,00:00:09,0.45,227.0
09:12:01,00:00:10,0.45,227.0

Perfect!
 
Upvote 0
Top