toit中的时区



我注意到当地时间有点奇怪。我的应用程序使用时间类打印日志:

time: = Time.now.local

如果使用运行应用程序以执行命令,控制台上会出现以下跟踪:

micrcx@micrcx-desktop:~/toit_apps/Hsm2/tests$ toit execute test_hsm_switch_async_4.toit 
18:31:53.532 exec [init INIT 0]
18:31:53.533 exec [switch Q_ENTRY 0]
18:31:53.535 exec [switch Q_INIT 0]
18:31:53.537 exec [off Q_ENTRY 0]
18:31:53.539 exec [off TURN 1]
18:31:53.540 exec [on Q_ENTRY 1]
18:31:53.543 exec [on TURN 2]
18:31:53.544 exec [off Q_ENTRY 2]
18:31:53.546 exec [off TURN 3]
18:31:53.548 exec [on Q_ENTRY 3]
18:31:53.549 exec [on RESET 4]
18:31:53.550 exec [switch Q_INIT 4]
18:31:53.552 exec [off Q_ENTRY 4]
^C
micrcx@micrcx-desktop:~/toit_apps/Hsm2/tests$

如果使用运行应用程序,则运行命令,控制台上会出现以下跟踪:

micrcx@micrcx-desktop:~/toit_apps/Hsm2/tests$ toit run test_hsm_switch_async_4.toit 
2021-04-24T18:34:12.677531Z: <process initiated>
20:34:12.536 exec [init INIT 0]
20:34:12.663 exec [switch Q_ENTRY 0]
20:34:12.705 exec [switch Q_INIT 0]
20:34:12.776 exec [off Q_ENTRY 0]
20:34:12.869 exec [off TURN 1]
20:34:13.055 exec [on Q_ENTRY 1]
20:34:13.160 exec [on TURN 2]
20:34:13.234 exec [off Q_ENTRY 2]
20:34:13.323 exec [off TURN 3]
20:34:13.397 exec [on Q_ENTRY 3]
20:34:13.569 exec [on RESET 4]
20:34:13.776 exec [switch Q_INIT 4]
20:34:13.826 exec [off Q_ENTRY 4]
^C
micrcx@micrcx-desktop:~/toit_apps/Hsm2/tests$ 

我的当地时间:

micrcx@micrcx-desktop:~/toit_apps/Hsm2/tests$ date
Sat 24 Apr 2021 21:37:07 IDT
micrcx@micrcx-desktop:~/toit_apps/Hsm2/tests$ 

因此,我的本地时间是~21:00,在ESP32-20:00上,在云上-<18:00>上。从20:00一切都或多或少地清楚了:这次是在哥本哈根,我的时间比它提前了一个小时。我不能说任何关于云的事情(真的是格陵兰岛吗?(,我只是陈述了差异的事实。我有一个简单的问题:有可能把时间带到我的当地时间吗?在这种情况下,到21:00。也许toit中有一些函数可以设置当前时区?类似于Time.setTimeZone";US/New_York">Time.setTimeZone";DK/哥本哈根">Time.setTimeZone";IL/耶路撒冷">Time.setTimeZone";英国/伦敦">&等

问候,MK

更新:在Toit v2(开源版本(中,set_tz_函数现在公开为set_timezone。以下示例将时区设置为太平洋时间。

main:
set_timezone "PST8PDT,M3.2.0,M11.1.0"

toit exec在Toit服务器上运行,而toit run在设备上运行。

很明显,两者的时区设置不同。设备的本地时间设置为CET/CEST,服务器上的本地时间(显然(设置为UTC。

理想情况下,应该有一种方法可以通过配置文件(或Toit控制台(在设备上设置时区。然而,这还不存在。

同时,还有一种方法可以在Toit程序中设置时区。core.time_impl中的set_tz_功能可以设置TZ变量:

/**
Stores the given $rules in the `TZ` environment variable and
calls `tzset`, thus activating it.
Valid TZ values can be easily obtained by looking at the last line of the
zoneinfo files on Linux machines:
```
tail -n1 /usr/share/zoneinfo/Europe/Copenhagen
```
*/
set_tz_ rules/string:
#primitive.core.set_tz

例如,对于以色列,可以写:

import core.time_impl show set_tz_
main:
set_tz_ "IST-2IDT,M3.4.4/26,M10.5.0"
print Time.now

请注意,此函数是私有的(它位于_impl.toit文件中,并且以_结尾(,因此不能保证保持稳定。然而,就目前而言,这是实现你想要的最好(也是唯一(的方式。在不久的将来也没有改变它的计划。

另请注意:以这种方式设置时区可能会泄露一点点内存。如果你只设置一次时区,但避免经常在两个时区之间交替,这不是问题。

最新更新