BLOG ARTICLE 요일 | 1 ARTICLE FOUND

  1. 2012.02.04 특정 날짜 요일 구하기

1. C 표준 라이브러리 함수

int getDayOfWeek(int year, int month, int day)
{
struct tm *pResultTime;
struct tm targetTime = { 0, 0, 0, day, month-1, year - 1900 };
time_t targetSec = mktime(&targetTime);
pResultTime = localtime(&targetSec);
return pResultTime->tm_wday;
}

 
2. Cocoa 클래스

참조: NSDateComponents Class Reference

- (int)getDayOfWeek:(int)year month:(int)month day:(int)day

{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setYear:year];
    [dateComponents setMonth:month];
    [dateComponents setDay:day];
    
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *date = [gregorian dateFromComponents:dateComponents];
    NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
    
    return [weekdayComponents weekday] - 1;
}


3.  첼러의 공식

참조: WikiPedia - Zeller's congruence

int getDayOfWeek2(int year, int month, int day)
{
    if(month <= 2) {
        year--;
        month += 12;
    }
    
    int year1 = year/100;
    int year2 = year%100;
    
    int weekDay =  (day + 26*(month+1)/10 + year2 + year2/4 + year1/4 - year1*2) % 7 - 1;
    if (weekDay < 0) {
        weekDay += 7;
    }
    
    return weekDay;


참조: Determination of the day of the week # Sakamoto's method

int dow(int y, int m, int d)
{
   static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};

    y -= m < 3;   
    
    return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;


저작자 표시 비영리 변경 금지
신고