-(NSString *)calculateTimeDifference:(NSString *)timeStamp{
// get the current date
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm a"];
// convert it to a string
NSString *dateStringDev = [dateFormat stringFromDate:date];
DebugLog(@"system timeStamp is %@",dateStringDev );
//convert string from webService to NSDate
NSString *dateString = [timeStamp substringToIndex:[timeStamp length] - 5];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm a"];
NSTimeInterval timeInterval = 14;
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:timeInterval]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT-7:58"]];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
DebugLog(@"webserver timeStamp is %@",dateFromString);
//calculate Time Difference
NSTimeInterval timeDifference = [date timeIntervalSinceDate:dateFromString];
int iminutes = timeDifference / 60;
int ihours = iminutes / 60;
int idays = iminutes / 1440;
DebugLog(@"%d, %d, %d", idays, ihours, iminutes);
if (iminutes > 60) {
while (iminutes>60) {
iminutes = iminutes/60;
ihours = ihours+ iminutes% 60;
}
}
if (ihours > 24) {
while (ihours>24) {
ihours = ihours/24;
idays = idays+ ihours% 24;
}
}
if (idays == 0 && ihours == 0){
timeStamp = [NSString stringWithFormat:@"%d minutes ago",iminutes];
}
else
if ((idays == 0 ) && (ihours != 0) ){
timeStamp = [NSString stringWithFormat:@"%d hours %d minutes ago",ihours,iminutes];
}
else if (idays != 0 && ihours != 0){
switch (idays) {
case 1:{
NSArray *arr = [timeStamp componentsSeparatedByString: @" "];
NSString *strSubStringDigNum = [arr objectAtIndex:1];
timeStamp = [NSString stringWithFormat:@"yesterday %@",strSubStringDigNum];
}
break;
default:
{ timeStamp= [timeStamp substringToIndex:[timeStamp length] - 5];
NSArray *arr = [timeStamp componentsSeparatedByString: @" "];
NSString *strSubstrDate = [arr objectAtIndex:0];
NSString *strSubstrHours = [arr objectAtIndex:1];
ihours = strSubstrHours.integerValue;
NSArray *arrMin= [strSubstrHours componentsSeparatedByString: @":"];
NSString *strSubstrMin = [arrMin objectAtIndex:1];
iminutes = strSubstrMin.integerValue;
if (ihours > 12) {
ihours = ihours - 12;
timeStamp = [NSString stringWithFormat:@"%@ %d:%d PM",strSubstrDate,ihours,iminutes ];
break;
}
else
timeStamp = [NSString stringWithFormat:@"%@ %d:%d AM",strSubstrDate,ihours,iminutes];
break;
}
}
}
return timeStamp;
}
timeStamp is my webservice Time as it is in 2012-11-29 06:11:16.0 format i am chopping 5 from last to calculate time difference and i tried with all STD GMT format non gave 0 min difference for present time with GMT -7.58 i got 0 diff , please suggest how to get device Abbreviations and to which format i should convert the webservice string to get proper time difference ..
any help much appreciated .