python - Micropython获取正确的当前时间
问题描述
因为 micropython 不导入日期时间。
我想使用 time 或 utime 模块来获取当前时间。
但是 time.localtime() 结果就像(2000, 1, 1, 0, 12, 35, 5, 1)
我想时间从2000/1/1
.
如何设置开始时间?
或者其他推荐的方式可以得到正确的结果?
谢谢!
解决方案
您可以使用库通过 NTP 协议通过 Internet 设置时间。您必须连接到互联网,例如 esp32 上的 wifi
import ntptime
import time
#if needed, overwrite default time server
ntptime.host = "1.europe.pool.ntp.org"
try:
print("Local time before synchronization:%s" %str(time.localtime()))
#make sure to have internet connection
ntptime.settime()
print("Local time after synchronization:%s" %str(time.localtime()))
except:
print("Error syncing time")
推荐阅读
- c# - WPF TextBlock 从字符串属性格式化粗体
- excel - 将表格从网站拉入 Excel VBA
- jenkins - 如何将参数值传递给 Jenkins 管道中的阶段构建?
- java - 如何从springboot调用用户定义的sql函数?
- c# - 无法读取超出流的末尾?
- javascript - 使用 JavaScript/XLS 函数在 XML 中包含命名空间
- kotlin - 当应用程序未运行时,广播接收器不会启动 Activity
- python - 使用 contains 合并数据框
- ignite - 在 Apache Ignite 上列出 SQL 查询
- azure-active-directory - Azure Active Directory ServicePrincipal SecurityGroup AppRoleAssignment 无法解析 appRole