可以在 rtc.datetime() 中更改元组吗?
Can the tuples be changed in rtc.datetime()?
import network, ntptime, time
from machine import RTC
# dictionary that maps string date names to indexes in the RTC's
datetime tuple
DATETIME_ELEMENTS = {
"year": 0,
"month": 1,
"day": 2,
"day_of_week": 3,
"hour": 4,
"minute": 5,
"second": 6,
"millisecond": 7
}
def connect_to_wifi(wlan, ssid, password):
if not wlan.isconnected():
print("Connecting to network...")
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
# set an element of the RTC's datetime to a different value
def set_datetime_element(rtc, datetime_element, value):
date = list(rtc.datetime())
date[DATETIME_ELEMENTS[datetime_element]] = value
rtc.datetime(date)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
connect_to_wifi(wlan, "SSID", "Password")
rtc = RTC()
ntptime.settime()
set_datetime_element(rtc, "hour", 8) # I call this to change the hour to 8am for me
print(rtc.datetime()) # print the updated RTC time
打印结果:
(2022, 4, 28, 3, 18, 50, 27, 0)
(2022, 4, 28, 3, 8, 50, 27, 0)
我正在尝试获取:
(2022, 4, 28, 8, 50, 27)
我不想要天或微秒。有什么建议吗?
如果你只想打印元组中字段的子集,你可以使用 Python 的切片操作(参见示例 here 到 select 只有那些字段:
>>> now=(2022, 4, 28, 3, 18, 50, 27, 0)
>>> print(now)
(2022, 4, 28, 3, 18, 50, 27, 0)
>>> print(now[:3] + now[5:7])
(2022, 4, 28, 50, 27)
import network, ntptime, time
from machine import RTC
# dictionary that maps string date names to indexes in the RTC's
datetime tuple
DATETIME_ELEMENTS = {
"year": 0,
"month": 1,
"day": 2,
"day_of_week": 3,
"hour": 4,
"minute": 5,
"second": 6,
"millisecond": 7
}
def connect_to_wifi(wlan, ssid, password):
if not wlan.isconnected():
print("Connecting to network...")
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
# set an element of the RTC's datetime to a different value
def set_datetime_element(rtc, datetime_element, value):
date = list(rtc.datetime())
date[DATETIME_ELEMENTS[datetime_element]] = value
rtc.datetime(date)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
connect_to_wifi(wlan, "SSID", "Password")
rtc = RTC()
ntptime.settime()
set_datetime_element(rtc, "hour", 8) # I call this to change the hour to 8am for me
print(rtc.datetime()) # print the updated RTC time
打印结果:
(2022, 4, 28, 3, 18, 50, 27, 0)
(2022, 4, 28, 3, 8, 50, 27, 0)
我正在尝试获取:
(2022, 4, 28, 8, 50, 27)
我不想要天或微秒。有什么建议吗?
如果你只想打印元组中字段的子集,你可以使用 Python 的切片操作(参见示例 here 到 select 只有那些字段:
>>> now=(2022, 4, 28, 3, 18, 50, 27, 0)
>>> print(now)
(2022, 4, 28, 3, 18, 50, 27, 0)
>>> print(now[:3] + now[5:7])
(2022, 4, 28, 50, 27)