#import "MAEaster.h"
@implementation MAEaster
- (IBAction)searchEaster:(id)sender
{
//入力日付を取得→inputDate
//dateWithNaturalLanguageString:メソッドを使用しているので"next year" などの入力も可能
NSCalendarDate *inputDate =
[NSCalendarDate dateWithNaturalLanguageString:[dateField stringValue]];
//Easterリスト、配列の最後はnil
NSArray *easterday = [[[NSArray alloc] initWithObjects:
@"2007/4/8",
@"2008/3/23",
@"2009/4/12",
@"2010/4/4",
@"2011/4/24",
@"2012/4/8",
@"2013/3/31",
@"2014/4/20",
@"2015/4/5",
@"2016/3/27",
@"2017/4/16",
@"2018/4/1",
@"2019/4/21",
@"2020/4/12",
@"2021/4/4",
nil] autorelease] ;
//Easterリストから取得する日付に使用
NSCalendarDate *compDate ;
//出力日付用のフォーマットyyyy/m/d形式
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]
initWithDateFormat:@"%1Y/%1m/%1d" allowNaturalLanguage:YES] autorelease];
//出力する日付に使用
NSString *formattedDateString = nil ;
//Easterリストを廻す添字
int i;
//Easterリストの最大値、iの上限
int max = [easterday count] ;
//出力用の文字列
NSString *message;
//Easterリストを始めからループ
for ( i = 0; i < max;i++) {
//Easterリストから日付を取得→compDate
compDate = [NSDate dateWithNaturalLanguageString:[easterday objectAtIndex:i]];
//入力日付とEasterリストの年を比較
if ( [inputDate yearOfCommonEra] < [compDate yearOfCommonEra])
{
//Easterリストの方が大きい場合、
//compdataが次のEasterの日となるのでformattedDateStringにセット
formattedDateString = [dateFormatter stringFromDate:compDate];
break;
//入力日付とEasterリストのt月日を比較(年は同じ)
} else if ( ([inputDate yearOfCommonEra] == [compDate yearOfCommonEra])
&& ([inputDate dayOfYear] <= [compDate dayOfYear]) )
{
//Easterリストの方が大きい場合、
//compdataが次のEasterの日となるのでformattedDateStringにセット
formattedDateString = [dateFormatter stringFromDate:compDate];
break;
}
}
if (formattedDateString != nil) {
//formattedDateStringがセットされていれば次のEasterとして表示
message = @"Next Easter Day is " ;
message = [message stringByAppendingString:formattedDateString];
[msgField setStringValue:message];
} else {
//formattedDateStringがセットされていない場合は上限エラー
[msgField setStringValue:@"Limit Date is 2021/4/4"];
}
- (IBAction)calcEaster:(id)sender
{
//メイアス・ジョーンズ・ブッチャーのグレゴリオ暦アルゴリズム(wikipedia)
//入力した年→year
int year = [yearField intValue];
int a,b,c,d,e,f,g,h,i,k,l,m,mm,dd;
a=year % 19; b=year / 100; c=year % 100; d=b / 4; e=b % 4; i = c / 4; k = c % 4;
f=( b + 8 ) / 25; g=( b - f + 1 ) / 3; h=( 19 * a + b - d - g + 15 ) % 30;
l = ( 32 + 2 * e + 2 * i - h - k ) % 7; m=( a + 11 * h + 22 * l ) / 451;
mm=( h + l - 7 * m + 114) / 31; dd = (( h + l - 7 * m + 114) % 31) + 1;
//結果を表示
[msgField setStringValue:
[NSString stringWithFormat:@"The EASTER Day is %d/%d/%d",year,mm,dd] ];
}
@end