Last active
January 30, 2023 08:45
-
-
Save Myoga1012/51c9bcf25dc504a9af11 to your computer and use it in GitHub Desktop.
Adaでカレンダーを出力するソースコードです。
This file contains 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
-- 名前 : Myoga Screw-bright (旧名:Myoga S. Tomonaka) | |
-- Twitter : https://twitter.com/Myoga1012 | |
-- 使用するライブラリの宣言 | |
-- 入出力 | |
with Ada.Text_IO, Ada.Integer_Text_IO; use Ada.Text_IO; | |
-- カレンダー(但し、他の言語でいう日付ライブラリみたいなものです) | |
with Ada.Calendar, Ada.Calendar.Formatting, Ada.Calendar.Time_Zones; | |
use Ada.Calendar, Ada.Calendar.Formatting, Ada.Calendar.Time_Zones; | |
procedure Calender is | |
-- 曜日をDay_Name列挙体から数値に変換します。 | |
function WeekToInt( Item : in Day_Name ) return Integer is | |
wi : Integer := 0; | |
begin | |
case Item is | |
when Sunday => wi := 0; | |
when Monday => wi := 1; | |
when Tuesday => wi := 2; | |
when Wednesday => wi := 3; | |
when Thursday => wi := 4; | |
when Friday => wi := 5; | |
when Saturday => wi := 6; | |
end case; | |
return wi; | |
end WeekToInt; | |
-- 当月末日を求めます。 | |
function DaysInMonth( Year : in Integer; Month : in Integer ) return Integer is | |
ld : Integer := 30; | |
begin | |
if Month = 2 then | |
if Year mod 4 = 0 then ld := 29; | |
else ld := 28; | |
end if; | |
elsif Month <= 7 and Month mod 2 /= 0 then ld := 31; | |
elsif Month >= 8 and Month mod 2 = 0 then ld := 31; | |
end if; | |
return ld; | |
end DaysInMonth; | |
-- 現在の日付を取得し、1日目の曜日と月末を求めます。 | |
Now : Time := Clock; | |
Now1 : Time := Ada.Calendar.Time_Of( Year => Ada.Calendar.Year( Now ), Month => Ada.Calendar.Month( Now ), Day => 1 ); | |
First : Integer := WeekToInt( Ada.Calendar.Formatting.Day_of_Week( Now1 ) ); | |
Last : Integer := DaysInMonth( Year => Ada.Calendar.Year(Now1), Month => Ada.Calendar.Month(Now1) ); | |
begin | |
-- 当月1日の曜日に合わせてオフセットします。 | |
for I in Integer range 1..First loop | |
Put( " " ); | |
end loop; | |
-- カレンダーを出力します。 | |
for CurDay in Integer range 1..Last loop | |
-- Itemに数値(日付)を、Widthに最小桁数を指定します。C言語のprintf関数でいう「printf( "%3d", CurDay );」です。 | |
Ada.Integer_Text_IO.Put( Item => CurDay, Width => 3 ); | |
if ( CurDay + First ) mod 7 = 0 then New_Line( 1 ); | |
elsif CurDay = Last then New_Line( 1 ); | |
end if; | |
end loop; | |
end Calender; | |
-- Calendar.coffee | |
-- 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