Android Question Wrong result when adding TicksPerHour?

sconlon

Active Member
Licensed User
Longtime User
I want to find out the value in ticks of a time after midnight with something like the following code:

Code: Copy
midNight = DateTime.DateParse(DateTime.Date(DateTime.now))
hoursInTicks = hour*datetime.TicksPerHour                            ' hour is integer value after midnight, eg 4
finalTime = midnight + hoursInTicks
timeString  =  DateTime.Time(finalTime) & " on " & DateTime.date(finalTime)

When hour = 4 above the timestring = "03:00 on 27/10/2013" where it should be "04:00 on 27/10/2013".

To try to explain this I ran this code

Code: Copy
dim midnight, ltime as long
midNight = DateTime.DateParse(DateTime.Date(DateTime.now))
for i = 1 to 5
    ltime = midnight + datetime.TicksPerHour * i
    log("Time is " & DateTime.Time(ltime) & " on " & DateTime.date(ltime)
next

and the log I get is:

Time is 01:00 on 27/10/2013
Time is 01:00 on 27/10/2013
Time is 02:00 on 27/10/2013
Time is 03:00 on 27/10/2013
Time is 04:00 on 27/10/2013

So adding 2 hours in TicksPerHour to midnight still gives 01:00 which surely is not right?

Ta.
 
dst?
 
Upvote 0
I have also been getting strange results for my reminders, which seem to notify at the wrong time. If all Date/Times are "long" variables, does the arithmetics still not work?

Here is my situation: Stored in a SQL database are Tasks with a Start Date (Long) and a Reminder field (Long), which is simply no of ticks before the Start Date that the reminder must send a notification. Below is the code for iterating through all records in the database to update when the next reminder must notify (lnNextReminder) and which record it is (strNextReminder):

Code: Copy
If cur.RowCount > 0 Then
    For i = 0 To cur.RowCount - 1
        cur.Position = i
        strID = cur.GetString("taskid")
        lnStart = cur.GetLong("start")
        lnReminder = cur.GetLong("reminder")
        If (lnStart - lnReminder) < lnNextReminder Then
            lnNextReminder = (lnStart - lnReminder)
            strNextReminderID = strID
        End If
    Next
End If

Does the Date/Time arithmetics "does not work" apply in this case?
 
Upvote 0
All the variables must be long or the results will be "random".

Does the Date/Time arithmetics "does not work" apply in this case?
If you already have two time instances stored as ticks then you can substract the two values to find the correct number of ticks between them.

Date / time arithmetics stop working when you want the time instances to apply to a calendar. The main problem is dealing with DST changes and other anomalies.

If you want to schedule a notification on a daily basis then the code I posted here will help you: http://www.b4x.com/android/forum/threads/startserviceat-headaches.33646/#post-196815
 
Upvote 0
Back
Top