Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
-(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 .

share|improve this question
    
If the code is not working, the question does not belong here in CodeReview. Perhaps a moderator can move it to StackOverflow. –  ANeves Dec 6 '12 at 13:34
    
@ANeves my code is working , but i was not sure of the logic i had used, now i got what i am supposed to use and below is the answer –  iMeMyself Dec 10 '12 at 9:56
    
@iMeMyself Accept the answer. –  Zaph Jan 13 '13 at 13:56
add comment

1 Answer

up vote 1 down vote accepted

following are the code changes done in order to get proper time difference in LocalTime Zone //Get Local TimeZone NSTimeZone* localTimeZone = [NSTimeZone localTimeZone]; NSString* localAbbreviation = [localTimeZone abbreviation]; // To transform it back from the localAbbreviation, is just a matter to re-create the timeZone:

 NSTimeZone* timeZoneFromAbbreviation = [NSTimeZone timeZoneWithAbbreviation:localAbbreviation];
 NSString* timeZoneIdentifier = timeZoneFromAbbreviation.name;
 DebugLog(@"Identifier: %@", timeZoneIdentifier);

 // get the current date in LocalTimeZone
 NSDate *date = [NSDate date];
 DebugLog(@"system date in GMT %@",date );
 NSTimeZone *tz = [NSTimeZone defaultTimeZone];
 NSInteger seconds = [tz secondsFromGMTForDate: date];
 date = [NSDate dateWithTimeInterval: seconds sinceDate: date];
 DebugLog(@"system date  in Local time zone %@",date );


 //convert string from webService to NSDate
 NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
 [dateFormat2 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:timeZoneIdentifier]];
 [dateFormat2 setDateFormat:@"yyyy-MM-dd HH:mm:ss.0"];
 NSDate *dateFromWebServiceString = [[NSDate alloc] init];
 dateFromWebServiceString = [dateFormat2 dateFromString:timeStamp];
 DebugLog(@"webserver time in date as is %@",dateFromWebServiceString);



 //Calculate difference in WebService Time and System Time

 NSTimeInterval timeDifference = [date timeIntervalSinceDate:dateFromWebServiceString];
 DebugLog(@"%f",timeDifference);*/


if(timeDifference < 0)  // Server time is 2 minutes ahead of device time.   So as soon as post is done it gives -2 minutes ago instead of 0 minutes ago
{
    timeDifference = 0;
}

int iminutes = timeDifference / 60;
int ihours = iminutes / 60;
int idays = iminutes / 1440;

iminutes = iminutes - ihours * 60 ;
ihours = ihours - idays *24 ;// this ives correct no of days than the looping in while several times


DebugLog(@"%d, %d, %d", idays, ihours, iminutes);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.