Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get time and timezone

Hi,

Is it possible to get the current timezone and time in HH:mm in Brightscript?
I'm looking at the http://sdkdocs.roku.com/display/sdkdoc/ifDateTime but I can only get year, month, day, day of the week, epoch, but not timezone or hours and minutes.

EDIT: I wan't to correct myself saying that, I found a way to get hours and minutes but is not taking into consideration the DST (Daylight Saving Time) so it shows 1 hour off.
Is there a way to detect that we are in DST timezone?

Thank you
Simon
0 Kudos
5 REPLIES 5
RokuChris
Roku Employee
Roku Employee

Re: Get time and timezone

0 Kudos

Re: Get time and timezone

"RokuChris" wrote:
http://sdkdocs.roku.com/display/sdkdoc/ifDeviceInfo#ifDeviceInfo-GetTimeZoneasString


I tried the GetTimezone() method and it returns me "Europe/United Kingdom", therefore not telling me if it's Daylight Saving Time (DST) or not.
So for example if UK is in GMT that's fine, but when they turn to BST (DST) it still returns the same timezone.
Is there any way knowing if we are in DST?

Thank you
Simon
0 Kudos
EnTerr
Roku Guru

Re: Get time and timezone

Back to your original need - how to get the local hours? toLocalTime() can help:
BrightScript Debugger> dt = createobject("rodatetime")
BrightScript Debugger> ? dt.getHours()
19
BrightScript Debugger> dt.toLocalTime()
BrightScript Debugger> ? dt.getHours()
12
belltown
Roku Guru

Re: Get time and timezone

What EnTerr said is correct. If you're in England your time will be one hour off if you don't convert to local time (7 hours off if you're in Los Angeles, for example). Note this is not specifically because of the one hour daylight savings time difference, but because of the one hour GMT/Local Time difference in your case.

And if you're in Samoa, your Roku will always be off by 24 hours after calling ToLocalTime () because of a bug in the way Roku calculates local Samoa time.

But if you really want a function that tells you if your Roku is currently observing daylight savings time:


Function IsDSTNow () As Boolean

dstNow = False

tzList = {}
' diff - Local time minus GMT for the time zone
' dst - False if the time zone never observes DST
tzList ["US/Puerto Rico-Virgin Islands"] = {diff: -4, dst: False}
tzList ["US/Guam"] = {diff: -10, dst: False}
tzList ["US/Samoa"] = {diff: -11, dst: True} ' Should be 13 [Workaround Roku bug]
tzList ["US/Hawaii"] = {diff: -10, dst: False}
tzList ["US/Aleutian"] = {diff: -10, dst: True}
tzList ["US/Alaska"] = {diff: -9, dst: True}
tzList ["US/Pacific"] = {diff: -8, dst: True}
tzList ["US/Arizona"] = {diff: -7, dst: False}
tzList ["US/Mountain"] = {diff: -7, dst: True}
tzList ["US/Central"] = {diff: -6, dst: True}
tzList ["US/Eastern"] = {diff: -5, dst: True}
tzList ["Canada/Pacific"] = {diff: -8, dst: True}
tzList ["Canada/Mountain"] = {diff: -7, dst: True}
tzList ["Canada/Central Standard"] = {diff: -6, dst: False}
tzList ["Canada/Central"] = {diff: -6, dst: True}
tzList ["Canada/Eastern"] = {diff: -5, dst: True}
tzList ["Canada/Atlantic"] = {diff: -4, dst: True}
tzList ["Canada/Newfoundland"] = {diff: -3.5, dst: True}
tzList ["Europe/Iceland"] = {diff: 0, dst: False}
tzList ["Europe/Ireland"] = {diff: 0, dst: True}
tzList ["Europe/United Kingdom"] = {diff: 0, dst: True}
tzList ["Europe/Portugal"] = {diff: 0, dst: True}
tzList ["Europe/Central European Time"] = {diff: 1, dst: True}
tzList ["Europe/Greece/Finland"] = {diff: 2, dst: True}

' Get the Roku device's current time zone setting
tz = CreateObject ("roDeviceInfo").GetTimeZone ()

' Look up in our time zone list - will return Invalid if time zone not listed
tzEntry = tzList [tz]

' Return False if the current time zone does not ever observe DST, or if time zone was not found
If tzEntry <> Invalid And tzEntry.dst
' Get the current time in GMT
dt = CreateObject ("roDateTime")
secsGmt = dt.AsSeconds ()

' Convert the current time to local time
dt.ToLocalTime ()
secsLoc = dt.AsSeconds ()

' Calculate the difference in seconds between local time and GMT
secsDiff = secsLoc - secsGMT

' If the difference between local and GMT equals the difference in our table, then we're on standard time now
dstDiff = tzEntry.diff * 60 * 60 - secsDiff
If dstDiff < 0 Then dstDiff = -dstDiff

dstNow = dstDiff > 1 ' Use 1 sec not zero as Newfoundland time is a floating-point value
Endif

Return dstNow

End Function

Re: Get time and timezone

Great,

that's exactly what I need, I get it now. Thanks to both of you!

Simon
0 Kudos