Android Question StartServiceAt UTC or Local Ticks

canalrun

Well-Known Member
Licensed User
Longtime User
I am going to be using StartServiceAt() to run a service at dawn then again at dusk. There is a web site with a JSON interface that gives me the dawn/dusk times as text strings in UTC.

I would like to use these times with StartServiceAt().

Do I need to convert the text strings to local time before I convert them to ticks in order to pass as the time parameter? Or should I leave them in UTC format (I am in time zone UTC -5)?

Thanks,
Barry.
 

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks. As I was searching the forum I came upon another post where you suggested setting the DateTime.SetTimeZone(0) before converting the time string to ticks using the DateTime.TimeParse function.

For example, the string I get will look like "11:13:00 AM". This is in UTC. It should correspond to 6:13:00 AM for sunrise at my location (I'm UTC -5).

I believe I should do the following:
B4X:
DateTime.SetTimeZone(0)
DateTime.TimeFormat = "hh:mm:ss a"
Dim ticks0 As Long = DateTime.TimeParse("11:13:00 AM") ' format of example time returned from JSON get sunrise HTTP

Then restore my default time zone and parse format.
Use the ticks0 value I got in my StartServiceAt call.

I'm hoping this should start the service at 6:13 AM my time so I can turn off my bulb.

Barry.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Another option:
B4X:
Sub ParseUTCTime (time As String) As Long
    Dim m As Matcher = Regex.Matcher("(\d+):(\d+):(\d+)\s(\w+)", time)
    If m.Find = False Then Return 0
    Dim hours As Int = m.Group(1) Mod 12
    Dim minutes As Int = m.Group(2)
    Dim seconds As Int = m.Group(3)
    Dim am As Boolean = m.Group(4).ToLowerCase = "am"
    If am = False Then hours = hours + 12
    Return DateUtils.SetDateAndTime2(DateTime.GetYear(DateTime.Now), DateTime.GetMonth(DateTime.Now), DateTime.GetDayOfMonth(DateTime.Now), _
        hours, minutes, seconds, 0)
End Sub
 
Upvote 0
Top