In my app I want to follow below steps to get the finalized string. My message format is given below
message <br/><br/> On {date_time_stamp} sender_name wrote <br/><br/>
Let say my message string is as below
Hello developers <br/><br/> On {2013-02-28 05:00:33} Bob wrote <br/><br/>
Hello World <br/><br/> On {2013-02-27 08:35:33} Jack wrote <br/><br/>
Hello Apple <br/><br/> On {2013-02-26 04:10:44} Tom wrote <br/><br/>
Now I need to follow below steps
Get the all dates in message one by one
Convert it from GMT time zone to local time zone
Replace GMT dates in message with local date time
Finally replace "breakline with \n" & remove curly braces from message.
I have the below code
-(void)parseMessageBody:(NSString*)msg
{
NSRange openBracket = [msg rangeOfString:@"{"];
NSRange closeBracket = [msg rangeOfString:@"}"];
NSRange numberRange = NSMakeRange(openBracket.location + 1, closeBracket.location - openBracket.location - 1);
NSString *numberString = [msg substringWithRange:numberRange];
NSLog(@"Parsed string: %@",numberString);
}
-(NSString*)formattedMessageFromString:(NSString*)msg
{
NSString *formattedMessage = @"";
formattedMessage = [msg stringByReplacingOccurrencesOfString:@"<br/>" withString:@"\n"];
return formattedMessage;
}
-(NSString*)getLocalDateFromUTCDateString:(NSString*)utcDateString
{
NSDateFormatter *serverDateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[serverDateFormatter setTimeZone:sourceTimeZone];
[serverDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromServer = [serverDateFormatter dateFromString:utcDateString];
DLog(@"UTC date From SERVER: %@",dateFromServer);
NSDateFormatter *localDateFormatter = [[NSDateFormatter alloc] init];
[localDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone* localTimeZone = [NSTimeZone systemTimeZone];
[localDateFormatter setTimeZone:localTimeZone];
NSString *localDate = [localDateFormatter stringFromDate:dateFromServer];
DLog(@"Converted Local DAte : %@",localDate);
return localDate;
}
I am not able perform steps 1 & 3 to get all the dates in message & replace it with local date time. Also message can grow over the time. So parsing needs to be fast. Can anybody tell me how can I get all the dates in message & replace it with converted date ?
Any kind of help is highly appreciated. Thanks in advance.
Finding and replacing the dates can be done with regular expressions:
NSString *msg = @"Hello developers <br/><br/> On {2013-02-28 05:00:33} Bob wrote <br/><br/>\n"
"Hello World <br/><br/> On {2013-02-27 08:35:33} Jack wrote <br/><br/>\n"
"Hello Apple <br/><br/> On {2013-02-26 04:10:44} Tom wrote <br/><br/>\n";
NSMutableString *replacedMsg = [msg mutableCopy];
NSString *pattern = @"\\{(.+?)\\}"; // Pattern for { ... }
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:NULL];
__block int offset = 0;
[regex enumerateMatchesInString:msg options:0 range:NSMakeRange(0, [msg length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange range0 = [result range]; // range including { }
NSRange range1 = [result rangeAtIndex:1]; // range excluding { }
range0.location += offset;
range1.location += offset;
NSString *oldDate = [replacedMsg substringWithRange:range1];
NSString *newDate = [self getLocalDateFromUTCDateString:oldDate];
if (newDate != nil ) {
[replacedMsg replaceCharactersInRange:range0 withString:newDate];
offset += [newDate length] - range0.length;
}
}];
NSLog(@"%@", replacedMsg);
Output (my local timezone is GMT+01):
Hello developers <br/><br/> On 2013-02-28 06:00:33 Bob wrote <br/><br/>
Hello World <br/><br/> On 2013-02-27 09:35:33 Jack wrote <br/><br/>
Hello Apple <br/><br/> On 2013-02-26 05:10:44 Tom wrote <br/><br/>