ToLocalTime() 在 Roku 中的使用

ToLocalTime() use in Roku

我在 Roku 中使用“roDateTime”来打印日期和时间。但它显示的是 UTC 时间。有什么方法可以将 UTC 转换为本地时间。我尝试了以下方式,但它没有显示任何显示 "UNINITIALIZED"。该文件称多次调用将对产生错误结果的时间进行多次时区调整。我只在 Roku Device 中打一个电话。

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

currentTime.Mark()

? "current time : " currentTime.ToLocalTime()

我找到了另一种方法:创建一个包含所有时区的数组 GetTimeZone() 可以 return,然后处理您使用 dt = CreateObject ("roDateTime").[=13 获得的当前时间=]

这是您可以在代码中使用的完整函数:

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

toLocalTime() 是一个 Void 函数,因此它没有 return 任何值,这就是为什么你得到 "UNINITIALIZED"在控制台中。

如果您只想打印当地时间的日期和时间,您可以使用下面这个简单的代码片段

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