Android Question Problem subtracting times

pjetson

Member
Licensed User
Longtime User
I'm a brand new B4A user, and I'm trying to create a program that calculates the difference between two times. Unfortunately, most likely due to something I'm doing wrong, the difference is 10 hours too high.

I've simplified the code down to a single button on the screen that toggles between on and off, and when it toggles off, it displays the elapsed time. If I wait, say, 5 seconds, the time shows as 10:00:05 instead of 00:00:05

Can anyone please help me see what I've done wrong?

B4X:
#Region  Project Attributes
  #ApplicationLabel: Test Program
  #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
  Dim StartTime As Long     ' Date/Time that user clicked ON
  Dim StopTime As Long     ' Date/Time that user clicked OFF
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.
  Private btnStartStop As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
  Activity.LoadLayout("Main")
   
  DateTime.DateFormat = DateTime.DeviceDefaultDateFormat
   
  btnStartStop.TextColor = Colors.Black
  btnStartStop.Color = Colors.Green
  btnStartStop.Text = "ON"
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub btnStartStop_Click
  Dim x As String
  If btnStartStop.Text = "ON" Then
  StartTime = DateTime.Now
  x = "ON at " & DateTime.Date(StartTime) & " " & DateTime.Time(StartTime)
  ToastMessageShow(x, True)
  btnStartStop.TextColor = Colors.Black
  btnStartStop.Color = Colors.Red
  btnStartStop.Text = "OFF"
  Else
  StopTime = DateTime.Now
  x = "OFF at " & DateTime.Date(StopTime) & " " & DateTime.Time(StopTime) & CRLF & "Duration: " & DateTime.Time(StopTime - StartTime)
  ToastMessageShow(x, True)
  btnStartStop.TextColor = Colors.Black
  btnStartStop.Color = Colors.Green
  btnStartStop.Text = "ON"
  End If   
End Sub

Thanks, Peter
 

pjetson

Member
Licensed User
Longtime User
Hi, Manfred. Thanks for your reply. I'll try using the DateUtils library to get around the problem.

I just realised that my timezone is UTC+10, and the number of hours added is also 10. Coincidence? I just searched the forums again, and found this: http://www.b4x.com/android/forum/threads/periodbetween-string-and-datetime-now.42201/

I'll try setting DateTime.SetTimeZone to see if that fixes the problem too.

Thanks, Peter
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

pjetson

Member
Licensed User
Longtime User
Okay, I'll use p = DateUtils.PeriodBetweenInDays(StartTime,StopTime) to calculate the time difference. However, I need to display it like a time, so what's the absolute simplest way to turn the integer values p.Days, p.Hours, p.Minutes, and p.Seconds into something like d hh:mm:ss?

Using NumberFormat() would seem to give me something like (note that I haven't tried this code!):
B4X:
Dim x as String
x = NumberFormat(p.Minutes, 2, 0) & ":" & NumberFormat(p.Hours, 2, 0) & ":" & NumberFormat(p.Seconds, 2, 0)
If p.Days <> 0 Then
  x = p.Days & " " & x
End If

It doesn't seem very efficient to convert integers into doubles and then into strings - is there a better way?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…