How to schedule a service for a different day

blablabla

Member
Licensed User
Longtime User
Hi everybody!
Houston we've got a problem! :sign0089:
Is there a way to schedule a service for a different day? For example tomorrow?

I use
B4X:
StartServiceAt(MyService,DateTime.TimeParse(Hour & ":" & Minute & ":00"),True)
it works fine, but when I try to schedule for tomorrow it doesn't work and start the service at the correct time but today.
I tried to use
B4X:
DateTime.Add(DateTime.TimeParse(Hour & ":" & Minute & ":00"),0,0,(Data-DateTime.GetDayOfMonth(DateTime.Now))
instead of
B4X:
DateTime.TimeParse(Hour & ":" & Minute & ":00")

Any ideas? Thanks to everybody! :D
 

rfresh

Well-Known Member
Licensed User
Longtime User
I too have a need to start a service at a specific time or on a specific min of every hour. We really need a way to simplify Service starting times. Would this best be done with a Function or a Lib?
 
Upvote 0

blablabla

Member
Licensed User
Longtime User
I found the problem is not in scheduling services but in datetime.
The issue is caused by the difference in time zones.
Specifically, I live in Italy and now we've 2 hours offset (1+1DST).
In fact if i try to schedule the service between 0:00 and 2:00 AM it doesn't work.
It seems that
B4X:
DateTime.TimeParse(Hour & ":" & Minute & ":00")
gives back a tick meaning it's "yesterday", so even if I add 1 day it comes to today.

I also did some test changing the time on my phone and this code:
B4X:
Hour = 1
Minute = 0

Msgbox("Time = " & DateTime.Time(DateTime.TimeParse(Hour & ":" & Minute & ":00")) & "Time parse = " & DateTime.TimeParse(Hour & ":" & Minute & ":00"),"") '& "data = " & DateTime.Date(zz),True)

give me different result if time on my phone is before or after 2:00 AM... "Time = 01:00" is always correct, but the tick coming from TimeParse is different.

I also tried setting
B4X:
DateTime.SetTimeZone(DateTime.GetTimeZoneOffsetAt(DateTime.Now))
but nothing changed.:sign0148:

I can't believe that this can't be fixed so i hope I'm really doing something wrong...

Please help me :sign0085:
 
Last edited:
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
My understanding is that the time used is what is on your phone.

I download files on my website for example, and they are time stamped with a 3 hour difference from my phone time because my phone is located in a different time zone than where my website is hosted.

So, I don't follow your logic regarding a time zone problem. You're running your app on your phone and it's using what ever time zone your carrier has you setup for, right? Or am I misunderstanding completely?
 
Upvote 0

blablabla

Member
Licensed User
Longtime User
Example:
On my phone I schedule a service for tomorrow at 07:00 AM
if I do it at 6 AM or 5 PM (for example) it works.
If I do it at 1 AM (between 0:00 and 2:00) it doesn't work.
It seems that the "zero" of time tick happen at 2 AM at my Locale not at 0:00, or something like this
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I had similar issues, I think it happens due to too many conversions from/to strings.
Try to keep the time in 'Long' and only convert once in order to display it.

Lets say you have Hour and Minute.
Store the time as Long:

B4X:
Dim tt as Long
tt = hours * DateTime.TicksPerHour + minutes * DateTime.TicksPerMinute 

Dim NowDay As Long 
NowDay = DateTime.DateParse(DateTime.Date(DateTime.Now))

Dim CurrentTime As Long 
CurrentTime = DateTime.Now - NowDay

If tt > CurrentTime Then
   'Time has not passed, schedule for today
   NextRunTime = NowDay + tt
Else 'Schedule for tomorrow
   NextRunTime = NowDay + DateTime.TicksPerDay + tt
End If

Log("NextRunTime:" & NextRunTime & " Date:" & DateTime.Date(NextRunTime) &  "Time:" & DateTime.Time(NextRunTime))

StartServiceAt("",NextRunTime,True)
 
Upvote 0
Top