Time profiler say that my code to expressive in memory and I see lags while scrolling tableView.
How can I replace this code?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm:ss"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[_duration doubleValue]];
return [dateFormatter stringFromDate:date];
_duration have value 123
I need string from _duration like 2:03 sec
Two options:
1) Create the date formatter once and reuse it.
2) Don't use a date formatter. There's no need. It's trivial to convert your duration to minutes and seconds.
int duration = [_duration intValue];
int mins = duration / 60;
int secs = duration % 60;
return [NSString stringWithFormat:#"%02d:%02d", mins, secs];
Related
I'm trying to get timeIntervalSinceDate to send a log in secs of how much time has elapsed since a previous date but it isn't working. I set it for 1 minute in the future each time I test it but I not get logs that correspond like the latest log was this: 2015-09-22 16:30:04.469 Wakey Wakey[49785:7077539] Seconds nan 2015-09-22 16:30:04.470. This is the code:
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date ];
NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd:MM:YYYY:ss:mm:hh"];
NSDate *dateTimeSeconds = [[NSDate alloc] init];
dateTimeSeconds = [dateFormatter1 dateFromString:dateTimeString];
NSTimeInterval secs = [currentTime timeIntervalSinceDate:dateTimeSeconds];
NSLog(#"Seconds %g", secs);
What should I do to fix it?
Why bother with NSDateFormatter. Try
NSTimeInterval seconds = [[NSDate date] timeIntervalSinceDate:dateTimePicker.date];
NSLog(#"seconds %.f", seconds);
Also use %.f instead of %g on the NSLog
Try with this
NSDate* date1 = someDate;
NSDate* date2 = someOtherDate;
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
Please try with below date format
kTimeStampFormat #"dd-MM-yyyy'T'HH:mm:ss.SSS'Z'"
Use UTC format for consistency
I want to convert milliseconds elapsed since Jan 1 1970 to NSDate without loosing milliseconds. Every solution says to divide milliseconds by 1000 and use dateWithTimeIntervalSince1970. but I want to keep the preserve the to milliseconds as well.
The parameter to dateWithTimeIntervalSince1970 is a NSTimeInterval, which is not an integer value (it's a double). There's no reason to lose milliseconds. Just don't use integers when you perform your division.
For example:
long long milliseconds = 1576058147753;
NSTimeInterval seconds = (NSTimeInterval)milliseconds / 1000.0;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
[formatter setLocalizedDateFormatFromTemplate:#"dMMMMyyyyHHmmssSSS"];
December 11, 2019, 01:55:47.753
Note the milliseconds of 753 are there.
You can get date in this way.You have to pass timeinterval and date format as per your requirement.
-(NSString *)getStringFromDate:(double)interval withFormat:(NSString*)format
{
if (interval == 0)
{
return #"";
}
double seconds = interval;
NSTimeInterval timeInterval = (NSTimeInterval)seconds;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter* df_utc = [[NSDateFormatter alloc] init];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df_utc setDateFormat:format];
NSString *Date=[df_utc stringFromDate:date];
return Date;
}
Hope this will help you.
I'm using AVPlayer. And I want to get current time of player to show for user.
The problem is the string getting wrong value from NSDate you can see in images
This is date value
And this string value getting from date
please help to solve. this is my code
NSDate* d = [NSDate dateWithTimeIntervalSince1970:CMTimeGetSeconds(mPlayer.currentTime)];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[dateFormatter setDateFormat:#"mm:ss"];
NSString* result = [dateFormatter stringFromDate:d];
avPlayerLabel.text = result;
You don't even need an NSDateFormatter:
Float64 time = CMTimeGetSeconds(mPlayer.currentTime) + 0.5;
unsigned minutes = (unsigned)time / 60;
unsigned seconds = (unsigned)time % 60;
avPlayerLabel.text = [NSString stringWithFormat:#"%02u:%02u", minutes, seconds];
I get a Unix timestamp (Created at time) from server of which I get the NSDate object using :
NSTimeInterval interval = [str doubleValue];
NSDate *timeStamp = [NSDate dateWithTimeIntervalSince1970:interval];
I need to find the time difference between the above created time and current time and display in hh:mm:ss format. I coded it :-
NSTimeInterval timeDiff = [agent.chatStartTimeStamp timeIntervalSinceNow];
// NSDate *now = [NSDate date];
// timeDiff = [now timeIntervalSinceDate:agent.chatStartTimeStamp]; // RETURNS NEGATIVE
// Divide the interval by 3600 and keep the quotient and remainder
div_t h = div(timeDiff, 3600);
int hours = h.quot;
// Divide the remainder by 60; the quotient is minutes, the remainder
// is seconds.
div_t m = div(h.rem, 60);
int minutes = m.quot;
int seconds = m.rem;
NSString *str = [NSString stringWithFormat:#"%d:%d%d", hours, minutes, seconds];
cell.timeLabel.text = str;
NSLog(#"*********** CV CTRL - AGENT CHATSTARTTIME - %# TIME DIFFERNCE = %f STR = %#", agent.chatStartTimeStamp, timeDiff, str);
The logs for the above 2 codes -
AGENT CHATSTART TIME - 1403342129.980000 SET TIME - 2014-06-21 09:15:29 +0000
*********** CV CTRL - AGENT CHATSTARTTIME - 2014-06-21 09:15:29 +0000 TIME DIFFERNCE = 130.419857 STR = 0:210
The above code gives me results as - suppose the value is 0:2:10, then this value reduces to 0:1:40, 0:1:6, 0:-1....
What I am looking out is - the time difference should increase as the created at time will be something before/earlier current time only. So I want that value of startTime should be deducted from now i.e. now - startTime (time). And I believe this will give me results as I am expecting. I tried with [now timeIntervalSinceDate:agent.chatStartTimeStamp]; but that returns negative response.
UPDATE
This is how I convert the unix timestamp to loca time :-
+ (NSDate *)getNSDateFromUnixTimeStamp : (NSString *) unixTime {
NSString *str = [NSString stringWithFormat:#"%f",[unixTime doubleValue]/(double)1000];
NSTimeInterval interval = [str doubleValue];
NSDate *timeStamp = [NSDate dateWithTimeIntervalSince1970:interval];
str = nil;
unixTime = nil;
return timeStamp;
}
And log for the same :-
2014-06-23 12:24:38.046 MintChat[1021:70b] AGENT CHATSTART TIME - 1403506610.771000 SET TIME - 2014-06-23 06:56:50 +0000
My system time is 12:24:38 & the unixtimestamp is also the current time at just few secs before, so I guess shouldn't the unix time set should also have time as almost same.
Can anyone help me how to get this simple time difference. Where am I going wrong ? I searched a lot on the subject, but couldn't get the expected results.
Any help is highly appreciated.
Thanks
Your commented code, [now timeIntervalSinceDate:agent.chatStartTimeStamp]; is correct. From the NSDate documentation -
Return Value
The interval between the receiver and the current date and time. If the receiver is earlier than the current date and
time, the return value is negative.
So, you can simply take the absolute value to get the number of seconds -
NSTimeInterval timeDiff = fabs([now timeIntervalSinceDate:agent.chatStartTimeStamp]);
Once you have the time interval you can use this answer to format it -
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeDiff];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSString *formattedDate = [dateFormatter stringFromDate:date];
NSLog(#"hh:mm:ss %#", formattedDate);
i am using the following code to convert a value of 18900 into hh:mm:ss
unfortunately the value returned doesn't seem right as it gives me 06:15:00
can anyone please advise why its going wrong
thanks
-(NSString*)setHours:(double)result
{
NSLog(#"result %0.2f",result);
NSDate *date = [NSDate dateWithTimeIntervalSince1970:result];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm:ss"];
NSString *localizedString = [formatter stringFromDate:date];
[formatter release];
return localizedString;
}
Since you have a value that is the number of seconds since midnight, you don't need a date formatter. Try this:
-(NSString*)setHours:(double)result {
int secs = result;
int h = secs / 3600;
int m = secs / 60 % 60;
int s = secs % 60;
NSString *text = [NSString stringWithFormat:#"%02d:%02d:%02d", h, m, s];
}
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
First of all, you have to convert miliseconds in to seconds by dividing miliseconds by 1000.
NSTimeInterval result= 18900/1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:result];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSString *formattedDate=[formatter stringFromDate:date];
NSLog(#"Formatted Date : %#",formattedDate);
It's giving you a result for your current time zone, and the time is specified in UTC. So you're an hour after GMT I suppose.
Check out this question: NSDate dateWithTimeIntervalSince1970 NOT returning GMT/UTC time