Forum Discussion

simonsafhalter's avatar
11 years ago

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

5 Replies

  • 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
  • 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