Posts: 6
Threads: 2
Joined: Feb 2019
Feb-22-2019, 06:47 AM
(This post was last modified: Feb-22-2019, 09:28 AM by Larz60+.)
self learning python and been stuck trying to figure this out.
Trying to output both the day before and the day after a given date without using the date.time module.
script so far:
#!/usr/bin/env python3
####enter date in yyyymmdd format
def dbda(today):
if len (today) != 8:
return '00000000'
else:
year = int(today[0:4])
month = int(today[4:6])
day = int(today[6:])
####calculate leap year
lyear = year % 4
if lyear == 0:
feb_max = 29
else:
feb_max = 28
lyear = year % 100
if lyear == 0:
feb_max = 28
lyear = year % 400
if lyear == 0:
feb_max = 29
next_day = day + 1 #tomorrow
pass_day = day - 1 #yesterday
mon_max = { 1:31, 2:feb_max, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
Posts: 818
Threads: 1
Joined: Mar 2018
I think, it would be better to create class that describes dates.
class Date:
feb_max_length = 29
allowed_month_lengths = {1: 31,
2: feb_max_length - 1,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31
}
def __init__(self, day, month, year):
self.day = day
self.month = month
self.year = year
self._validate()
def _validate(self):
if not(isinstance(self.year, int) and isinstance(self.month, int) and isinstance(self.day, int)):
raise Exception("Error: year, month, day should be integers.")
if not(1 <= self.month <= 12):
raise Exception("Error: month should be in [1, 12], your value is {}.".format(self.month))
if not (1 <= self.day <= self.month_length):
raise Exception("Error: day number ({}) should be less or equal {}.".format(self.day, self.month_length))
def __str__(self):
return 'Date: {}.{}.{}'.format(self.day, self.month, self.year)
def __repr__(self):
return self.__str__()
@property
def is_leap_year(self):
return self.year % 4 == 0 and (self.year % 100 != 0 or self.year % 400 == 0)
@property
def month_length(self):
_ = self.allowed_month_lengths.get(self.month, None)
return _ if not self.is_leap_year else (_ + 1 if self.month == 2 else _)
@property
def day_after(self):
"""Returns date = 1 day + current date
"""
if 1 <= self.day < self.month_length:
return Date(self.day + 1, self.month, self.year)
if self.day == self.month_length and self.month == 12:
return Date(1, 1, self.year + 1)
elif self.day == self.month_length and self.month < 12:
return Date(1, self.month + 1, self.year)
@property
def day_before(self):
raise NotImplementedError
d = Date(28, 2, 2019)
print("Current date:", d)
print("Next day: ", d.day_after)
print("Two days after:", d.day_after.day_after)
Posts: 6
Threads: 2
Joined: Feb 2019
Posts: 74
Threads: 13
Joined: Feb 2019
Just amended the above script by adding the 'day before'. Cheers
class Date:
feb_max_length = 29
allowed_month_lengths = {1: 31,
2: feb_max_length - 1,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31
}
def __init__(self, day, month, year):
self.day = day
self.month = month
self.year = year
self._validate()
def _validate(self):
if not(isinstance(self.year, int) and isinstance(self.month, int) and isinstance(self.day, int)):
raise Exception("Error: year, month, day should be integers.")
if not(1 <= self.month <= 12):
raise Exception("Error: month should be in [1, 12], your value is {}.".format(self.month))
if not (1 <= self.day <= self.month_length):
raise Exception("Error: day number ({}) should be less or equal {}.".format(self.day, self.month_length))
def __str__(self):
return ' {}.{}.{}'.format(self.day, self.month, self.year)
def __repr__(self):
return self.__str__()
@property
def is_leap_year(self):
return self.year % 4 == 0 and (self.year % 100 != 0 or self.year % 400 == 0)
@property
def month_length(self):
_ = self.allowed_month_lengths.get(self.month, None)
return _ if not self.is_leap_year else (_ + 1 if self.month == 2 else _)
@property
def day_after(self):
"""Returns date = 1 day + current date
"""
if 1 <= self.day < self.month_length:
return Date(self.day + 1, self.month, self.year)
if self.day == self.month_length and self.month == 12:
return Date(1, 1, self.year + 1)
elif self.day == self.month_length and self.month < 12:
return Date(1, self.month + 1, self.year)
@property
def day_before(self):
"""Returns date = -1 day + current date
"""
if 1 <= self.day < self.month_length:
return Date(self.day - 1, self.month, self.year)
if self.day == self.month_length and self.month == 12:
return Date(1, 1, self.year + 1)
elif self.day == self.month_length and self.month < 12:
return Date(1, self.month + 1, self.year)
d = Date(28, 2, 2020)
print("Current date: ", d)
print("Next day: ", d.day_after)
print("Two days after:", d.day_after.day_after)
print("Day before: ", d.day_before)
# ----------- OUTPUT ---------------
#Current date: 28.2.2020
#Next day: 29.2.2020
#Two days after: 1.3.2020
#Day before: 27.2.2020
#-----------------------------------
Posts: 6
Threads: 2
Joined: Feb 2019
is there a way to make so that it takes any date instead of just one given date
Posts: 74
Threads: 13
Joined: Feb 2019
(Feb-22-2019, 10:40 PM)apexman Wrote: is there a way to make so that it takes any date instead of just one given date Of course, just modifying this line inside the code source:
d = Date(28, 2, 2020) Cheers
Posts: 74
Threads: 13
Joined: Feb 2019
Feb-23-2019, 09:28 AM
(This post was last modified: Feb-23-2019, 09:29 AM by samsonite.)
Better to have the yearly calendar, as shown underneath. Cheers
# ------- mycal.py ----------
import calendar
calendar.prcal(2020)
# ------- OUTPUT ------------
# 2020
#
# January February March
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 1 2 1
# 6 7 8 9 10 11 12 3 4 5 6 7 8 9 2 3 4 5 6 7 8
# 13 14 15 16 17 18 19 10 11 12 13 14 15 16 9 10 11 12 13 14 15
# 20 21 22 23 24 25 26 17 18 19 20 21 22 23 16 17 18 19 20 21 22
# 27 28 29 30 31 24 25 26 27 28 29 23 24 25 26 27 28 29
# 30 31
#
# April May June
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
# 6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
# 13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
# 20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
# 27 28 29 30 25 26 27 28 29 30 31 29 30
#
# July August September
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 1 2 1 2 3 4 5 6
# 6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
# 13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
# 20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
# 27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
# 31
#
# October November December
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 1 1 2 3 4 5 6
# 5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
# 12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
# 19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
# 26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
# 30
#
Posts: 818
Threads: 1
Joined: Mar 2018
Feb-23-2019, 12:43 PM
(This post was last modified: Feb-23-2019, 12:43 PM by scidam.)
(Feb-22-2019, 08:49 PM)samsonite Wrote: def day_before(self):
"""Returns date = -1 day + current date
"""
Does not work correctly: if you pass, e.g. d = Date(1,1,2020) , than d.day_before causes an error.
Posts: 74
Threads: 13
Joined: Feb 2019
Yes, scidam, you are right. As a matter of generality, such a code is too weak, it's preferable refer to module calendar, as per my previous post. Thank you
Posts: 6
Threads: 2
Joined: Feb 2019
much thanks for all the replies.
|