B4R Question Date-time math errors

Mostez

Well-Known Member
Licensed User
Longtime User
Hello,
I want to get minutes count from current date-time to validate user account expiry(year-month-day-hour-min) I get date time from DS1302 with no problems at all, but the problem is the minutes count is wrong, it seems like variable overflows! any ideas how to correct this?

Thanks

B4X:
Sub Process_Globals
   
    Private Years As Int
    Private Months As Byte
    Private DayOfMonth As Byte
    Private Hours As Byte
    Private Minutes As Byte
    Private Seconds As Byte
    Private DayOfWeek As Byte
    Private TimeTicks As ULong
    Private MinutesSerial As ULong

B4X:
Sub DS1320timer_Tick()
    'The current time must be defined in the sub. The global can not be used.
    Dim currenttime As DSTime
    currenttime = DS1302.CurrentTime

     Years = currenttime.years
     Months = currenttime.Months
     DayOfMonth = currenttime.DayOfMonth
     Hours = currenttime.Hours
     Minutes = currenttime.Minutes
     Seconds = currenttime.Seconds
     DayOfWeek = currenttime.DayOfWeek
     TimeTicks= TimeTicks + 1
    
     Dim Yr As ULong = (Years) * (360) * (24) * (60)
     Dim Mn As ULong = (Months) * (30)  * (24) * (60)
     Dim Dy As ULong = (DayOfMonth) * (24) * (60)
     Dim Hr As ULong = (Hours) * (60)
     MinutesSerial = Yr + Mn +  Dy + Hr + Minutes


Log ("Current Date Time:",Years,"\",Months,"\",DayOfMonth,"-",Hours,":",Minutes)    
Log ("Minutes Products:",Yr,"-",Mn,"-",Dy,"-",Hr,"-",Minutes,"-",MinutesSerial)

I got the following result:

Current Date Time:2016\10\17-22:3
Minutes Products:4294959104-4294940544-24480-1320-3-4294958155


Yr should be, Yr = 2016 * 360 * 24 * 60 = 1045094400 not 4294959104
Mn should be, Mn = 10 * 30 * 24 * 60 = 432000 not 4294940544
 

Mostez

Well-Known Member
Licensed User
Longtime User
I tried your example, all variables types should be the same

B4X:
Dim Yrs As Int = 2016
Dim Yr As ULong = (Yrs) * 360 * 24 * 60
Log(Yr) ' returned 4294959104(wrong)

B4X:
Dim Yrs As ULong = 2016
Dim Yr As ULong = (Yrs) * 360 * 24 * 60
Log(Yr) ' returned 1045094400(correct)

B4X:
Dim Yrs As Int = 2016
Dim Yr As ULong = (Yrs) * o360x24x60
Log(Yr) ' returned 1045094400 (correct)

however it worked also like this:

Thanks Erel

B4X:
     Dim o24x60 As ULong = 24 * 60
     Dim o30x24x60 As ULong = 30 * o24x60
     Dim o360x24x60 As ULong =  360 * o24x60
  
     Dim Yr As ULong = (Years) * o360x24x60
     Dim Mn As ULong = (Months) * o30x24x60
     Dim Dy As ULong = (DayOfMonth) * o24x60
     Dim Hr As ULong = (Hours) * (60)
     MinutesSerial = Yr + Mn +  Dy + Hr + Minutes

Current Date Time:2016\10\18-11:20
Minutes Products:1045094400-432000-25920-660-20-1045553000
Current Date Time:2016\10\18-11:20
Minutes Products:1045094400-432000-25920-660-20-1045553000
Current Date Time:2016\10\18-11:21
Minutes Products:1045094400-432000-25920-660-21-1045553001
Current Date Time:2016\10\18-11:21
Minutes Products:1045094400-432000-25920-660-21-1045553001
Current Date Time:2016\10\18-11:21
Minutes Products:1045094400-432000-25920-660-21-1045553001
Current Date Time:2016\10\18-11:21
Minutes Products:1045094400-432000-25920-660-21-1045553001
 
Last edited:
Upvote 0
Top