Python源码示例:utime.localtime()
示例1
def _do_time(self, action): # TIME received from ESP8266
lnk = self._lnk
try:
t = int(action[0])
except ValueError: # Gibberish.
lnk.quit('Invalid data from ESP8266')
return
self._time_valid = t > 0
if self._time_valid:
rtc = pyb.RTC()
tm = localtime(t)
hours = (tm[3] + self._local_time_offset) % 24
tm = tm[0:3] + (tm[6] + 1,) + (hours,) + tm[4:6] + (0,)
rtc.datetime(tm)
self._rtc_last_syn = time()
lnk.vbprint('time', localtime())
else:
lnk.vbprint('Bad time received.')
# Is Pyboard RTC synchronised?
示例2
def perform(self) :
localtime = self.get_localtime()
secs = Scheduler.secs_since_midnight(localtime)
(_year, _month, _mday, _hour, _minute, _second, wday, _yday) = localtime
seq = self.get_current_seq(secs, wday + 1)
if seq :
if seq != self.current_seq :
color_name = seq['color_name']
logging.info("Scheduler: Setting lamp color to {}", color_name)
self.lamp.set_colorspec(self.color_specs[color_name])
self.current_seq = seq
else :
self.lamp.set_colorspec(self.color_specs['black'])
self.current_seq = None
return True
##
## Internal operations
##
示例3
def aclock():
uv = lambda phi : cmath.rect(1, phi) # Return a unit vector of phase phi
pi = cmath.pi
days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
'Sunday')
months = ('Jan', 'Feb', 'March', 'April', 'May', 'June', 'July',
'Aug', 'Sept', 'Oct', 'Nov', 'Dec')
# Instantiate CWriter
CWriter.set_textpos(ssd, 0, 0) # In case previous tests have altered it
wri = CWriter(ssd, arial10, GREEN, BLACK, verbose=False)
wri.set_clip(True, True, False)
# Instantiate displayable objects
dial = Dial(wri, 2, 2, height = 75, ticks = 12, bdcolor=None, label=120, pip=False) # Border in fg color
lbltim = Label(wri, 5, 85, 35)
hrs = Pointer(dial)
mins = Pointer(dial)
secs = Pointer(dial)
hstart = 0 + 0.7j # Pointer lengths and position at top
mstart = 0 + 0.92j
sstart = 0 + 0.92j
while True:
t = utime.localtime()
hrs.value(hstart * uv(-t[3]*pi/6 - t[4]*pi/360), YELLOW)
mins.value(mstart * uv(-t[4] * pi/30), YELLOW)
secs.value(sstart * uv(-t[5] * pi/30), RED)
lbltim.value('{:02d}.{:02d}.{:02d}'.format(t[3], t[4], t[5]))
dial.text('{} {} {} {}'.format(days[t[6]], t[2], months[t[1] - 1], t[0]))
refresh(ssd)
utime.sleep(1)
示例4
def printtime():
print('{:02d}:{:02d}:{:02d} '.format(localtime()[3], localtime()[4], localtime()[5]), end='')
示例5
def time():
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1b
addr = socket.getaddrinfo(host, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(1)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
s.close()
val = struct.unpack("!I", msg[40:44])[0]
return val - NTP_DELTA
# There's currently no timezone support in MicroPython, so
# utime.localtime() will return UTC time (as if it was .gmtime())
示例6
def settime():
t = time()
import machine
import utime
tm = utime.localtime(t)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
print(utime.localtime())
示例7
def time():
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1b
addr = socket.getaddrinfo(host, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(1)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
s.close()
val = struct.unpack("!I", msg[40:44])[0]
return val - NTP_DELTA
# There's currently no timezone support in MicroPython, so
# utime.localtime() will return UTC time (as if it was .gmtime())
示例8
def settime():
t = time()
import machine
import utime
tm = utime.localtime(t)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
print(utime.localtime())
示例9
def time():
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1b
addr = socket.getaddrinfo(host, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(1)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
s.close()
val = struct.unpack("!I", msg[40:44])[0]
return val - NTP_DELTA
# There's currently no timezone support in MicroPython, so
# utime.localtime() will return UTC time (as if it was .gmtime())
示例10
def time():
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1b
addr = socket.getaddrinfo(host, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(1)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
s.close()
val = struct.unpack("!I", msg[40:44])[0]
return val - NTP_DELTA
# There's currently no timezone support in MicroPython, so
# utime.localtime() will return UTC time (as if it was .gmtime())
示例11
def settime():
t = time()
import machine
import utime
tm = utime.localtime(t)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
print(utime.localtime())
示例12
def convert(self, set_rtc=False): # Return a tuple in localtime() format (less yday)
data = self.timebuf
ss = bcd2dec(data[0])
mm = bcd2dec(data[1])
if data[2] & 0x40:
hh = bcd2dec(data[2] & 0x1f)
if data[2] & 0x20:
hh += 12
else:
hh = bcd2dec(data[2])
wday = data[3]
DD = bcd2dec(data[4])
MM = bcd2dec(data[5] & 0x1f)
YY = bcd2dec(data[6])
if data[5] & 0x80:
YY += 2000
else:
YY += 1900
# Time from DS3231 in time.localtime() format (less yday)
result = YY, MM, DD, hh, mm, ss, wday -1, 0
if set_rtc:
if rtc is None:
# Best we can do is to set local time
secs = utime.mktime(result)
utime.localtime(secs)
else:
rtc.datetime((YY, MM, DD, wday, hh, mm, ss, 0))
return result
示例13
def save_time(self):
(YY, MM, mday, hh, mm, ss, wday, yday) = utime.localtime() # Based on RTC
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 0, tobytes(dec2bcd(ss)))
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 1, tobytes(dec2bcd(mm)))
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 2, tobytes(dec2bcd(hh))) # Sets to 24hr mode
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 3, tobytes(dec2bcd(wday + 1))) # 1 == Monday, 7 == Sunday
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 4, tobytes(dec2bcd(mday))) # Day of month
if YY >= 2000:
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 5, tobytes(dec2bcd(MM) | 0b10000000)) # Century bit
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 6, tobytes(dec2bcd(YY-2000)))
else:
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 5, tobytes(dec2bcd(MM)))
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 6, tobytes(dec2bcd(YY-1900)))
# Wait until DS3231 seconds value changes before reading and returning data
示例14
def save_time(self):
(YY, MM, mday, hh, mm, ss, wday, yday) = utime.localtime() # Based on RTC
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 0, tobytes(dec2bcd(ss)))
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 1, tobytes(dec2bcd(mm)))
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 2, tobytes(dec2bcd(hh))) # Sets to 24hr mode
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 3, tobytes(dec2bcd(wday + 1))) # 1 == Monday, 7 == Sunday
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 4, tobytes(dec2bcd(mday))) # Day of month
if YY >= 2000:
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 5, tobytes(dec2bcd(MM) | 0b10000000)) # Century bit
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 6, tobytes(dec2bcd(YY-2000)))
else:
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 5, tobytes(dec2bcd(MM)))
self.ds3231.writeto_mem(DS3231_I2C_ADDR, 6, tobytes(dec2bcd(YY-1900)))
示例15
def set_time():
ntptime.settime()
tm = utime.localtime()
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
print('current time: {}'.format(utime.localtime()))
示例16
def set_datetime(secs) :
import utime
import machine
tm = utime.localtime(secs)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
示例17
def get_datetime() :
import time
year, month, day, hour, minute, second, millis, _tzinfo = time.localtime()
return "%d-%02d-%02dT%02d:%02d:%02d.%03d" % (year, month, day, hour, minute, second, millis)
示例18
def datetime_str(localtime) :
(year, month, day, hour, minute, second, millis, _tzinfo) = localtime
return "%d-%02d-%02dT%02d:%02d:%02d.%03d" % (year, month, day, hour, minute, second, millis)
示例19
def get_localtime(raw=False):
import utime
localtime = utime.localtime()
if raw:
return utime.mktime(localtime)
else:
return DateTimeCmd.datetime_str(localtime)
示例20
def get_datetime_from_secs(secs):
import utime
tm = utime.localtime(secs)
return DateTimeCmd.datetime_str(tm)
示例21
def get_localtime(self) :
secs = utime.time()
secs += self.tzd.get_offset_hours() * 60 * 60
return utime.localtime(secs)
示例22
def secs_since_midnight(localtime) :
secs = utime.mktime(localtime)
return secs - Scheduler.midnight_epoch_secs(localtime)
示例23
def midnight_epoch_secs(localtime) :
(year, month, mday, hour, minute, second, wday, yday) = localtime
return utime.mktime((year, month, mday, 0, 0, 0, wday, yday))
示例24
def date(secs=False):
localtime = utime.localtime()
if secs:
print(utime.mktime(localtime))
else:
print(core.util.localtime_to_string(localtime))
示例25
def secs_to_string(secs=None):
import core
import utime
localtime = utime.localtime(secs)
return localtime_to_string(localtime)
示例26
def shutoff(self):
""" shut off the MCU """
import DS3231tokei
import utime
from machine import Pin, RTC
bus = self.sensor_manager.get_bus_by_sensortype('DS3231')
ds = DS3231tokei.DS3231(bus.adapter)
interval = self.settings.get('main.interval.shutoff', 10) * 60 # convert from minutes to seconds
(year,month,day,dotw,hour,minute,second) = ds.getDateTime() # get the current time
rtc = RTC() # create RTC
if year < 2001:
year = 2001 # sanity check, as of mpy 1.12 year must be >= 2001
rtc.init((year,month,day,dotw,hour,minute,second,0)) # set time
# check if its night or winter and adjust interval
night_start = self.settings.get('main.interval.night_start', 0)
night_end = self.settings.get('main.interval.night_end', 0)
winter_start = self.settings.get('main.interval.winter_start', 0)
winter_end = self.settings.get('main.interval.winter_end', 0)
if night_start > 0 and (hour >= night_start or hour <= night_end): # double interval for the night
interval *= 2
if winter_start > 0 and (month >= winter_start or month <= winter_end): # double interval for winter
interval *= 2
# Compute sleeping duration from measurement interval and elapsed time.
elapsed = int(self.duty_chrono.read())
now_secs = utime.mktime(utime.localtime())
wake_at = now_secs - elapsed + interval
if (wake_at - now_secs) < 180: # don't shutoff for less than 3 minutes
wake_at += interval
(year,month,day,hour,minute,second, dotw, doty) = utime.localtime(wake_at) # convert the wake up time
# set alarm
ds.setAlarm2(day,hour,minute, DS3231tokei.A2_ON_HOUR_MINUTE)
# turn off MCU via MOSFET
utime.sleep(1)
ds.enableAlarm2()
ds.resetAlarm2()
# The End