Note that there are some explanatory texts on larger screens.

plurals
  1. PONSCalendar problem with BC era
    primarykey
    data
    text
    <p>Greetings,</p> <p>Recently I faced a big problem (as it seems to me) with NSCalendar class.</p> <p>In my task I need to work with a large time periods starting from 4000BC to 2000AD (Gregorian calendar). In some place I was forced to increment some NSDate by 100 year interval. When incrementing the years in AD timeline (0->...) everything worked fine, but when I tried the same thing with BC i was a little confused.</p> <p>The problem is, when you try to add 100 years to 3000BC [edited] year, you get 3100BC [edited] no matter what... Personally i found it strange and illogical. The right result should be 2900BC.</p> <p>Here is the code sample for you to see this "not right" behavior:</p> <pre><code>NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; // initing NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease]; [comps setYear:-1000]; NSDate *date = [gregorian dateFromComponents:comps]; // math NSDateComponents *deltaComps = [[[NSDateComponents alloc] init] autorelease]; [deltaComps setYear:100]; date = [gregorian dateByAddingComponents:deltaComps toDate:date options:0]; // output NSString *dateFormat = @"yyyy GG"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:dateFormat]; NSLog(@"%@", [formatter stringFromDate:date]); </code></pre> <p>What can you say about this behavior? Is this how it should work or is this a bug? I'm confused :S.</p> <p>BTW.: the method [NSCalendar components:fromDate:toDate:options:] doesn't allow us to calculate the difference between years in BC era... additional 'WHY?' in this Pandora's box.</p> <p>P.S.: I was digging through official documentation and other resources but found nothing regarding this problem (or maybe it's intended to work so and I'm an idiot?).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload