Fix GH-216

This commit is contained in:
krateng 2023-10-30 17:49:09 +01:00
parent 504450fd84
commit 02a848c747
3 changed files with 12 additions and 43 deletions

@ -212,16 +212,17 @@ class MTRangeWeek(MTRangeSingular):
def __init__(self,year=None,week=None):
# do this so we can construct the week with overflow (eg 2020/-3)
thisisoyear_firstday = date.fromchrcalendar(year,1,1)
thisisoyear_firstday = date.fromisocalendar(year,1,1) + timedelta(days=malojaconfig['WEEK_OFFSET']-1)
self.firstday = thisisoyear_firstday + timedelta(days=7*(week-1))
self.firstday = date(self.firstday.year,self.firstday.month,self.firstday.day)
# for compatibility with pre python3.8 (https://bugs.python.org/issue32417)
self.lastday = self.firstday + timedelta(days=6)
# now get the actual year and week number (in case of overflow)
self.year,self.week,_ = self.firstday.chrcalendar()
fakedate = self.firstday - timedelta(days=malojaconfig['WEEK_OFFSET']-1)
# fake date that gives the correct iso return for the real date considering our week offset
self.year,self.week,_ = fakedate.isocalendar()
@ -359,7 +360,9 @@ def today():
def thisweek():
tod = datetime.now(tz=TIMEZONE)
tod = date(tod.year,tod.month,tod.day)
y,w,_ = tod.chrcalendar()
fakedate = tod - timedelta(days=malojaconfig['WEEK_OFFSET']-1)
# fake date for correct iso representation
y,w,_ = fakedate.isocalendar()
return MTRangeWeek(y,w)
def thismonth():
tod = datetime.now(tz=TIMEZONE)
@ -564,7 +567,9 @@ def year_from_timestamp(stamp):
def week_from_timestamp(stamp):
dt = datetime.fromtimestamp(stamp,tz=TIMEZONE)
d = date(dt.year,dt.month,dt.day)
y,w,_ = d.chrcalendar()
fakedate = d - timedelta(days=malojaconfig['WEEK_OFFSET']-1)
# fake date for correct iso representation
y,w,_ = fakedate.isocalendar()
return MTRangeWeek(y,w)
def from_timestamp(stamp,unit):

@ -189,7 +189,8 @@ malojaconfig = Configuration(
"delimiters_informal":(tp.Set(tp.String()), "Informal Delimiters", ["vs.","vs","&"], "Delimiters in informal artist strings with spaces expected around them"),
"delimiters_formal":(tp.Set(tp.String()), "Formal Delimiters", [";","/","|","","",""], "Delimiters used to tag multiple artists when only one tag field is available"),
"filters_remix":(tp.Set(tp.String()), "Remix Filters", ["Remix", "Remix Edit", "Short Mix", "Extended Mix", "Soundtrack Version"], "Filters used to recognize the remix artists in the title"),
"parse_remix_artists":(tp.Boolean(), "Parse Remix Artists", False)
"parse_remix_artists":(tp.Boolean(), "Parse Remix Artists", False),
"week_offset":(tp.Integer(), "Week Begin Offset", 0, "Start of the week for the purpose of weekly statistics. 0 = Sunday, 6 = Saturday")
},
"Web Interface":{
"default_range_startpage":(tp.Choice({'alltime':'All Time','year':'Year','month':"Month",'week':'Week'}), "Default Range for Startpage Stats", "year"),

@ -28,40 +28,3 @@ try:
except Exception:
pass
# proper sunday-first weeks
# damn iso heathens
from datetime import date, timedelta
import datetime
class expandeddate(date):
def chrweekday(self):
return self.isoweekday() + 1 % 7
def chrcalendar(self):
tomorrow = self + timedelta(days=1)
cal = tomorrow.isocalendar()
return (cal[0],cal[1],cal[2])
@classmethod
def fromchrcalendar(cls,y,w,d):
try:
return datetime.date.fromisocalendar(y,w,d) - timedelta(days=1) #sunday instead of monday
except Exception:
# pre python3.8 compatibility
firstdayofyear = datetime.date(y,1,1)
wkday = firstdayofyear.isoweekday()
if wkday <= 4: # day up to thursday -> this week belongs to the new year
firstisodayofyear = firstdayofyear - timedelta(days=wkday) #this also shifts to sunday-first weeks
else: # if not, still old year
firstisodayofyear = firstdayofyear + timedelta(days=7-wkday) #same
return firstisodayofyear + timedelta(days=(w-1)*7) + timedelta(days=d-1)
datetime.date = expandeddate