Last active
March 8, 2017 10:20
-
-
Save reqshark/e34d8ca833b79d4b2a7df87cb26e75e1 to your computer and use it in GitHub Desktop.
the calendar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const days = [ 's', 'm', 't', 'w', 't', 'f', 's' ] | |
const today = new Date(Date.now()) | |
// calsz, for conducting hypertext markup language measurements | |
const calsz = [ 0 ] | |
// generate a calendar year | |
const calendar = yr ( today.getFullYear() ) | |
calendar | |
.map( a => a.length * 54 ) | |
.reduce( (p, c, i) => { | |
calsz[++i] = p + c | |
return calsz[i] | |
}, 0) | |
function yr ( year ) { | |
var t = [] // to return 12 months | |
[ 'j', 'f', 'm', 'a', 'm', 'j', 'j', 'a', 's', 'o', 'n', 'd' ] | |
.map(function (m, i) { return makecal ( i, year ) }) | |
.reduce ( nextmonth, makecal ( 12, year-1 ) ) | |
return t | |
function nextmonth ( p, cal, i ) { | |
var j = cal.day, prevlen = p.len, w = [], s= 1; | |
w[j] = s | |
while ( j-- ) | |
w[j] = prevlen-- | |
j = w.length - 1 | |
while ( j++ < 6 ) | |
w[j] = ++s // done with week 1, moving onto more weeks | |
// this hamster spins out the remainder of the month into arrays of weeks | |
j = 7; var weeks = [ w ] // <--- starting w/ the first week | |
while ( j-- ) | |
weeks.push( nextweek( -1, [] ) ) | |
weeks = weeks.filter( y => y.length ) // remove any extra empty weeks | |
// if days of last week bleed into first days of next month, pop() it | |
if ( weeks[ weeks.length - 1 ].length < 7 ) | |
weeks.pop() | |
t.push(weeks) | |
return cal // the next month needs this info | |
function nextweek ( i, x ) { | |
while ( i++ < 6 && s++ < cal.len ) | |
x[i] = s | |
return x // full next week | |
} | |
} | |
} | |
function makecal ( month, y ) { | |
return { | |
day: ( new Date( y, month, 1 ) ).getDay(), // starting day # of the week | |
len: mlen( month, y ), // length of month | |
} | |
} | |
function mlen ( m, y ) { | |
if ( m == 1 ) // feb leap year | |
if ( ( y % 4 == 0 && y % 100 != 0 ) || y % 400 == 0 ) | |
return 29 | |
return [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ][ m ] // days per mo | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment