在Roku中使用ToLocalTime()

时间:2019-11-28 13:18:21

标签: roku brightscript

我在Roku中使用了“ roDateTime”来打印日期和时间。但它显示的是UTC时间。有什么办法可以将UTC转换为本地时间。我以下面的方式尝试过,但是它不显示任何内容它显示“未初始化” 。该文档说,多次调用将对时间进行多次时区调整,从而产生错误的结果。我只在Roku设备中打过一个电话。

currentTime =  CreateObject("roDateTime") ' roDateTime is initialized

currentTime.Mark()

? "current time : " currentTime.ToLocalTime()

2 个答案:

答案 0 :(得分:2)

toLocalTime()Void函数,因此它不返回任何值,这就是为什么您在控制台中获得“ UNINITIALIZED” 的原因。

如果您只想以当地时间打印日期和时间,则可以在下面使用此简单代码段

datetime = CreateObject("roDateTime")
datetime.toLocalTime()
? "current time: "datetime.toISOString()

答案 1 :(得分:0)

我找到了另一种方法:创建一个可以返回所有时区GetTimeZone()的数组,然后处理通过dt = CreateObject ("roDateTime")获得的当前时间。

这是您可以在代码中使用的全部功能:

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
    else

       currentTime =  CreateObject("roDateTime") ' roDateTime is initialized

       ?"Here Display UTC Time Hours : " currentTime.GetHours()

      currentTime.ToLocalTime()

       ? "Here Display Local Time Hours : " currentTime.GetHours()

    Endif

    Return dstNow

End Function