كيفية إنشاء تاريخ محدد في الماضي البعيد ، عصر كولومبيا البريطانية
-
01-10-2019 - |
سؤال
أحاول إنشاء موعد في عصر BC ، لكنني فشلت بشدة. يعود التالي "4713" بالسنة ، بدلاً من "-4712":
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [NSDateComponents new];
[components setYear: -4712];
NSDate *date = [calendar dateFromComponents:components];
NSLog(@"%d", [[calendar components:NSYearCalendarUnit fromDate: date] year]);
أي فكرة عما أفعله خطأ؟
تحديث: رمز العمل
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [NSDateComponents new];
[components setYear: -4712];
NSDate *date = [calendar dateFromComponents:components];
NSDateComponents *newComponents = [calendar components:NSEraCalendarUnit|NSYearCalendarUnit fromDate:date];
NSLog(@"Era: %d, year %d", [newComponents era], [newComponents year]);
هذا يطبع 0 للعصر ، كما أوضح بن.
المحلول
الرمز الخاص بك يعمل في الواقع بشكل جيد. نظرًا لعدم وجود سنة صفر ، فإن -4712 هو العام 4713 قبل الميلاد. إذا قمت بفحص مكون ERA ، فسترى أنه صفر ، والذي يشير في التقويم الغريغوري إلى BC. اقلب تلك العلامة السلبية وسترى 4712 م (عصر 1).
لا تنتمي إلى StackOverflow