PlayGround
Code:
import datetime
from datetime import timedelta
import calendar
from enum import Enum, auto
class enumTimes(Enum):
DATE = auto()
DAY = auto()
YEAR = auto()
HOUR = auto()
MINUTES = auto()
SECONDS = auto()
class PlayGround:
def __init__(self):
self.week_days = {1: 'sunday',
2: 'monday',
3: 'tuesday',
4: 'wednesday',
5: 'thursday',
6: 'friday',
7: 'saturday',
}
self.dayOfMonth = {1:"first_of", 2:"second_of", 3: "third_of", 4: "fourth_of", 5: "fifth_of", 6: "sixth_of", 7: "seventh_of",
8: "eighth_of", 9: "nineth_of", 10 : "tenth_of", 11: "eleventh_of", 12: "twelveth_of", 13: "thirteenth_of",
14 : "fourteenth_of", 15: "fifteenth_of", 16: "sixteenth_of", 17: "seventeenth_of", 18: "eighteenth_of",
19: "nineteenth_of", 20: "twentyth_of", 21: "twentyfirst_of", 22: "twentysecond_of", 23: "twentythird_of",
24: "twentyfourth_of", 25: "twentyfifth_of", 26: "twentysixth_of", 27: "twentyseventh_of", 28: "twentyeighth_of",
29: "twentynineth_of", 30: "thirtyth_of", 31: "thirtyfirst_of"}
def getCurrentTimeStamp(self) -> str:
'''This method returns the current time (hh:mm)'''
right_now = datetime.datetime.now()
return str(right_now.hour) + ":" + str(right_now.minute)
def getMonthAsInt(self) -> int:
'''This method returns the current month (MM)'''
right_now = datetime.datetime.now()
return right_now.month
def getDayOfTheMonthAsInt(self) -> int:
'''This method returns the current day (dd)'''
right_now = datetime.datetime.now()
return right_now.day
def getYearAsInt(self) -> int:
'''This method returns the current year (yyyy)'''
right_now = datetime.datetime.now()
return right_now.year
def getDayAsInt(self) -> int:
'''This method returns the current day of the week (1, 2, ... 7)'''
right_now = datetime.datetime.now()
return right_now.isoweekday()
def getMinutes(self) -> str:
'''This method returns the current minutes (mm)'''
right_now = datetime.datetime.now()
return str(right_now.minute)
def getSeconds(self) -> str:
'''This method returns the current seconds (ss)'''
right_now = datetime.datetime.now()
return str(right_now.second)
def getDayOfDWeek(self) -> str:
'''This method returns the current day of the week as a word (monday, ...)'''
right_now = datetime.datetime.now()
return calendar.day_name[right_now.weekday()]
def translateMonthDay(self, day: int) -> str:
'''This method returns the current day of the month as a word (first_of, ...)'''
return self.dayOfMonth.get(day, "")
def getSpecificTime(self, time_variable: enumTimes) -> str:
'''This method returns the current specific date in words (eleventh_of June 2021, ...)'''
enum_temp = time_variable.name
if enum_temp == "DATE":
right_now = datetime.datetime.now()
output = self.translateMonthDay(right_now.day) + " " + calendar.month_name[right_now.month] + " " + str(
right_now.year)
elif enum_temp == "HOUR":
output = str(datetime.datetime.now().hour)
elif enum_temp == "SECONDS":
output = str(datetime.datetime.now().second)
elif enum_temp == "MINUTES":
output = str(datetime.datetime.now().minute)
elif enum_temp == "YEAR":
output = str(datetime.datetime.now().year)
else:
output = ""
return output
def getSecondsAsInt(self) -> int:
'''This method returns the current seconds'''
right_now = datetime.datetime.now()
return right_now.second
def getMinutesAsInt(self) -> int:
'''This method returns the current minutes'''
right_now = datetime.datetime.now()
return right_now.minute
def getHoursAsInt(self) -> int:
'''This method returns the current hour'''
right_now = datetime.datetime.now()
return right_now.hour
def getFutureInXMin(self, extra_minutes: int) -> str:
'''This method returns the date in x minutes'''
right_now = datetime.datetime.now()
final_time = right_now + datetime.timedelta(minutes=extra_minutes)
return str(final_time)
def getPastInXMin(self, less_minutes: int) -> str:
'''This method returns the date x minutes before'''
right_now = datetime.datetime.now()
final_time = right_now - datetime.timedelta(minutes=less_minutes)
return str(final_time)
def getFutureHour(self, startHour: int, addedHours: int) -> int:
'''This method returns the hour in x hours from the starting hour'''
return (startHour + addedHours) % 24
def getFutureFromXInYMin(self, to_add: int, start: str) -> str:
'''This method returns the time (hh:mm) in x minutes the starting time (hh:mm)'''
values = start.split(":")
times_to_add = (int(values[1]) + to_add) // 60
new_minutes = (int(values[1]) + to_add) % 60
new_time = str((int(values[0]) + times_to_add) % 24) + ":" + str(new_minutes)
return new_time
def timeInXMinutes(self, x: int) -> str:
'''This method returns the time (hh:mm) in x minutes'''
right_now = datetime.datetime.now()
final_time = right_now + datetime.timedelta(minutes=x)
return str(final_time.hour) + ":" + str(final_time.minute)
def isDayTime(self) -> bool:
'''This method returns true if it's daytime (6-18)'''
return 5 < datetime.datetime.now().hour < 19
def smallToBig(self, *a) -> bool:
for i in range(len(a) - 1):
if a[i] > a[i + 1]:
return False
return True
def partOfDay(self) -> str:
'''This method returns which part of the day it is (morning, ...)'''
hour: int = self.getHoursAsInt()
if self.smallToBig(5, hour, 12):
return "morning"
elif self.smallToBig(11, hour, 17):
return "afternoon"
elif self.smallToBig(16, hour, 21):
return "evening"
return "night"
def convertToDay(self, number: int) -> str:
'''This method converts the week number to the weekday name'''
return self.week_days.get(number, "")
def isNight(self) -> bool:
'''This method returns true if it's night (21-5)'''
hour: int = self.getHoursAsInt()
return hour > 20 or hour < 6
def getTomorrow(self) -> str:
'''This method returns tomorrow'''
right_now = datetime.datetime.now()
if (right_now.weekday == 6):
return "Monday"
return calendar.day_name[right_now.weekday() + 1]
def getYesterday(self) -> str:
'''This method returns yesterday'''
right_now = datetime.datetime.now()
if right_now.weekday == 0:
return "Sunday"
return calendar.day_name[right_now.weekday() - 1]
def getGMT(self) -> int:
'''This method returns the local GMT'''
right_now = datetime.datetime.now()
timezone = int(str(right_now.astimezone())[-6:-3])
return timezone
def getLocal(self) -> str:
'''This method returns the local time zone'''
return datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
if __name__ == "__main__":
playground = PlayGround()
print("The current time is: " + playground.getCurrentTimeStamp())
print("The current month is: " + str(playground.getMonthAsInt()))
print("The current day is: " + str(playground.getDayOfTheMonthAsInt()))
print("The current year is: " + str(playground.getYearAsInt()))
print("The current day of the week (number) is: " + str(playground.getDayAsInt()))
print("The current minutes are: " + playground.getMinutes())
print("The current seconds are: " + playground.getSeconds())
print("The current day of the week (word) is: " + playground.getDayOfDWeek())
print("here")
print("The current date in words is: " + playground.getSpecificTime(enumTimes.DATE))
print("The current seconds are: " + str(playground.getSecondsAsInt()))
print("The current minutes are: " + str(playground.getMinutesAsInt()))
print("The current hours are: " + str(playground.getHoursAsInt()))
print("The current date in words is: " + playground.getFutureInXMin(30))
print("The current date in words is: " + playground.getPastInXMin(30))
print("The hour x in y hours will be: " + str(playground.getFutureHour(5, 24)))
print("The time (hh:mm) in x minutes will be: " + playground.getFutureFromXInYMin(150, "23:10"))
print("The current time (hh:mm) in x minutes will be: " + playground.timeInXMinutes(90))
print("Right now is daytime: " + str(playground.isDayTime()))
print("Right now is: " + playground.partOfDay())
print("Right now is night: " + str(playground.isNight()))
print("Tomorrow will be a: " + playground.getTomorrow())
print("Yesterday was: " + playground.getYesterday())
print("The GTM is: " + str(playground.getGMT()))
print("The local time zone is: " + str(playground.getLocal()))
print("The weekday corresponding to number 2 is: " + playground.convertToDay(7))
print(playground.convertToDay(1))