I'm creating an app for playing a ringtone and I'd want to know the current time in milliseconds of the played ringtone every time.
CMTime cTime = player_.currentTime;
float currentTime = cTime.value / cTime.timescale;
That currentTime here gets the value in seconds.
How can I get the exact currentTime value but in milliseconds?
There is a way to get current times in seconds, you can take 1000 times that and ther you have your miliseconds:
Float64 dur = CMTimeGetSeconds([player currentTime]);
Float64 durInMiliSec = 1000*dur;
Sadly you will have to use it as a float or Float64 you can't use that result as a CMTime
I think that the accepted answer loses detail. A more accurate way to get milliseconds would be the following:
let seconds : Double = Float64(time.value * 1000) / Float64(time.timescale)
If you convert to seconds first you'd lose precision.
CMTime.value and CMTime.timescale are both integers, so judging by your code, the result gets rounded and you don't get a precise timestamp. Try CMTimeGetSeconds instead.
Related
Is there a method in matlab to convert seconds from a known date to a standard date time format?
For example, if I have a vector of values shown as seconds from 1901/01/01, how would I convert them to a dateTime? In this case a value of 28125 would correspond to 1981/01/01. Is there an efficient method for doing this?
The numbers in your example do not make sense so it is not clear if your time is in seconds or days but since you asked for seconds I will use this.
What you want to achieve can be done using datenum function. This function returns the number of (fractional) days from 1/1/0000. So first you need to find your offset, e.g.:
offsetInDays = datenum(1901,1,1);
Next, you convert the date from seconds to days:
dateInDays = YourRequiredDateInSec * 3600 * 24;
Finally, you date is given by
RequiredDate = datestr(offsetInDays + dateInDays);
I have set a NSTimer.scheduledTimerWithTimeInterval method which has an interval every 20 minutes. I want to be able to find out how much time is left when the the app goes into background mode. How do I find out how much time is left from the interval?
Thanks
You have access to a NSTimer's fireDate, which tells you when the timer is going to fire again.
The difference between now and the fireDate is an interval you can calculate using NSDate's timeIntervalSinceDate API.
E.G. something like:
let fireDate = yourTimer.fireDate
let nowDate = NSDate()
let remainingTimeInterval = nowDate.timeIntervalSinceDate(fireDate)
When using the Solution of #Michael Dautermann with normal Swift type Timer and Date I have noticed, that his solution will give you a negative value e.g.:
let fireDate = yourTimer.fireDate
let nowDate = Date()
let remainingTimeInterval = nowDate.timeIntervalSinceDate(fireDate)
//above value is negative e.g. when the timers interval was 2 sec. and you
//check it after 0.5 secs this was -1,5 sec.
When you insert a negative value in the Timer.scheduledTimer(timeInterval:,target:,selector:,userInfo:,repeats:) function it would lead to a timer set to 0.1 milliseconds as the Documentation of the Timer mentions it.
So in my case I had to use the negative value of the nowDate.timeIntervalSinceDate(fireDate) result: -nowDate.timeIntervalSinceDate(fireDate)
How can I convert an integer such as 115900 to a time? I'd like to do arithmetic operations on times so that something like: 115900 + 100 will equal 120000, rather than 11600.
Your big problem is that an integer number does not behave like a date/time. Since you are using Objective-C, you really should be using the NSDate class and the associated classes for formatting dates and times and managing calendars.
Start by reading the Date and Time Programming Guide. That will be better than me writing it all out again.
int seconds = 115900 % 60;
int minutes = (115900 / 60) % 60;
int hours = 115900/ 3600;
return [NSString stringWithFormat:#"%02i:%02i:%02i",hours, minutes, seconds];
//output like is HH:MM:SS
I did not really know on how to title this question so hopefully you've find the way in :)
My Problem is:
I wanted to set a clock time for a label with a UISlider.
So basically my slider min value is 0000 and the max value is 2400. (24 hour format)
So how do I achieve a properly formatted clock?
For example if my slider's value is at (1161)11:61 it should be (1201)12:01 and so on.
Any tipps for that :)
Would be great to get some help here.
Thanks to all who participate.
why don't you start from 0 to 1440. (24 hours = 1440 minutes) and do something like below.
int hours = slider.value / 60; -> no of hours;
int minutes = slider.value %60; -> no of minutes;
NSString *clock = [NSString stringWithFormat:#"%d : %d", hours, minutes];
You could do this with an NSDateComponents object. Create one, then break up your slider value into two parts: the thousands and hundred digits become the hour, and the tens and ones digits become the minute. You can feed this object to an NSCalendar to transform it into an actual NSDate (if that's what you want).
I am developing Alarm Clock.
I want to compare a time now and setTime. Is it possible to compare in minutes only.
My Problem is NSDate will compare in seconds, for example 9:38:50 is not equal to 9:38:00.
how can I compare in minutes?
First, convert them to UNIX timestamp ignoring milliseconds.
NSUInteger time1 = (NSUInteger)[date1 timeIntervalSince1970];
NSUInteger time2 = (NSUInteger)[date2 timeIntervalSince1970];
Ignore seconds.
time1 = time1 - (time1 % 60);
time2 = time2 - (time2 % 60);
Now, you can safely compare them.
if (time1 == time2) {
NSLog(#"Bingo");
}
Use NSCalendar to get the NSDateComponents you're interested in. Then it's easy to compare those.
Though it might be sufficient to check whether the current date is later than the alarm date. If it is and the alarm wasn't stopped, you should probably go off even if the computer momentarily lost power or something.