Android Question Chrono Pause, Stop, Reset with DateUtils

zed

Active Member
Licensed User
Good morning all.
Here is an example of a chrono that uses system time and the DateUtils library.

The principle is very simple.
It takes two different times to use DateUtils.PeriodBetween (H_Start, H_End).
H_start is obtained when the start button is clicked. It's system time. she is frozen.
H_End is the current time second after second thanks to the T_clock timer.
You just have to display the different time between the two.

When we click on the start button again after the break.
H_Start is obtained by the system time minus the time already measured.
H_End remains the same.

It's not much, but maybe someone will be interested in it.

Here is the code:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Private T_clock As Timer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private clk_BtPause As Boolean = False
    Private myTimeZone As Int
    Private H_Start, H_End As String
    Private lblDiff As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    'Defined the TimeZone and general date time format
    Dim s, d As String
    Dim l As Long
    s = DateTime.DateFormat
    DateTime.DateFormat = "MM-dd-yyyy HH:mm:ss"
    l = DateTime.Now
    d = DateTime.Date(l) & " GMT"
    DateTime.DateFormat = "MM-dd-yyyy HH:mm:ss z"
    myTimeZone = -Round((l - DateTime.DateParse(d))/3600000)
    DateTime.DateFormat = s
    DateTime.SetTimeZone(myTimeZone)
    DateTime.DateFormat = "EEEE dd MMMM" 'Date format
    DateTime.TimeFormat="HH:mm:ss" 'Time format
    
    'default chrono label
    lblDiff.Text = "00:00:00"

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub btStart_Click
    
    If clk_BtPause = True Then 'Relaunch after clicking the pause button
    
        'Convert string -pause time- to long. The chrono time when we click on the pause button ex: 00:01:23
        Dim dfPause As String = DateTime.DateFormat
        Dim pause As Long
        DateTime.DateFormat = "HH:mm:ss"
        pause = DateTime.DateParse(lblDiff.Text)
        DateTime.DateFormat = dfPause
        
        'Convert string -current time- to long. The system time when we click on the pause button
        Dim dfCurrTime As String = DateTime.DateFormat
        Dim currTime As Long
        DateTime.DateFormat = "HH:mm:ss"
        currTime = DateTime.DateParse(DateTime.time(DateTime.Now))
        DateTime.DateFormat = dfCurrTime
                
        'We restart at the current system time minus the break time.
        Dim myStartTime As Long
        DateTime.SetTimeZone(0)
        myStartTime = currTime - pause
        DateTime.DateFormat="HH:mm:ss"
        
        'New start time - the time is 2 times in the restart string. I do not know why
        Dim restart As String = DateUtils.TicksToString(myStartTime)
        Dim mystart() As String
        mystart = Regex.Split(" ",restart)
        H_Start = mystart(0)
        
        'End time is now
        DateTime.SetTimeZone(myTimeZone)
        H_End = DateTime.time(DateTime.Now)
        T_clock.Enabled = True
        
        clk_BtPause = False
        
    Else
        'Start the chrono
        If T_clock.Enabled = True Then
            xui.MsgboxAsync("Stop the chrono before starting a new one","oups!")
            Return
        Else
            'The chrono starts with the system time
            H_Start = DateTime.time(DateTime.Now)
            T_clock.Initialize("myTemps",1000)
            T_clock.Enabled = True
            
        End If
        
    End If

End Sub

Sub myTemps_Tick
    
    'H_Start = The time at which we started the chrono. This time is fixed (btStart_Click)
    'H_End = The time is current time. This time changes seconds after seconds
    H_End = DateTime.time(DateTime.Now)
    
    'Calculate and show the difference between the two times
    myDiff (H_Start, H_End)

End Sub

Sub myDiff (StartH As String, EndH As String)
    
    Dim PerDiff As Period
    Dim HS() As String
    Dim HE() As String
    
    'Split hh:mm:ss
    HS = Regex.Split("\:",StartH)
    HE = Regex.Split("\:",EndH)
    HS = Regex.Split("\:",StartH)
    HE = Regex.Split("\:",EndH)
    
    'Years, Months, Days, is not use
    Dim TimeA As Long = DateUtils.SetDateAndTime(2020, 10, 10, HS(0), HS(1), HS(2))
    Dim TimeB As Long = DateUtils.SetDateAndTime(2020, 10, 10, HE(0), HE(1), HE(2))
    
    PerDiff = DateUtils.PeriodBetween(TimeA, TimeB)
    
    'To have two digits
    Dim h, m, s As String
    h = PerDiff.Hours
    m = PerDiff.Minutes
    s = PerDiff.Seconds
    If h.Length = 1 Then
        h = "0"&PerDiff.Hours
    End If
    If m.Length = 1 Then
        m = "0"&PerDiff.Minutes
    End If
    If s.Length = 1 Then
        s = "0"&PerDiff.Seconds
    End If
    
    'Show the chrono
    lblDiff.Text = h & ":" & m & ":" & s

End Sub

Private Sub btPause_Click
    clk_BtPause = True 'Check if the pause button has been clicked
    T_clock.Enabled = False
End Sub

Private Sub btStop_Click
    T_clock.Enabled = False
End Sub

Private Sub btReset_Click
    If clk_BtPause = True Then
        xui.MsgboxAsync("The chrono is paused","oups!")
        Return
    Else
        If T_clock.Enabled = True Then
            xui.MsgboxAsync("Stop the chrono before reseting","oups!")
            Return
        Else
            T_clock.Enabled = False
            lblDiff.Text = "00:00:00"
        End If
    End If
    T_clock.Enabled = False
    lblDiff.Text = "00:00:00"
End Sub
 

Attachments

  • testChrono.zip
    19 KB · Views: 118

Erel

B4X founder
Staff member
Licensed User
Longtime User
Moved to the questions forum.

DateTime.Date / Time should never be used for anything other than to represent specific time points - March 3, 2014 12:34.
Be very careful with code that changes the timezone. In most cases it is a mistake.

Better implementation:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private lblDiff As B4XView
    Private StartTime As Long
    Private AccumulatedTime As Long
    Private Timer1 As Timer
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("layout")
    Timer1.Initialize("Timer1", 1000)
End Sub

Private Sub btStart_Click
    If Timer1.Enabled Then Return
    StartTime = DateTime.Now
    Timer1.Enabled = True
    Timer1_Tick
End Sub

Private Sub Timer1_Tick
    lblDiff.Text = ConvertMillisecondsToString(DateTime.Now - StartTime + AccumulatedTime)
End Sub

Private Sub btPause_Click
    AccumulatedTime = DateTime.Now - StartTime + AccumulatedTime
    Timer1.Enabled = False
End Sub

Private Sub btStop_Click
    Timer1.Enabled = False
    AccumulatedTime = 0
End Sub

Private Sub btReset_Click
    btStop_Click
    btStart_Click
End Sub

Private Sub ConvertMillisecondsToString(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 $"$1.0{hours}:$2.0{minutes}:$2.0{seconds}"$
End Sub

Edit: Fixed bug with pause implementation.
 

Attachments

  • Project.zip
    22.9 KB · Views: 126
Last edited:
Upvote 0

zed

Active Member
Licensed User
The part of code that changes the time zone is used in another application only to display the date and time. In this case, it is not necessary.
 
Upvote 0
Top