Skip to content

Instantly share code, notes, and snippets.

@Nia-TN1012
Forked from Myoga1012/Calendar.adb
Last active January 30, 2023 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nia-TN1012/d62e8cf5825c50a8bb1ac9bade31b9d4 to your computer and use it in GitHub Desktop.
Save Nia-TN1012/d62e8cf5825c50a8bb1ac9bade31b9d4 to your computer and use it in GitHub Desktop.
Adaでカレンダーを出力するソースコードです。
-- 名前 : Nia Tomonaka
-- Twitter : https://twitter.com/nia_tn1012
-- 使用するライブラリの宣言
-- 入出力
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-2023 Nia T.N. Tech Lab. / Chronoir.net.
-- This software is released under the MIT License.
-- http://opensource.org/licenses/mit-license.php
@Nia-TN1012
Copy link
Author

GitHubのアカウント統合のため、Myoga1012→Nia-TN1012に移行しました。

旧URL: https://gist.github.com/Myoga1012/51c9bcf25dc504a9af11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment