minj / foxtrick

FoxTrick is a browser extension for the Hattrick online football manager game, currently available for Firefox, Google Chrome, as well as for Opera.
https://www.foxtrick.org
GNU General Public License v3.0
69 stars 49 forks source link

BUG gregorianToHT ?? #1666

Closed akasolace closed 3 years ago

akasolace commented 4 years ago

I just tried to call gregorianToHT(new Date(2020,8,31)) and it returns { season: 76, week: 2 } Am I doing something wrong because I imagine this function has been tested quite a lot Should you not have ORIGIN = new Date(1997, 6, 2) ?

akasolace commented 4 years ago

in case it helps this is what I came up with (in python) that seems to do the job:

def gregorianToHT(_date, useLocal = False, iLeagueID=None):

    ORIGIN = date(1997, 6, 2)
    dayDiff = (_date-ORIGIN).days
    season = dayDiff // 112
    week = (dayDiff % 112 // 7) + 1

    if useLocal:
        assert iLeagueID is not None, "if useLocal = True, iLeagueID should be provided"
        season += dSeasonOffset[iLeagueID]

    return season, week

# some tests ======
assert gregorianToHT(date(2020, 6, 27)) == (75, 4)
assert gregorianToHT(date(2018, 5, 10)) == (68, 5)
assert gregorianToHT(date(2009, 5, 28)) == (39, 2)
assert gregorianToHT(date(2020, 8, 31), True, 52) == (60, 14)
minj commented 3 years ago
>>> str(date(2020, 8, 31))
'2020-08-31'
new Date(2020,8,31).toString()
"Thu Oct 01 2020 00:00:00 GMT+0200 (Central European Summer Time)"

Does that answer your question?

minj commented 3 years ago

To be specific, this passes no problem:

gregorianToHT = (...args) => { let {season, week} = Foxtrick.util.time.gregorianToHT(...args); return season*16 + week; };
console.assert(gregorianToHT(new Date(2020, 5, 27)) == 75*16+4);
console.assert(gregorianToHT(new Date(2018, 4, 10)) == 68*16+5);
console.assert(gregorianToHT(new Date(2009, 4, 28)) == 39*16+2);
minj commented 3 years ago

For JS weirdness see MDN docs. I.e. month starts from 0. I know, I know.

Keep in mind, that local season is specific to the running user, and we also (sometimes) take into account HTDateFormat settings.

The sometimes part should probably be fixed. But I don't think this issue itself is valid