Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
scaper12123
Level 7

How to manage time zones?

I have a bit of a delema as I try to program this bit of code. My church uses a schedule for all their livestreamed services, and I need to be able to use this schedule to decide whether to attempt showing a user video footage or to show them a screen that tells them that there is no live stream at the moment. The idea is, of course, that I compare their device's date and time to that of the scheduel. If they are the same or if the device's date and time are between start and end time, the console sends the livestream. The problem is the timing information for said schedule is in EST when Brightscript programming always operates in UCT.

..."Event_Start_Date":"2016-07-02T18:00:00","Event_End_Date":"2016-07-02T19:30:00",...


both of these ISO strings are in EST, and I do not have the means to change them to UCT for convenience. I can't seem to find a simple way to change the ISO time string to the proper time zone when looking through the roDateTime documentation. Does anybody have any simple suggestions on how to do this, or am I going to have to brute-force my way through this issue? (i.e. manually change the string to reflect UCT time)
I AM THE ARCHMAGE... who is also rather new to Brightscript so forgive me for seeming inept at times.
0 Kudos
2 Replies
belltown
Level 7

Re: How to manage time zones?

Assuming the times really are in EST and not ET, this should work:


Sub Main ()
dt = timeConvert ("2016-07-02T18:00:00")
printTime (dt)
Stop
End Sub

Function timeConvert (estString As String) As Object

' An roDateTime by default gives current time in UTC
dt = CreateObject ("roDateTime")

' Set the time from the EST string input
dt.FromISO8601String (estString)

' Difference in seconds UTC - EST
estOffsetSecs = 5 * 60 * 60

' Convert EST seconds to UTC seconds
utcSecs = dt.AsSeconds () + estOffsetSecs

' Make an roDateTime from UTC seconds
dt.FromSeconds (utcSecs)

' Convert to local time
dt.ToLocalTime ()

' Return the local time representation of the EST input
Return dt

End Function

' Quick 'n dirty time formatter
Function printTime (dt As Object) As Void
tz = CreateObject ("roDeviceInfo").GetTimeZone ()
isoString = dt.ToISOString ()
isoString = Mid (isoString, 1, 10) + " " + Mid (isoString, 12, 8)
Print isoString; " "; tz
End Function
https://github.com/belltown/
0 Kudos
scaper12123
Level 7

Re: How to manage time zones?

"belltown" wrote:
Assuming the times really are in EST and not ET, this should work:


Sub Main ()
dt = timeConvert ("2016-07-02T18:00:00")
printTime (dt)
Stop
End Sub

Function timeConvert (estString As String) As Object

' An roDateTime by default gives current time in UTC
dt = CreateObject ("roDateTime")

' Set the time from the EST string input
dt.FromISO8601String (estString)

' Difference in seconds UTC - EST
estOffsetSecs = 5 * 60 * 60

' Convert EST seconds to UTC seconds
utcSecs = dt.AsSeconds () + estOffsetSecs

' Make an roDateTime from UTC seconds
dt.FromSeconds (utcSecs)

' Convert to local time
dt.ToLocalTime ()

' Return the local time representation of the EST input
Return dt

End Function

' Quick 'n dirty time formatter
Function printTime (dt As Object) As Void
tz = CreateObject ("roDeviceInfo").GetTimeZone ()
isoString = dt.ToISOString ()
isoString = Mid (isoString, 1, 10) + " " + Mid (isoString, 12, 8)
Print isoString; " "; tz
End Function


Hey, thanks for your help with this! I definately am getting the information I need now, but the only thing left for me to do is to compare the start time with the current local time and see what the difference is (i.e. if the stream should even attempt to show). Is there a simple way to do that or do I just have to find a way to do it from scratch?
I AM THE ARCHMAGE... who is also rather new to Brightscript so forgive me for seeming inept at times.
0 Kudos