A real calendar: months of 28 to 31 days and leap years; monthly prices, loans, the intern budget and conferences follow it (#257)
This commit is contained in:
parent
25a43e22df
commit
3501a642a2
12 changed files with 81 additions and 12 deletions
|
|
@ -10,7 +10,18 @@ const pick=a=>a[Math.floor(Math.random()*a.length)];
|
|||
// used, the same names were added again every day (half a million entries, half of a late-game save).
|
||||
const useName=n=>{if(!S.usedNames.includes(n))S.usedNames.push(n);};
|
||||
const esc=s=>String(s).replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
||||
function dateStr(d){const y=1+Math.floor(d/360),m=Math.floor(d/30)%12,dd=d%30+1;return tr('helpers.y_y_mon_d',{y,mon:MONTHS[m],d:dd});}
|
||||
/* A real calendar (#257): months of 28 to 31 days and Gregorian leap years, counted from Y1 (Y4 is a leap year,
|
||||
Y100 is not). Day 0 is 1 January Y1. Monthly prices are per calendar month. */
|
||||
const leapY=y=>y%4===0&&(y%100!==0||y%400===0),MDAYS=[31,28,31,30,31,30,31,31,30,31,30,31],monDays=(y,m)=>m===1&&leapY(y)?29:MDAYS[m];
|
||||
const yearStart=y=>{const n=y-1;return n*365+Math.floor(n/4)-Math.floor(n/100)+Math.floor(n/400);};
|
||||
function cal(d){d=Math.max(0,Math.floor(d)||0);let y=1+Math.floor(d/365.2425);while(y>1&&yearStart(y)>d)y--;while(yearStart(y+1)<=d)y++;
|
||||
let r=d-yearStart(y),m=0;while(r>=monDays(y,m)){r-=monDays(y,m);m++;}return {y,m,d:r+1};}
|
||||
const dayOf=(y,m,dd)=>{let n=yearStart(y);for(let i=0;i<m;i++)n+=monDays(y,i);return n+dd-1;};
|
||||
// Months since the start, to tell one calendar month from the next; and the length of the month d is in.
|
||||
const monthIdx=d=>{const c=cal(d);return (c.y-1)*12+c.m;},monLen=d=>{const c=cal(d);return monDays(c.y,c.m);};
|
||||
// A monthly date that started on day s: the same day of the month, or the month's last day when it is shorter.
|
||||
const monthiversary=(s,d)=>{if(d<=s)return false;const a=cal(s),b=cal(d);return b.d===Math.min(a.d,monDays(b.y,b.m));};
|
||||
function dateStr(d){const c=cal(d);return tr('helpers.y_y_mon_d',{y:c.y,mon:MONTHS[c.m],d:c.d});}
|
||||
function wrapDx(a,b){let dx=b-a;if(dx>W/2)dx-=W;if(dx<-W/2)dx+=W;return dx;}
|
||||
function kmBetween(a,b){const A=CITY[a],B=CITY[b];return Math.max(20,Math.round(hav(A.lon,A.lat,B.lon,B.lat)));}
|
||||
// Cities on a land bridge belong to two landmasses: Cairo reaches Asia overland across the Sinai, the way
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue