Hej
Staram się połączyć oba kody
init python:
class clock(object):
def __init__(self, century = 19, year = 1999, month = 1, day = 1, hour = 8, minute = 0):
self._century = century
self._year = year
self._month = month
self._day = day
self._hour = hour
self._minute = minute
def addtime(self, minutes, hours = 0, days = 0, months = 0, years = 0, centuries = 0):
self._minute += minutes # add minutes
self._hour += hours # add hours
self._day += days
self._month += months
self._year += years
self._century += centuries
# add hours by floor division minute over 60
self._hour += self._minute // 60
# add days by floor division hour over 24 - make sure to do this after hour's been added
self._day += self._hour // 24
self._month += self._day // 31 # Q Ok how to ?
self._year += self._month // 12 # Q 372 days
self._century += self._year // 100
# now we can clean up minutes and hours by using mod %
self._minute = self._minute % 60
self._hour = self._hour % 24
self._day = self._day % 31
self._month = self._month % 12
self._year = self._year % 372 # Q how to 365? and 366?
# use property to return formatted hour and minutes
@property
def century(self): # day as int
return self._century
@property
def year(self): # day as int
return self._year
@property
def yy(self): # minutes as mm.
year = "0" + str(self._year)
return year[-2:]
@property
def month(self): # day as int
return self._month
@property
def mn(self): # minutes as mm.
month = "0" + str(self._month)
return month[-2:]
@property
def day(self): # day as int
return self._day
@property
def dd(self): # minutes as mm.
day = "0" + str(self._day)
return day[-2:]
@property
def hour(self): # hour as int
return self._hour
@property
def hh(self): # hours as HH. I'm not familiar enough with python to know if there's a shorter way to do this
hour = "0" + str(self._hour)
return hour[-2:] # Q what is doing this 2?
@property
def minute(self): # minute as int
return self._minute
@property
def mm(self): # minutes as mm.
minute = "0" + str(self._minute)
return minute[-2:]
default clock = clock()
label start:
while True:
"Day: [clock.day] Time: [clock.hh]:[clock.mm]"
$ clock.addtime(15) # Q how to able add for ex months? Count? 60x24x31~~44640
"Date: [clock.day]/[clock.month]/[clock.year] Time: [clock.hh]:[clock.mm]"
$ clock.addtime(44640) # Here Error why?
"Blow mind"
return
Drugi
init python:
weekdays = ("mon","tue","wed","thu","fri","sat","sun")
class Date:
def __init__(self):
self.year = 2019
self.month = 2
self.day = 28
self.weekday = weekdays[3]
def end_day(self):
self.day += 1
self.weekday = weekdays[weekdays.index(self.weekday)-6]
if self.month in (1,3,5,7,8,10,12):
if self.day > 31:
self.day = 1
self.month += 1
elif self.month in (4,6,9,11):
if self.day > 30:
self.day = 1
self.month += 1
#february in leap year
else :
# devide by 4
if (self.year // 4) == 0 :
# devide by 100
if (self.year // 100) == 0 :
# devide by 400
if (self.year // 400) == 0 :
#leap year
__d = 29
else:
#normal year
__d = 28
else:
#devide by 4 - leap year
__d = 29
else:
#normal year
__d = 28
if self.day > __d:
self.day = 1
self.month += 1
if self.month > 12:
self.month = 1
self.year += 1
# get weekdays
def get_weekdays(self):
if self.month < 3 :
__m = self.month + 12
__y = self.year - 1
else :
__m = self.month
__y = self.year
__weekday = ((__y+__y/4-__y/100+__y/400+(13*__m+8)/5+self.day) % 7) - 1
self.weekday = weekdays[__weekday]
return weekdays[__weekday]
def return_date(self):
return unicode(self.year)+". "+unicode(self.month)+". "+unicode(self.day)
Nie wiem jak a chciałbym zachować obie funkcjonalności
*-mieć zegar z formatowaniem
"Day: [clock.day] Time: [clock.hh]:[clock.mm]"
$ clock.addtime(15)
*-dni tygodnia z ich krótkimi anglojezycznymi nazwami
*-lata przestępne z drugiego kodu
Pozdrawiam, Marcin