Skip to content

Instantly share code, notes, and snippets.

@jonasmalacofilho
Created August 7, 2013 07:57
Show Gist options
  • Save jonasmalacofilho/6172107 to your computer and use it in GitHub Desktop.
Save jonasmalacofilho/6172107 to your computer and use it in GitHub Desktop.
Quick and dirty day type by month calculator in Haxe.
Year Month Weekdays Saturdays Sundays
2013 1 23 4 4
2013 2 20 4 4
2013 3 21 5 5
2013 4 22 4 4
2013 5 23 4 4
2013 6 20 5 5
2013 7 23 4 4
2013 8 22 5 4
2013 9 21 4 5
2013 10 23 4 4
2013 11 21 5 4
2013 12 22 4 5
# Builds the "Quick and dirty day type by month calculator in Haxe" for neko
-neko types_of_days.n
-main TypesOfDays
/*
* Quick and dirty day type by month calculator in Haxe.
*
* Counts the number of weekdays, saturdays and sundays for each month
* of the suplied range.
*
* Copyright 2013, Jonas Malaco Filho.
* All rights reserved. Licensed under the BSD 2-clause license:
* http://opensource.org/licenses/BSD-2-Clause
*/
class TypesOfDays {
function new( start:PartialDate, finish:PartialDate ) {
var cur = start;
Sys.println( "Year\tMonth\tWeekdays\tSaturdays\tSundays" );
do {
var cnt = count( cur );
Sys.println( [ cur.year, cur.month, cnt.weekdays, cnt.saturdays, cnt.sundays ].join( "\t" ) );
cur = cur.nextMonth();
} while ( cur.toDate().getTime() <= finish.toDate().getTime() );
}
function count( month:PartialDate ):{ weekdays:Int, saturdays:Int, sundays:Int } {
// trace( untyped [ month, month.toDate().toString() ] );
var helper = [Sunday,Weekday,Weekday,Weekday,Weekday,Weekday,Saturday,Sunday,Weekday,Weekday,Weekday,Weekday,Weekday,Saturday];
var noDays = DateTools.getMonthDays( month.toDate() );
var weekdays = 20;
var saturdays = 4;
var sundays = 4;
var ref = month.toDate().getDay();
for ( i in 0...( noDays - 28 ) ) {
switch ( helper[ ref + i ] ) {
case Weekday: weekdays++;
case Saturday: saturdays++;
case Sunday: sundays++;
}
}
return { weekdays:weekdays, saturdays:saturdays, sundays:sundays };
}
static function main() {
var app = new TypesOfDays( new PartialDate( 1980, 1 ), new PartialDate( 2013, 12 ) );
}
}
class PartialDate {
public var year:Int;
public var month:Int;
/*
* Proper month should be in the range 1-12.
*/
public function new( _year:Int, _properMonth:Int ) {
year = _year;
month = _properMonth;
}
public function toDate():Date {
return new Date( year, month - 1, 1, 0, 0, 0 );
}
public function nextMonth():PartialDate {
if ( month == 12 )
return new PartialDate( year + 1, 1 );
else
return new PartialDate( year, month + 1 );
}
}
enum DayType {
Weekday;
Saturday;
Sunday;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment