[英]ToLocalTime() use in Roku
我在 Roku 中使用了“ roDateTime ”来打印日期和时间。 但它显示的是UTC时间。 有什么方法可以将UTC转换为本地时间。 我尝试了以下方式但它不显示任何内容它显示“UNINITIALIZED” 。 文档说多次调用将对时间进行多次时区调整,从而产生不正确的结果。 我只在 Roku 设备中打过一次电话。
currentTime = CreateObject("roDateTime") ' roDateTime is initialized
currentTime.Mark()
? "current time : " currentTime.ToLocalTime()
toLocalTime()是一个Void
函数,因此它不返回任何值,这就是您在控制台中得到“UNINITIALIZED”的原因。
如果您只想打印当地时间的日期和时间,您可以使用下面的这个简单片段
datetime = CreateObject("roDateTime")
datetime.toLocalTime()
? "current time: "datetime.toISOString()
我找到了另一种方法:创建一个包含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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.