Re: How to Display a Time Interval


Steve Mills
 

On May 20, 2019, at 04:26:31, Dave <dave@...> wrote:

As far as I know I don’t have to worry about leap years, get the Interval between now and some future date, this includes all the leap years in between. I am displaying it at the moment by dividing by the number of seconds in a year, week, hours and minute which is accurate enough for this applications.

Here is a method to do it:


#define kLTWTimeSecondsPerMinute 60
#define kLTWTimeMinutesPerHour 60
#define kLTWTimeSecondsPerHour (kLTWTimeSecondsPerMinute * kLTWTimeMinutesPerHour)
#define kLTWTimeHoursPerDay 24
#define kLTWTimeDaysPerWeek 7
#define kLTWTimeWeeksPerYear 52

#define kLTWTimeSecondsPerDay (kLTWTimeSecondsPerHour * kLTWTimeHoursPerDay)
#define kLTWTimeSecondsPerWeek (kLTWTimeSecondsPerDay * kLTWTimeDaysPerWeek)
#define kLTWTimeSecondsPerYear (kLTWTimeSecondsPerWeek * kLTWTimeWeeksPerYear)
*snip*

I'm not sure why you needed to reinvent the wheel when NSCalendar and NSDateComponents already does this via:

-(NSDateComponents*) components:(NSCalendarUnit)unitFlags fromDate:(NSDate*)startingDate toDate:(NSDate*)resultDate options:(NSCalendarOptions)opts;

All you need to do is format the string, like you did. Plus, you didn't display months, and most people would want to see that rather than having to do math in their head.

--
Steve Mills
Drummer, Mac geek

Join cocoa@apple-dev.groups.io to automatically receive all group messages.