Time in milliseconds to typical time format - objective-c

I need to convert time collected in milliseconds (e.g. 116124) to typical format of time like this: 03:12:32:04.
I don't know how to simple do it... Could you help me?

According to an Apple Dev forum linked here:
https://discussions.apple.com/thread/2350190?start=0&tstart=0
you have to do it yourself. Here is the function you can use that will return a formated string:
- (NSString *) formatInterval: (NSTimeInterval) interval{
unsigned long milliseconds = interval;
unsigned long seconds = milliseconds / 1000;
milliseconds %= 1000;
unsigned long minutes = seconds / 60;
seconds %= 60;
unsigned long hours = minutes / 60;
minutes %= 60;
NSMutableString * result = [NSMutableString new];
if(hours)
[result appendFormat: #"%d:", hours];
[result appendFormat: #"%2d:", minutes];
[result appendFormat: #"%2d:", seconds];
[result appendFormat: #"%2d",milliseconds];
return result;
}

Related

Having milliseconds in my timer in Objective-C

I'm doing a stopwatch using a youtube tutorial. The problem is that I want milliseconds in my timer but the tutorial only shows how to get seconds and minutes. I would like to get the milliseconds displayed like the minutes and seconds, but I've got no idea how to do it.
How to get milliseconds using this code?
#implementation ViewController {
bool start;
NSTimeInterval time;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.display.text = #"0:00";
start = false;
}
- (void) update {
if ( start == false ) {
return;
}
NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval elapsedTime = currentTime - time;
int minutes = (int) (elapsedTime / 60.0);
int seconds = (int) (elapsedTime = elapsedTime - (minutes * 60));
self.display.text = [NSString stringWithFormat:#"%u:%02u", minutes, seconds];
[self performSelector:#selector(update) withObject:self afterDelay:0.1];
}
According to the docs, "NSTimeInterval is always specified in seconds; it yields sub-millisecond precision over a range of 10,000 years." So all you need to do is extract the milliseconds from your elapsedTime variable and then format your text again so that it includes milliseconds. It might looks something like this:
NSInteger time = (NSInteger)elapsedTime;
NSInteger milliseconds = (NSInteger)((elapsedTime % 1) * 1000);
NSInteger seconds = time % 60;
NSInteger minutes = (time / 60) % 60;
//if you wanted hours, you could do that as well
//NSInteger hours = (time / 3600);
self.display.text = [NSString stringWithFormat: "%ld:%ld.%ld", (long)minutes, (long)seconds, (long)milliseconds];

convert float or double ( lengh of aufiofile ) in minute : seconds millisecond

Hello I tried to find a solution in Objective C to get the lenght of audio file included milliseconds.
at the moment I have only this code:
-(NSString *) DoubleToNSStringTime:(double)valore
{
NSNumber *theDouble = [NSNumber numberWithDouble:valore];
int inputSeconds = [theDouble intValue];
int hours = inputSeconds / 3600;
int minutes = (inputSeconds - hours * 3600 ) / 60;
int seconds = inputSeconds - hours * 3600 - minutes * 60;
NSString *theTime = [NSString stringWithFormat:#"%.2d:%.2d", minutes, seconds];
NSLog(#"TDoubleToNSStringTime= %#", theTime);
return theTime;
}
the Issue is this:
I would like to get more precise timePosition, Minutes : Seconds : Milleseconds
some of you has a solution?
Thanks
You don’t need to convert to NSNumber and back:
- (NSString *)DoubleToNSStringTime:(double)valore
{
NSInteger interval = (NSInteger)valore;
double milliseconds = (valore - interval) * 1000;
NSInteger seconds = interval % 60;
NSInteger minutes = (interval / 60) % 60;
minutes += (interval / 60);
return [NSString stringWithFormat:#"%02ld:%02ld:%3.0f", minutes, seconds, milliseconds];
}
That should print something like 60:45:789 for an input of 3645.789.

NSString time format to int64_t

I have an NSString that represents a time in mm:ss format. How do i convert it in int64_t so i can submit it as a score in game center?
If you wanted to be rigorous you could use an NSDateFormatter to convert to an NSDate then get an NSTimeInterval from that; if you wanted to be more direct then you might try something as simple as:
NSArray *components = [string componentsSeparatedByString:#":"];
if([components count] != 2)
{
// some error condition
return;
}
int64_t totalSeconds = ([[components objectAtIndex:0] integerValue] * 60) +
[[components objectAtIndex:1] integerValue];

How to convert "3:31" mm:ss from string to integer representing seconds [duplicate]

This question already has answers here:
Convert seconds into minutes and seconds
(6 answers)
Closed 8 years ago.
I'm relatively new to programming for iOS using Xcode and Objective-C.
I need to be able to convert the length of a song, for example 3:31 that is a string into an integer representing seconds. so 3:31 (mm:ss) would be 211 seconds, and then back from 211 to 3:31.
Any help to get me started would be appreciated.
To convert the time in seconds to the string you described, you can use the following code:
int songLength = 211;
int minutes = songLength / 60;
int seconds = songLength % 60;
NSString *lengthString = [NSString stringWithFormat:#"%d:%02d", minutes, seconds];
Note the use of 0 in %02d This makes values like 188 transformed into 3:08 instead of 3:8.
You can use NSDateFormatter to get seconds and minutes from the time string:
NSString *timeString = #"3:31";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"mm:ss";
NSDate *timeDate = [formatter dateFromString:timeString];
formatter.dateFormat = #"mm";
int minutes = [[formatter stringFromDate:timeDate] intValue];
formatter.dateFormat = #"ss";
int seconds = [[formatter stringFromDate:timeDate] intValue];
int timeInSeconds = seconds + minutes * 60;
Edit: Adding hours
int songLength = 4657;
int hours = songLength / 3600;
int minutes = (songLength % 3600) / 60;
int seconds = songLength % 60;
NSString *lengthString = [NSString stringWithFormat:#"%d:%02d:%02d", hours, minutes, seconds];
And
NSString *timeString = #"2:3:31";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"hh:mm:ss";
NSDate *timeDate = [formatter dateFromString:timeString];
formatter.dateFormat = #"hh";
int hours = [[formatter stringFromDate:timeDate] intValue];
formatter.dateFormat = #"hh";
int minutes = [[formatter stringFromDate:timeDate] intValue];
formatter.dateFormat = #"ss";
int seconds = [[formatter stringFromDate:timeDate] intValue];
int timeInSeconds = seconds + minutes * 60 + hours * 3600;
You can split the string at :, and calculate the result like this:
NSArray* tokens = [lengthStr componentsSeparatedByString:#":"];
NSUInteger lengthInSeconds = 0;
for (int i = 0 ; i != tokens.count ; i++) {
lengthInSeconds = 60*lengthInSeconds + [[tokens objectAtIndex:i] integerValue];
}
To format the value back, use
[NSString stringWithFormat:#"%d:%02d", lengthInSeconds / 60, lengthInSeconds % 60];
NSString has tons of great methods to help with this type of thing.
You can use componentsSeperatedByString to break up your minutes and seconds
NSArray *listItems = [list componentsSeparatedByString:#":"];
then convert the strings to ints with intValue.
Finally, convert your minutes to seconds and add it all up.
You can get details on all the great things NSString does with the class refernce
(NSString Reference)
This is a method I use in one of my apps to convert a long to mm:ss format:
long seconds = //anything;
NSString *output = [NSString stringWithFormat:#"%02lu:%02lu",seconds/60,seconds-(seconds/60)*60];
If you're using an int you just have to replace lu in the format with i like this:
[NSString stringWithFormat:#"%02i:%02i",seconds/60,seconds-(seconds/60)*60];
The 02 makes sure that the output is always two digits long. 65 seconds will be displayed as 01:05. If you would use %i:%i in the format the output for 65 seconds would look like this: 1:5 and thats not how you want it to look.

conversion to doubleValue doesn't work .. but to integerValue does

Hello all I'm trying to convert a string to a double and it doesn't work.
However if I convert it to an integer it does work
this is my code snippet
int myDuration = [myDurationString integerValue];
int conversionMinute = myDuration / 60;
if( myDuration < 60 )
{
[appDelegate.rep1 addObject:[NSString stringWithFormat:#"%d",myDuration]];
NSLog(#"show numbers %d", myDuration);
}
else
{
[appDelegate.rep1 addObject:[NSString stringWithFormat:#"%d",conversionMinute]];
NSLog(#"show numbers %d", conversionMinute);
}
Now if I try to do
double myDuration = [myDurationString doubleValue];
double conversionMinute = myDuration / 60;
then it doesn't work. It gives me an output of 0.
So the integerconversion works but somehow the double doesn't does anybody have an idea why?
You need to supply a matching format specifier. Replace every occurence of %d with %f.
%d simply fetches the next 32-bit word from the stack and treats it as a signed integer. Because of the internal representation of the floating point number, this word is zero in quite a few cases, which is why you get 0.
Here is an example that works fine for me (also with other contents of myDurationString):
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString * myDurationString = #"19";
double myDuration = [myDurationString doubleValue];
double conversionMinute = myDuration / 60;
if( myDuration < 60 )
{
NSLog(#"show numbers %f", myDuration);
}
else
{
NSLog(#"show numbers %f", conversionMinute);
}
[pool drain];
return 0;
}