Skip to content

Instantly share code, notes, and snippets.

@Myoga1012
Last active January 30, 2023 08:52
Show Gist options
  • Save Myoga1012/f2968e4c695bb7fcc56e to your computer and use it in GitHub Desktop.
Save Myoga1012/f2968e4c695bb7fcc56e to your computer and use it in GitHub Desktop.
Ceylonでカレンダーを出力するソースコードです。
// 名前 : Myoga Screw-bright (旧名:Myoga S. Tomonaka)
// Twitter : https://twitter.com/Myoga1012
import ceylon.time { Date, date, today }
"Run the module `calendar`."
shared void run() {
// 現在日を取得し、当月1日の曜日と月末日を求めます。
Date now = today(); variable String wl = ""; // wlはカレンダー出力用文字列変数です。
value prePad = date( now.year, now.month, 1 ).dayOfWeek.integer; // 日曜日0始まりです。
value lastDay = date( now.year, now.month.integer + 1, 1 ).minusDays( 1 ).day;
// カレンダーを出力します。
print( now.month.string + " " + now.year.string ); // 年月を出力します。
for( curDay in ( -prePad + 1 )..lastDay ){
// Ceylonのprintメソッドは自動的に改行してしまうので、
// 週単位で文字列にまとめ、それを出力します。
wl += ( curDay < 1 then " " else ( curDay < 10 then " " + curDay.string else curDay.string ) ) + " ";
if( ( curDay + prePad ) % 7 == 0 || curDay == lastDay ){ print( wl ); wl = ""; }
}
}
/*
※備考
Ceylonでは、Date.monthの値(月)とDate.dayOfWeekの値(曜日)が
いわゆる列挙体のようなデータ型です。
それらのstringプロパティの値は月名の英語(januaryなど)や曜日名の英語(sundayなど)
となります。数値として扱うときはintegerプロパティを使用します。
*/
// Calender.ceylon
//Copyright (c) 2014 Myoga-TN.net All Rights Reserved.
//This software is released under the MIT License.
//http://opensource.org/licenses/mit-license.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment