2020-10-13 21:02:40 +02:00
|
|
|
import datetime
|
|
|
|
import dateutil.relativedelta
|
|
|
|
import pytz
|
2021-03-03 18:04:31 +01:00
|
|
|
import time
|
2020-10-13 21:02:40 +02:00
|
|
|
|
|
|
|
|
2020-12-23 00:32:33 +01:00
|
|
|
def epochToDate(epochTimeStamp, strFormat="%d/%m/%Y om %H:%M:%S"):
|
2020-10-13 21:02:40 +02:00
|
|
|
now = dateTimeNow()
|
2020-10-23 16:34:02 +02:00
|
|
|
updateTime = datetime.datetime.fromtimestamp(int(epochTimeStamp), pytz.timezone("Europe/Brussels"))
|
2020-10-13 21:02:40 +02:00
|
|
|
diff = now - updateTime
|
2020-10-23 16:34:02 +02:00
|
|
|
updateFormatted = str(updateTime.strftime(strFormat))
|
2020-10-13 21:02:40 +02:00
|
|
|
timeAgo = str(time.strftime('%H:%M:%S', time.gmtime(diff.total_seconds())))
|
2020-10-23 19:33:01 +02:00
|
|
|
return {"date": updateFormatted, "dateDT": updateTime, "timeAgo": timeAgo}
|
2020-10-13 21:02:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def dateTimeNow():
|
|
|
|
return datetime.datetime.now(pytz.timezone("Europe/Brussels"))
|
|
|
|
|
|
|
|
|
|
|
|
def leadingZero(hourMinutes):
|
|
|
|
return ("0" if len(hourMinutes) == 3 else "") + hourMinutes
|
|
|
|
|
|
|
|
|
|
|
|
def delimiter(hourMinutes, delim=":"):
|
|
|
|
return hourMinutes[:2] + delim + hourMinutes[2:]
|
|
|
|
|
|
|
|
|
|
|
|
def timeFromInt(value):
|
|
|
|
return delimiter(leadingZero(str(value)))
|
|
|
|
|
|
|
|
|
|
|
|
# Returns age in YMWD
|
|
|
|
def age(seconds):
|
|
|
|
# Amount of years
|
|
|
|
years, seconds = timeIn(seconds, "years")
|
|
|
|
months, seconds = timeIn(seconds, "months")
|
|
|
|
weeks, seconds = timeIn(seconds, "weeks")
|
|
|
|
days, seconds = timeIn(seconds, "days")
|
|
|
|
|
|
|
|
return {"years": years, "months": months, "weeks": weeks, "days": days}
|
|
|
|
|
|
|
|
|
|
|
|
# Returns amount of [unit] in [seconds] + the remaining seconds
|
|
|
|
def timeIn(seconds, unit):
|
|
|
|
timeDic = {"years": 31556926, "months": 2629743, "weeks": 604800, "days": 86400}
|
|
|
|
timeUnit = timeDic[unit]
|
|
|
|
return seconds // timeUnit, seconds - ((seconds // timeUnit) * timeUnit)
|
|
|
|
|
|
|
|
|
|
|
|
# Creates a string representation based on Days/Hours/Minutes/Seconds
|
|
|
|
def diffDayBasisString(timestamp):
|
|
|
|
if isinstance(timestamp, int):
|
2020-10-23 19:33:01 +02:00
|
|
|
timestamp = epochToDate(timestamp)["dateDT"]
|
|
|
|
now = dateTimeNow()
|
2020-10-13 21:02:40 +02:00
|
|
|
diff = dateutil.relativedelta.relativedelta(now, timestamp)
|
|
|
|
|
|
|
|
timeList = []
|
|
|
|
|
|
|
|
# Don't add obsolete info such as "0 days", ...
|
|
|
|
if diff.days != 0:
|
|
|
|
timeList.append("{} {}".format(diff.days, getPlural(diff.days, "days")))
|
|
|
|
|
|
|
|
if diff.hours != 0:
|
|
|
|
timeList.append("{} uur".format(diff.hours))
|
|
|
|
|
|
|
|
if diff.minutes != 0:
|
|
|
|
timeList.append("{} {}".format(diff.minutes, getPlural(diff.minutes, "minutes")))
|
|
|
|
|
|
|
|
if diff.seconds != 0:
|
|
|
|
timeList.append("{} {}".format(diff.seconds, getPlural(diff.seconds, "seconds")))
|
|
|
|
|
|
|
|
timeString = ", ".join(timeList[:-1])
|
|
|
|
if len(timeString) > 0:
|
|
|
|
timeString += " en "
|
|
|
|
timeString += timeList[-1]
|
|
|
|
return timeString
|
|
|
|
|
|
|
|
|
|
|
|
def diffYearBasisString(timestamp):
|
|
|
|
if isinstance(timestamp, int):
|
2020-10-23 19:33:01 +02:00
|
|
|
timestamp = epochToDate(timestamp)["dateDT"]
|
|
|
|
now = dateTimeNow()
|
2020-10-13 21:02:40 +02:00
|
|
|
diff = dateutil.relativedelta.relativedelta(now, timestamp)
|
|
|
|
timeList = []
|
|
|
|
|
|
|
|
# Don't add obsolete info such as "0 days", ...
|
|
|
|
if diff.years != 0:
|
|
|
|
timeList.append("{} jaar".format(diff.years))
|
|
|
|
|
|
|
|
if diff.months != 0:
|
|
|
|
timeList.append("{} {}".format(diff.months, getPlural(diff.months, "months")))
|
|
|
|
|
|
|
|
if diff.weeks != 0:
|
|
|
|
timeList.append("{} {}".format(diff.weeks, getPlural(diff.weeks, "weeks")))
|
|
|
|
|
|
|
|
if diff.days != 0:
|
|
|
|
timeList.append("{} {}".format(diff.days, getPlural(diff.days, "days")))
|
|
|
|
|
2020-11-22 00:30:26 +01:00
|
|
|
timeString = ""
|
|
|
|
|
|
|
|
if not timeList:
|
|
|
|
return "Minder dan een dag"
|
|
|
|
|
|
|
|
if len(timeList) > 1:
|
|
|
|
timeString = ", ".join(timeList[:-1])
|
2020-10-13 21:02:40 +02:00
|
|
|
if len(timeString) > 0:
|
|
|
|
timeString += " en "
|
2020-11-22 00:30:26 +01:00
|
|
|
|
2020-10-13 21:02:40 +02:00
|
|
|
timeString += timeList[-1]
|
2020-11-22 00:30:26 +01:00
|
|
|
|
2020-10-13 21:02:40 +02:00
|
|
|
return timeString
|
|
|
|
|
|
|
|
|
|
|
|
# Returns the full day based on an abbreviation or full name
|
|
|
|
def getFormat(term):
|
|
|
|
terms = ["days", "weeks", "months", "years"]
|
|
|
|
for word in terms:
|
|
|
|
if word.startswith(term.lower()):
|
|
|
|
return word
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
# Gets the plural of a unit if necessary
|
|
|
|
def getPlural(amount, unit):
|
|
|
|
dic = {
|
|
|
|
"seconds": {"s": "seconde", "p": "seconden"},
|
|
|
|
"minutes": {"s": "minuut", "p": "minuten"},
|
|
|
|
"hours": {"s": "uur", "p": "uur"},
|
|
|
|
"days": {"s": "dag", "p": "dagen"},
|
|
|
|
"weeks": {"s": "week", "p": "weken"},
|
|
|
|
"months": {"s": "maand", "p": "maanden"},
|
|
|
|
"years": {"s": "jaar", "p": "jaar"}
|
|
|
|
}
|
|
|
|
return dic[unit.lower()]["s" if amount == 1 else "p"]
|
|
|
|
|
|
|
|
|
2021-07-23 21:03:14 +02:00
|
|
|
def weekdayToInt(day) -> int:
|
2020-10-13 21:02:40 +02:00
|
|
|
days = {"maandag": 0, "dinsdag": 1, "woensdag": 2, "donderdag": 3, "vrijdag": 4, "zaterdag": 5, "zondag": 6}
|
2021-07-23 21:03:14 +02:00
|
|
|
|
|
|
|
if day.lower() not in days:
|
|
|
|
return -1
|
|
|
|
|
2020-10-13 21:02:40 +02:00
|
|
|
return days[day.lower()]
|
|
|
|
|
|
|
|
|
|
|
|
def intToWeekday(day):
|
|
|
|
return ["Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag", "Zondag"][day]
|
2021-01-20 00:55:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def fromString(timeString: str, formatString="%d/%m/%Y"):
|
|
|
|
"""
|
|
|
|
Constructs a datetime object from an input string
|
|
|
|
"""
|
|
|
|
return datetime.datetime.strptime(timeString, formatString)
|