RosettaCodeData/Task/Calendar/Huginn/calendar.huginn
2023-07-01 13:44:08 -04:00

61 lines
1.7 KiB
Text

import DateTime as dt;
import Algorithms as algo;
import Text as text;
class Calendar {
_monthNames_ = none;
_dayNames_ = none;
constructor() {
t = dt.now();
_monthNames_ = algo.materialize( algo.map( algo.range( 1, 13 ), @[t]( m ) { dt.format( "%B", t.set_date( 1, m, 1 ) ); } ), tuple );
_dayNames_ = algo.materialize( algo.map( algo.range( 1, 8 ), @[t]( d ) { dt.format( "%a", t.set_date( 1, 1, d ) )[:2]; } ), tuple );
}
print_year( year_, cols_ ) {
t = dt.now();
print( "{:^66d}\n".format( year_ ) );
for ( rm : algo.range( 12 / cols_ ) ) {
m = rm * cols_;
print( text.repeat( "{:^22s}", cols_ ).format( _monthNames_[m:m + cols_]... ) + "\n" );
day = [];
daysInMonth = [];
for ( mc : algo.range( cols_ ) ) {
print( " {} {} {} {} {} {} {} ".format( _dayNames_... ) );
t.set_date( year_, m + mc + 1, 1 );
day.push( - t.get_day_of_week() + 1 );
daysInMonth.push( t.get_days_in_month() );
}
print( "\n" );
haveDay = true;
while ( haveDay ) {
haveDay = false;
for ( mc : algo.range( cols_ ) ) {
for ( d : algo.range( 7 ) ) {
if ( ( day[mc] > 0 ) && ( day[mc] <= daysInMonth[mc] ) ) {
print( " {:2d}".format( day[mc] ) );
haveDay = true;
} else {
print( " " );
}
day[mc] += 1;
}
print( " " );
}
print( "\n" );
}
}
}
}
main( argv_ ) {
cal = Calendar();
cols = size( argv_ ) > 2 ? integer( argv_[2] ) : 3;
if ( 12 % cols != 0 ) {
cols = 3;
}
cal.print_year(
size( argv_ ) > 1
? integer( argv_[1] )
: dt.now().get_year(),
cols
);
}