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.
Related
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];
How can I show a countdown in HH:mm:ss format from NOW to a desired NSDate that will happen in the future?
Start at the documentation.
NSDate *future = // whatever
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateCounter:) userInfo:nil repeats:YES];
- (void)updateCounter:(NSTimer *)tmr
{
NSTimeInterval iv = [future timeIntervalSinceNow];
int h = iv / 3600;
int m = (iv - h * 3600) / 60;
int s = iv - h * 3600 - m * 60;
aUILabel.text = [NSString stringWithFormat:#"%02d:%02d:%02d", h, m, s];
if (h + m + s <= 0) {
[tmr invalidate];
}
}
You have to use a timer for ticking the date.
Store a future date, and keep on substracting future date - [nsdate today]...
Calculate the time in seconds and calculate it into Hours, minutes, seconds...
//Make two properties NSDate *nowDate, *futureDate
futureDate=...;
nowDate=[NSDate date];
long elapsedSeconds=[nowDate timeIntervalSinceDate:futureDate];
NSLog(#"Elaped seconds:%ld seconds",elapsedSeconds);
NSInteger seconds = elapsedSeconds % 60;
NSInteger minutes = (elapsedSeconds / 60) % 60;
NSInteger hours = elapsedSeconds / (60 * 60);
NSString *result= [NSString stringWithFormat:#"%02ld:%02ld:%02ld", hours, minutes, seconds];
This will come handy for you...kindly check the project...
Give this code a shot:
NSTimer* timer= [NSTimer scheduledTimerWithInterval: [self.futureDate timeIntervalSinceNow] target: self selector: #selector(countdown:) userInfo: nil, repeats: YES];
The countdown: method:
- (void) countdown: (NSTimer*) timer
{
if( [self.futureDate timeIntervalSinceNow] <= 0)
{
[timer invalidate];
return;
}
NSDateComponents* comp= [ [NSCalendar currentCalendar] components: NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit startingDate: [NSDate date] toDate: self.futureDate options: 0];
NSLog(#"%lu:%lu:%lu", comp.hour,comp.minute.comp.second);
}
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;
}
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.
I have the following so far, but can't figure out a tidy way to get the direction letters in without a bunch of messy if statements. Any ideas? Ideally I'd like to extend the CLLocation class with a category to do this.
-(NSString *)nicePosition{
double latitude = [self.latitude doubleValue];
double longitude = [self.longitude doubleValue];
int latSeconds = (int)round(latitude * 3600);
int latDegrees = latSeconds / 3600;
latSeconds = abs(latSeconds % 3600);
int latMinutes = latSeconds / 60;
latSeconds %= 60;
int longSeconds = (int)round(longitude * 3600);
int longDegrees = longSeconds / 3600;
longSeconds = abs(longSeconds % 3600);
int longMinutes = longSeconds / 60;
longSeconds %= 60;
//TODO: Use N,E,S,W notation in lat/long
return [NSString stringWithFormat:#"%i° %i' %i\", %i° %i' %i\"", latDegrees, latMinutes, latSeconds, longDegrees, longMinutes, longSeconds];
}
For the record I did the following.
-(NSString *)nicePosition{
double latitude = [self.latitude doubleValue];
double longitude = [self.longitude doubleValue];
int latSeconds = (int)round(abs(latitude * 3600));
int latDegrees = latSeconds / 3600;
latSeconds = latSeconds % 3600;
int latMinutes = latSeconds / 60;
latSeconds %= 60;
int longSeconds = (int)round(abs(longitude * 3600));
int longDegrees = longSeconds / 3600;
longSeconds = longSeconds % 3600;
int longMinutes = longSeconds / 60;
longSeconds %= 60;
char latDirection = (latitude >= 0) ? 'N' : 'S';
char longDirection = (longitude >= 0) ? 'E' : 'W';
return [NSString stringWithFormat:#"%i° %i' %i\" %c, %i° %i' %i\" %c", latDegrees, latMinutes, latSeconds, latDirection, longDegrees, longMinutes, longSeconds, longDirection];
}
Standard way:
char lonLetter = (lon > 0) ? 'E' : 'W';
char latLetter = (lat > 0) ? 'N' : 'S';
Here's some Objective-C based on Daniel's solution above:
- (NSString*)coordinateString {
int latSeconds = (int)(self.latitude * 3600);
int latDegrees = latSeconds / 3600;
latSeconds = ABS(latSeconds % 3600);
int latMinutes = latSeconds / 60;
latSeconds %= 60;
int longSeconds = (int)(self.longitude * 3600);
int longDegrees = longSeconds / 3600;
longSeconds = ABS(longSeconds % 3600);
int longMinutes = longSeconds / 60;
longSeconds %= 60;
NSString* result = [NSString stringWithFormat:#"%d°%d'%d\"%# %d°%d'%d\"%#",
ABS(latDegrees),
latMinutes,
latSeconds,
latDegrees >= 0 ? #"N" : #"S",
ABS(longDegrees),
longMinutes,
longSeconds,
longDegrees >= 0 ? #"E" : #"W"];
return result;
}
Here's a solution in C#:
void Run(double latitude, double longitude)
{
int latSeconds = (int)Math.Round(latitude * 3600);
int latDegrees = latSeconds / 3600;
latSeconds = Math.Abs(latSeconds % 3600);
int latMinutes = latSeconds / 60;
latSeconds %= 60;
int longSeconds = (int)Math.Round(longitude * 3600);
int longDegrees = longSeconds / 3600;
longSeconds = Math.Abs(longSeconds % 3600);
int longMinutes = longSeconds / 60;
longSeconds %= 60;
Console.WriteLine("{0}° {1}' {2}\" {3}, {4}° {5}' {6}\" {7}",
Math.Abs(latDegrees),
latMinutes,
latSeconds,
latDegrees >= 0 ? "N" : "S",
Math.Abs(longDegrees),
longMinutes,
longSeconds,
latDegrees >= 0 ? "E" : "W");
}
This is an example run:
new Program().Run(-15.14131211, 56.345678);
new Program().Run(15.14131211, -56.345678);
new Program().Run(15.14131211, 56.345678);
Which prints:
15° 8' 29" S, 56° 20' 44" W
15° 8' 29" N, 56° 20' 44" E
15° 8' 29" N, 56° 20' 44" E
Hope this helps, and that it does the right thing. Good luck!
If you want to do it in swift you can make something like that:
import MapKit
extension CLLocationCoordinate2D {
var latitudeDegreeDescription: String {
return fromDecToDeg(self.latitude) + " \(self.latitude >= 0 ? "N" : "S")"
}
var longitudeDegreeDescription: String {
return fromDecToDeg(self.longitude) + " \(self.longitude >= 0 ? "E" : "W")"
}
private func fromDecToDeg(input: Double) -> String {
var inputSeconds = Int(input * 3600)
let inputDegrees = inputSeconds / 3600
inputSeconds = abs(inputSeconds % 3600)
let inputMinutes = inputSeconds / 60
inputSeconds %= 60
return "\(abs(inputDegrees))°\(inputMinutes)'\(inputSeconds)''"
}
}
Regarding to Alex's answer, here is a solution in Swift 3 with an output tuple. It returns the coordinates in a tuple.
Furthermore, it really extends the class CLLocationDegrees and doesn't require an extra parameter.
import MapKit
extension CLLocationDegrees {
func degreeRepresentation() -> (northOrEast: Bool, degrees: Int, minutes: Int, seconds: Int) {
var inputSeconds = Int(self * 3600)
let inputDegrees = inputSeconds / 3600
inputSeconds = abs(inputSeconds % 3600)
let inputMinutes = inputSeconds / 60
inputSeconds %= 60
return (inputDegrees > 0, abs(inputDegrees), inputMinutes, inputSeconds)
}
}
int latSeconds = (int)round(abs(latitude * 3600));
This is mistake! Proper is
int latSeconds = abs(round(latitude * 3600));