Ok so I kind of left my previous post a bit incomplete. I did so because there was that bit of date calculation that I wasn’t yet done resolving.
Remember this bit of code:
int castedInterval = (int)interval; int minutes = (castedInterval / 60) % 60;
Ok so I will be honest I wasn’t that proud of the date calculations. Especially when you take into consideration that the full implementation looked something like:
-(NSString*) stringFromTimeInterval:(NSTimeInterval) interval { int castedInterval = (int)interval; int years = (castedInterval / (((3600 * 24)*7)*52)); int weeks = (castedInterval / ((3600 * 24)*7)); int days = (castedInterval / (3600 * 24)); int hours = (castedInterval / 3600); int minutes = (castedInterval / 60) % 60; int seconds = castedInterval % 60; if (hours >= 24){ hours = hours - (days * 24); } if (days >= 7){ days = days - (weeks * 7); } if(weeks >= 52) { weeks = weeks - (years * 52); } // removed other code }
I shivered every time I read that code especially that god awful mess for determining the number of years. I looked at it a few times and thought to myself ok I could probably clean that up really nice with some constants or some defines.
Ok who am I kidding it still would look crap!
Regardless of it solving the problem I still hate ugly code. Mostly because I hear myself saying what I say to so many other software developers.
In five years when you and I are no longer working here we will be judged by the code we leave behind. For that future developer lets try and make his job easier.
Quick jump to my favorite research tool and five minutes later I come up with NSCalendar
and NSDateComponents
. You can get more information about them here and here.
The cool thing with NSCalendar
is that it has a calculations method called components
that allows me to pass in 2 NSDate
objects to calculate the difference. It then returns back a NSDateComponents
which I can then query to get the information I am looking for.
The following is the result:
-(NSString*) stringFromTimeInterval:(NSDate *) fromDate toDate:(NSDate *)toDate interval:(NSTimeInterval) interval { NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSUInteger flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *dateComponents = [calender components:flags fromDate:[NSDate date] toDate:[self.dueDatePicker date] options:0]; int years = [dateComponents year]; int months = [dateComponents month]; int weeks = [dateComponents week]; int days = [dateComponents day]; int hours = [dateComponents hour]; int minutes = [dateComponents minute]; int seconds = [dateComponents second]; NSString* result = [[NSString alloc] init]; if (years > 0) { result = (years > 1) ? [result stringByAppendingString:[NSString stringWithFormat:@"%d Years ", years]] : [result stringByAppendingString:[NSString stringWithFormat:@"%d Year ", years]]; } if (months > 0) { result = (months > 1) ? [result stringByAppendingString:[NSString stringWithFormat:@"%d Months ", months]] : [result stringByAppendingString:[NSString stringWithFormat:@"%d Month ", months]]; } if (weeks > 0) { result = (weeks > 1) ? [result stringByAppendingString:[NSString stringWithFormat:@"%d Weeks ", weeks]] : [result stringByAppendingString:[NSString stringWithFormat:@"%d Week ", weeks]]; } if (days > 0) { result = (days > 1) ? [result stringByAppendingString:[NSString stringWithFormat:@"%d Days ", days]] : [result stringByAppendingString:[NSString stringWithFormat:@"%d Day ", days]]; } if (hours > 0) { result = (hours > 1) ? [result stringByAppendingString:[NSString stringWithFormat:@"%d Hours ", hours]] : [result stringByAppendingString:[NSString stringWithFormat:@"%d Hour ", hours]]; } if (minutes > 0) { result = (minutes > 1) ? [result stringByAppendingString:[NSString stringWithFormat:@"%d Minutes ", minutes]] : [result stringByAppendingString:[NSString stringWithFormat:@"%d Minute ", minutes]]; } if ((years == 0) && (months == 0) && (weeks == 0)) { result = [result stringByAppendingString:[NSString stringWithFormat:@"%02d Seconds", seconds]]; } return result; }
I am still passing in NSTimeInterval
because I am debating whether I want to include the calculation of milliseconds. I couldn’t find milliseconds as a component so the following bit of code should work if I want:
NSTimeInterval actualTimer = interval; int castedInterval = (int)actualTimer; int millSeconds = ((double)actualTimer - castedInterval) / .01;
Happy coding everyone.