图伊特的时区

Time zones in toit

我注意到当地时间有些奇怪。我的应用程序使用时间 class:

打印日志
time: = Time.now.local

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

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$

如果运行 app用toit 运行 ...命令,下面的trace出现在控制台:

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/Copenhagen", Time.setTimeZone "IL/Jerusalem", Time.setTimeZone "UK/London" 等...

问候,MK

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 文件中,并以 _ 结尾),因此不能保证保持稳定。然而,就目前而言,这是实现您想要的目标的最佳(也是唯一)方式。近期也没有改的打算。

另请注意:以这种方式设置时区可能会泄漏一点点内存。如果您只设置一次时区,这不是问题,但要避免在两个时区之间切换太频繁。