From 89d86c1b9fa69cb6479997981f6302202aca830d Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 3 Sep 2011 23:03:14 +0200 Subject: //shack/Eselkalk: initial commit This commit tries to calculate the dates of shack-plena. --- shack/Eselkalk/index.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ shack/bin/is-plenum | 34 ----------------------------- 2 files changed, 57 insertions(+), 34 deletions(-) create mode 100644 shack/Eselkalk/index.js delete mode 100755 shack/bin/is-plenum (limited to 'shack') diff --git a/shack/Eselkalk/index.js b/shack/Eselkalk/index.js new file mode 100644 index 00000000..f64e0605 --- /dev/null +++ b/shack/Eselkalk/index.js @@ -0,0 +1,57 @@ +// +// node //shack/Eselkalk DATE +// +// where DATE ∈ [YYYY[-MM]], defaulting to the current YYYY-MM +// + +range = process.argv[2] || + JSON.parse(JSON.stringify(new Date())).slice(0, '....-..'.length) + +function dates(date) { + var year = date.getFullYear() + var month = date.getMonth() + + var i = new Date([year, (month < 9 ? '0' : '') + (month + 1)].join('-')) + + var days = [] + var next_day = 4; + for ( + ; i.getMonth() === month + ; i = new Date(+i + 24 * 60 * 60 * 1000)) { + if (i.getDay() === next_day) { + next_day = next_day === 3 ? 4 : 3 + if (next_day === 3) { + var next_4day = new Date(+i + 7 * 24 * 60 * 60 * 1000) + if (next_4day.getMonth() !== month) { + i = new Date(+i - 24 * 60 * 60 * 1000) + next_4day = 4 + } + } + days.push(new Date(+i + (20 * 60 + i.getTimezoneOffset()) * 60 * 1000)) + while (i.getDay() !== 0) { + i = new Date(+i + 24 * 60 * 60 * 1000) + } + } + } + + return days +} + + +result = [] + +// TODO if (/^....-..-..$/.test(range)) { ... } +if (/^....-..$/.test(range)) { + result = dates(new Date(range)) +} +else if (/^....$/.test(range)) { + ['01','02','03','04','05','06','07','08','09','10','11','12' + ].forEach(function (i) { + result = result.concat(dates(new Date([range, i].join('-')))) + }) +} +else { + throw new Error('You are made of stupid! ' + range) +} + +console.log(JSON.stringify(result, null, 2)) diff --git a/shack/bin/is-plenum b/shack/bin/is-plenum deleted file mode 100755 index 1c686ca1..00000000 --- a/shack/bin/is-plenum +++ /dev/null @@ -1,34 +0,0 @@ -#! /bin/sh -# -# //shack/is-plenum [YEAR] -# -# where YEAR defaults to the current year -# - -y=${1-`date +%Y`} -for m in `seq 1 12`; do - t1= - t2= - t3= - t4= - t5= - week_done=-1 - for d in `cal $m $y | tr ' ' '\n' | grep '^[0-9][0-9]\?$'`; do - echo $m | grep -q '^.$' && m=0$m - echo $d | grep -q '^.$' && d=0$d - date=$y-$m-$d - date_week=`date -d $date +%W` - if test $week_done -lt $date_week; then - test -z "$t1" && { test `date -d $date +%u` = 4 && t1=$date && week_done=$date_week; continue; } - test -z "$t2" && { test `date -d $date +%u` = 3 && t2=$date && week_done=$date_week; continue; } - test -z "$t3" && { test `date -d $date +%u` = 4 && t3=$date && week_done=$date_week; continue; } - test -z "$t4" && { test `date -d $date +%u` = 3 && t4=$date && week_done=$date_week; continue; } - test -z "$t5" && { test `date -d $date +%u` = 3 && t5=$date && week_done=$date_week; continue; } - fi - done - test -n "${t1}" && echo $t1 - test -n "${t2}" && echo $t2 - test -n "${t3}" && echo $t3 - test -n "${t4}" && echo $t4 - test -n "${t5}" && echo $t5 -done -- cgit v1.2.3