It is entirely possible that NSCalendar is buggy. I wrote this code 2 or 3 years ago and it has been relatively untested since then.
I just took a quick look at the code and it seems there is no test for the year in -dateFromComponents:. This explains why the year isn't being picked up. I won't have time to look into this until tonight, but the fix seems trivial. On Mon, Jan 21, 2013 at 11:59 PM, Abhi Beckert <[email protected]> wrote: > I'm having a problem with NSCalendar. This is the code I'm using: > > NSCalendar *gregorian = [[NSCalendar alloc] > initWithCalendarIdentifier:NSGregorianCalendar]; > > NSDate *midnightThisMorning = [NSDate date]; > NSLog(@"%@", midnightThisMorning); > > NSDateComponents *comps = [gregorian components:(NSYearCalendarUnit | > NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:midnightThisMorning]; > midnightThisMorning = [gregorian dateFromComponents:comps]; > NSLog(@"%@", midnightThisMorning); > > It works on Mac OS X and iOS. I get: > > 2013-01-22 07:55:08 +1000 > 2013-01-22 00:00:00 +1000 > > Bun on GNUstep I get this: > > 2013-01-22 07:55:08 +1000 > 1970-01-22 00:00:00 +1000 > > For some reason the year (and possibly month?) doesn't get copied across to > the new date. > > Am I doing something wrong? Or is NSCalendar buggy? All I really need is the > UNIX timestamp for the beginning/end of the current day. > > - Abhi > > > > _______________________________________________ > Discuss-gnustep mailing list > [email protected] > https://lists.gnu.org/mailman/listinfo/discuss-gnustep > _______________________________________________ Discuss-gnustep mailing list [email protected] https://lists.gnu.org/mailman/listinfo/discuss-gnustep
