NSTimeInterval to minutes and seconds

I have an integer in seconds, that when converted to minutes is less than 60 minutes (So no need to worry about hours). I want to convert the seconds string to a format like this:
int seconds = 352;


NSString *time = @"05:52";

NOTE: minutes must have the 0 in front of it if its less than 10 minutes. Same for seconds obviously :)

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I guess you have to write something yourself, if you do not find a special class for handling that stuff. May be that OpenGroupware has support in that area.

#include <Foundation/Foundation.h>
NSString *secondsToMinutesAndSeconds(long seconds) {
  /* check that the seconds are less than a day */
  NSString *result;
  div_t minutesAndSeconds;
  if (seconds > 60 * 24 
     || seconds < 0) {
    NSLog(@"Time in seconds is is out of bounds\n");
    return nil;
  minutesAndSeconds = div(seconds, 60);
  result = [NSString stringWithFormat: @"%0.2d:%0.2d", minutesAndSeconds.quot,
                     minutesAndSeconds.rem ];
  return result;
  id pool = [[NSAutoreleasePool alloc] init];
  int seconds = 352;
  NSString *formatted_time = secondsToMinutesAndSeconds(seconds);
  NSLog(@"%d seconds are %@ (min:sec)\n", seconds, formatted_time);
  // The end...
  [pool release];
  return 0;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TheMaximumWeaselAuthor Commented:
Is it possible you could make that into a c function that uses no objects except for the NSString that is returned? I know the amount of seconds will not be over a day.
I can not see why that could be a requirement. You have to create the NSString somehow. So why not using is as I did? However you can use any C function like e.g

char formatted_string_buf[20];

sprintf("formatted_string_buf, "%0.2d:%02d", minutesAndSeconds.quot, minutesAndSeconds.rem);
then you create the NSString from the C buffer.

Ok, let me clarify it. You question was fully and AFAIKT correctly answered, now you can accept the solution or not. If you like to get another answer then state precicly what can and what can not be used, but in another question.

TheMaximumWeaselAuthor Commented:
Sorry about the slow response I hadn't got around to checking the question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.