我能正确理解NSHebrewCalendar吗?

时间:2011-11-20 00:36:42

标签: objective-c cocoa-touch calendar nscalendar

我使用Cocoa Touch进行了快速测试,以了解NSHebrewCalendar的工作原理。我对月份数字特别感兴趣。我使用了日期选择器来轻松更改日期,我将其传递给了记录希伯来日期,希伯来月份数,希伯来年份以及年份是否为闰年的方法。看起来像这样:

BOOL isHebrewLeapYear = [self.calendar isHebrewLeapYear:[calendar hebrewYearForDate:[self.calendar workingDate]]];

NSLog(@"Hebrew Date:%@, Month Number: %i, %i is Leap Year: %i", [self.calendar stringFromHebrewDate:[self.calendar workingDate]], [self.calendar hebrewMonthNumberFromDate:[self.calendar workingDate]], [calendar hebrewYearForDate:[self.calendar workingDate]], isHebrewLeapYear);    

self.calendar对象是一个自定义类。 workingDate属性是NSDate个实例。以下是相关的方法声明。

//  Check if a given year is a leap year
- (BOOL) isHebrewLeapYear:(NSInteger)year{
    return ((7 * year + 1) % 19) < 7;
}

//Get the hebrew year for a given date
- (NSInteger) hebrewYearForDate:(NSDate *)date{
   NSCalendar *hebrewCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar] autorelease];
   return [[hebrewCalendar components:NSYearCalendarUnit fromDate:date] year];
}

- (NSInteger) hebrewMonthNumberFromDate:(NSDate *)date{
    NSCalendar *hebrewCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar] autorelease];
    return [[hebrewCalendar components:NSMonthCalendarUnit fromDate:date] month];
}

显然希伯来语的闰年处理如下:

  • 月份编号从1开始,当月是“Tishri”。
  • 在非闰年,Adar是第7个月,而不是6个月。
  • 在闰年,Adar I排名第6,Adar II排名第7。
  • “Nisan”总是8,依此类推,直到“Elul”,总是13岁。

听起来我的实验是否能产生准确的结果?这种行为记录在哪里吗?

2 个答案:

答案 0 :(得分:3)

  

月份编号从1开始,那个月是“Tishri”。

正确。

  

在非闰年,Adar是第7个月,而不是6个月。

技术上不正确。 (更多内容见下文)

  

在闰年,Adar I排名第6,Adar II排名第7。

正确。

  

“Nisan”总是8,依此类推,直到“Elul”,总是13岁。

正确。

那么Adar在非闰年会怎样?我运行此代码以找出:

@autoreleasepool {

    NSDate *today = [NSDate date];
    NSCalendar *hebrew = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];
    NSDateComponents *diff = [[NSDateComponents alloc] init];
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"d MMMM y"];
    [f setCalendar:hebrew];

    for (NSInteger i = 0; i < 19; ++i) {
        NSDateComponents *comp = [[NSDateComponents alloc] init];
        [comp setYear:5772 + i];
        [comp setDay:1];

        NSLog(@"============= %d ============", [comp year]);

        for (NSInteger i = 1; i <= 13; ++i) {
            [comp setMonth:i];

            NSDate *d = [hebrew dateFromComponents:comp];
            NSLog(@"%d: %@ (%@)", i, [f stringFromDate:d], d);
        }
    }

}

在闰年,这将记录您的期望:

============= 5790 ============
1: 1 Tishri 5790 (2029-09-10 07:00:00 +0000)
2: 1 Heshvan 5790 (2029-10-10 07:00:00 +0000)
3: 1 Kislev 5790 (2029-11-08 08:00:00 +0000)
4: 1 Tevet 5790 (2029-12-07 08:00:00 +0000)
5: 1 Shevat 5790 (2030-01-05 08:00:00 +0000)
6: 1 Adar I 5790 (2030-02-04 08:00:00 +0000)
7: 1 Adar II 5790 (2030-03-06 08:00:00 +0000)
8: 1 Nisan 5790 (2030-04-04 07:00:00 +0000)
9: 1 Iyar 5790 (2030-05-04 07:00:00 +0000)
10: 1 Sivan 5790 (2030-06-02 07:00:00 +0000)
11: 1 Tamuz 5790 (2030-07-02 07:00:00 +0000)
12: 1 Av 5790 (2030-07-31 07:00:00 +0000)
13: 1 Elul 5790 (2030-08-30 07:00:00 +0000)

对于“月”日期组件的每个增量,我们得到不同的日期。但是当我们在非闰年运行时,我们得到了这个:

============= 5789 ============
1: 1 Tishri 5789 (2028-09-21 07:00:00 +0000)
2: 1 Heshvan 5789 (2028-10-21 07:00:00 +0000)
3: 1 Kislev 5789 (2028-11-19 08:00:00 +0000)
4: 1 Tevet 5789 (2028-12-19 08:00:00 +0000)
5: 1 Shevat 5789 (2029-01-17 08:00:00 +0000)
6: 1 Adar 5789 (2029-02-16 08:00:00 +0000)
7: 1 Adar 5789 (2029-02-16 08:00:00 +0000)
8: 1 Nisan 5789 (2029-03-17 07:00:00 +0000)
9: 1 Iyar 5789 (2029-04-16 07:00:00 +0000)
10: 1 Sivan 5789 (2029-05-15 07:00:00 +0000)
11: 1 Tamuz 5789 (2029-06-14 07:00:00 +0000)
12: 1 Av 5789 (2029-07-13 07:00:00 +0000)
13: 1 Elul 5789 (2029-08-12 07:00:00 +0000)

在这里,我们看到有6个月的 7将两者评估为Adar。因此,Adar是非闰年的第6个月和第7个月。


另外,由于我们知道5790年是闰年,我们可以推断出-isHebrewLeapYear:方法的更简单实现:

- (BOOL) isHebrewLeapYear:(NSInteger)year{
    return year % 19 == 14;
}

<击>

答案 1 :(得分:0)

为了避免自己计算闰年的时间,这里是我用来测试它的代码(基于之前的发现,第6个月 7是Adar I在常规年份:

- (BOOL)isRegularYear:(NSDate *)date {
    NSCalendar *hebrew = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];

    NSDateComponents *comp = [hebrew components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];

    [comp setMonth:6];
    NSDate *adar_i = [hebrew dateFromComponents:comp];
    [comp setMonth:7];
    NSDate *adar_ii = [hebrew dateFromComponents:comp];

    return [adar_i isEqualToDate:adar_ii];
}