B4J Question Minus integer by one each day based on time

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I have a silly and simple question but not sure on how to do it..

I have a database (SQLite database) and it has a 2 columns:
timestamp, NumberOfDays

When I add an item to this database I am adding the timestamp as the ticks (DateTime.Now)

The NumberOfDays holds a integer value. (example 100)

The above is fine..

The part I can't work out how to do is, at the timestamp time each day I want to minus 1 from the NumberOfDays. (forget about the date and only work on the time value)

So let's say the timestamp I added the value is 4:04PM then at 4:04PM each day I want to minus 1 from the NumberOfDays value just for that one record.

If I add another record and the timestamp for this next record was 6:30PM then only minus that value by 1 at 6:30PM each day without minus the other record.

There could over 100-200+ records I need to do this to, and the times could be all different.

Once the NumberOfDays value is 0, I then need it to trigger a sub (for testing make it log that the value is now 0)

Anyone got any ideas on how to do this ?
 

DonManfred

Expert
Licensed User
Longtime User
Get the stored timestamp and NumberOfDays from DB.
In a loop add 1 Day to the value.
for each day in the iteration minus 1 on NumberOfDays
Stop the iteration if the calculated timestamp value is bigger than Datetime.Now.
Store the calculated new value for NumberOfDays
 
Upvote 0

udg

Expert
Licensed User
Longtime User
There was an example by Erel (probabily about the StartAt or StartAtExact functions) where a list of wake up times was used to start the program.
In your case, a similar algorithm could be used to "wake up and decrease" on your 100-200 points on time.

Edit: it was FindNextTime
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Ended up making a small example.

Seems to work From what I can tell.

Would this be OK or is there a better way ?


B4X:
Sub Process_Globals
    Dim db As SQL
End Sub

Sub AppStart (Args() As String)

    ' Check and Create database for testing
    If File.Exists(File.DirApp,"/database.db") = False Then
        db.InitializeSQLite(File.DirApp, "/database.db", True)
        db.ExecNonQuery("CREATE TABLE items(timestamp TEXT, NumberOfDays TEXT)")
        db.ExecNonQuery("INSERT INTO `items`(`timestamp`,`NumberOfDays`) VALUES ('1547536832454','100')")
        db.ExecNonQuery("INSERT INTO `items`(`timestamp`,`NumberOfDays`) VALUES ('1547538059332','100')")
    Else
        db.InitializeSQLite(File.DirApp, "/database.db", False)
    End If
    
    'begin checking
    Check

    StartMessageLoop
End Sub

Sub Check
    
    Log("Checking...")
    
    Dim RS As ResultSet = db.ExecQuery("SELECT * FROM `items`")
    
    ' Loop though all records..
    Do While RS.NextRow
        If DateTime.GetHour(RS.GetString("timestamp")) = DateTime.GetHour(DateTime.Now) And DateTime.GetMinute(RS.GetString("timestamp")) = DateTime.GetMinute(DateTime.Now) Then
            Log("Process item..")
            ' hour and minute is the same (so it must be the next day)
            ' Update Database, minus 1 from NumberDays: RS.GetString("NumberOfDays")
        End If
    Loop
    RS.Close
    
    Log("Checking.. Done!")
    
    ' Run this sub again in 60 seconds (since minute would of changed)
    Sleep(60 * 1000)
    Check
    
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Get the stored timestamp and NumberOfDays from DB.
In a loop add 1 Day to the value.
for each day in the iteration minus 1 on NumberOfDays
Stop the iteration if the calculated timestamp value is bigger than Datetime.Now.
Store the calculated new value for NumberOfDays
B4X:
Dim oldDate As Long = DateTime.DateTimeParse("01/01/2019","08:00:00")
    Dim days As Int = 100
    Dim dummy As Long = oldDate
    Do While dummy < DateTime.Now
        dummy = DateTime.Add(dummy,0,0,1)
        days = days -1
    Loop
    Log($"Days left: ${days}"$)
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
That code will work better.
Now I can get the ticks from the database, and then set the 'days' value from the database and check how many days are left without having to write anything to the database.

Good job on that code.
 
Upvote 0
Top