def get_datetime(self): if not self.check_sanity(): # if clock are insane: try to reset it self.reset_clock() # if clock are still insane: return None if not self.check_sanity(): return None year, month, date = ds1302.get_date() hour, minute, second = ds1302.get_time() ''' if self.rangechecks: if year < 2000 or year > 3000: ds1302.set_date(2000, month, date) return self.get_datetime() if month not in range(1, 13): ds1302.set_date(year, 1, date) return self.get_datetime() if date not in range(1, 32): ds1302.set_date(year, month, 1) return self.get_datetime() if hour not in range(0, 24): ds1302.set_time(0, minute, second) return self.get_datetime() if minute not in range(0, 60): ds1302.set_time(hour, 0, second) return self.get_datetime() if second not in range(0, 60): ds1302.set_time(hour, minute, 0) return self.get_datetime() ''' return datetime(year, month, date, hour, minute, second)
def check_sanity(self): "check sanity of a clock. returns True if clock is sane and False otherwise" year, month, day = ds1302.get_date() hours, mins, secs = ds1302.get_time() if year == 2000 or month == 0 or day == 0: return False if secs == 80: return False return True
def check_sanity(self): "check sanity of a clock. returns True if clock is sane and False otherwise" year, month, date = ds1302.get_date() hours, mins, secs = ds1302.get_time() if year == 2000 or month == 0 or date == 0: return False if secs == 80: return False return True
def get_datetime(self): if not self.check_sanity(): # if clock are insane: try to reset it self.reset_clock() # if clock are still insane: return None if not self.check_sanity(): return None year, month, day = ds1302.get_date() hour, minute, second = ds1302.get_time() return datetime(year, month, day, hour, minute, second)
#VERIFICANDO DATA DO SISTEMA f = open('rtc.log', 'a') sys.stdout = f print "> Sistema iniciado" o = commands.getoutput("date -Ins ") y, m, d, h, mi, s = o[0:4], o[5:7], o[8:10], o[11:13], o[14:16], o[17:19] ds1302.init_clock() for i in range(10): year, month, day = ds1302.get_date() hour, minute, second = ds1302.get_time() if (month > 12): month = 0 if (day > 31): day = 0 if (hour > 24): hour = 0 if (minute > 60): minute = 0 if (second > 60): second = 0 if (int(mi) < minute or int(h) < hour or int(d) < day or int(m) < month): print ">>> Atualizando data" os.system("date -s " + str(year) + "/" + str(month) + "/" + str(day)) os.system("date -s " + str(hour) + ":" + str(minute) + ":" + str(second)) o = commands.getoutput("date -Ins ") y, m, d, h, mi, s = o[0:4], o[5:7], o[8:10], o[11:13], o[14:16], o[ 17:19] break