Google Xml parser - xcode

can anyone help me in getting the formatted date from the parsed XML.
I can format that using NSXML formatter but now i am found difficulty in using it through Google XMl parser. where i can get the values through the key but i need that in a formatted way,
return [NSDictionary dictionaryWithObjectsAndKeys:
[[[xmlItem elementsForName:#"title"] objectAtIndex:0] stringValue], #"title",
[[[xmlItem elementsForName:#"link"] objectAtIndex:0] stringValue], #"link",
[[[xmlItem elementsForName:#"pubDate"] objectAtIndex:0] stringValue], #"Date",
nil];
here the pubDate is returned as such from xml with hr,min,sec i need the date alone.
Thanks in advance!!!!

I got the answer
//cell.textLabel.text = [item objectForKey:#"pubDate"];
NSString *yourXMLDate = [item objectForKey:#"pubDate"];
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"E, d LLL yyyy HH:mm:ss Z"];
NSDate *inputDate = [inputFormatter dateFromString:yourXMLDate];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"MMM dd, HH:mm a"];
NSString *outputDate = [outputFormatter stringFromDate:inputDate];
cell.textLabel.text=outputDate;
where this results in Oct 12, 12:30 AM. whatever parser used in the xcode this works fine....

Related

How to get current time along with the Timezone in XCode?

I tried this :
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd hh:mm:ss TZD"];
NSString *dateString = [dateFormat stringFromDate:date];
NSLog(#"Current Date : %#",dateString);
and the result I get is Date and Time but not the Timezone.
#Vaibhav you have to use time zone like this
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
refer this link
Get the time and date of selected time zone?

Converting string from xml to NSDate and how to translate the date

This is my data string:
<pubDate>Sun, 24 Mar 2013 00:42:13 +0200</pubDate>
I want to convert this to "Sun, 24/3/13, 00:42"
and to translate the day name to hebrew.
here is my code so far:
currentPubDate = [self.xmlParser.pubDate objectAtIndex:indexPath.row];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *loacle = [[NSLocale alloc]initWithLocaleIdentifier:#"he_IL"];
[dateFormatter setLocale:loacle];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
[dateFormatter dateFromString:currentPubDate];
What should I do to make it work? I've read few guides but without success.
Here is the answer for your first part
NSString *currentPubDate = #"Sun, 24 Mar 2013 00:42:13 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:currentPubDate];
[dateFormatter setDateFormat:#"EEE, dd/M/yy HH:mm"];
NSLog(#"%#", [dateFormatter stringFromDate:date]);
For the translation part you have to set locale, but thats not working in my system as well.

NSDictionary object to NSDate

I need help about this issue.
I would convert a NSString object got by key from a NSMutableDictionary into a NSDate
This is the object from the dictionary
[elemento objectForKey:#"data"]
Here is the entire code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// Time format for the string value
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss +0000"];
NSDate *date = [dateFormatter dateFromString:[elemento objectForKey:#"data"]];
NSLog(#"date => %#", date);
[dateFormatter setDateFormat:#"d MMM yyyy HH:mm:ss Z"];
NSString *dateStr = [dateFormatter stringFromDate:date];
NSLog(#"date modded => %#", dateStr);
`
But from the console I obtain this messages:
2012-08-27 11:51:31.961 iRSSReader[2319:c07] date from dictionary => Thu, 03 May 2012 16:55:02 +0000
2012-08-27 11:51:31.962 iRSSReader[2319:c07] date => (null)
2012-08-27 11:51:31.962 iRSSReader[2319:c07] date modded => (null)
If I try with a NSString value, instead of NSDictionary value, for example
NSString *dateStr = #"Thu, 03 May 2012 16:55:02 +0000"
it works fine.
Thanks in advance
Possible elemento not contains key "data" -> [elemento objectForKey:#"data"] is nil

cocoa: speech and time

I'm building an app that a part of will speak the time. However, when I pass in my date strings (like 10/24/11) to the NSSpeechSynthesizer it will speak them literally, as "one, zero, slash two four slash one one", same with a timestamp, "eight colon one one colon colon", etc. etc.
I looked at the NSSpeechSynthesizer docs and I guess I'd have to work with the phonemesFromText method but that seems like a lot of grunt work to get the app to speak the time and date smoothly. Is there a quicker method?
Thanks
You could try something like this:
#implementation MDAppController
- (id)init {
if ((self = [super init])) {
}
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSDateFormatter *dateParser = [[[NSDateFormatter alloc]
initWithDateFormat:#"%m/%d/%y" allowNaturalLanguage:YES] autorelease];
NSDate *date = [dateParser dateFromString:#"10/24/11"];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSString *string = [dateFormatter stringFromDate:date];
NSLog(#"string == %#", string);
// prints "October 24, 2011"
NSSpeechSynthesizer *alex = [[NSSpeechSynthesizer alloc]
initWithVoice:[NSSpeechSynthesizer defaultVoice]];
[alex setDelegate:self];
[alex startSpeakingString:string];
}
- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender
didFinishSpeaking:(BOOL)finishedSpeaking {
if (finishedSpeaking) [sender autorelease];
}
#end
Basically, this is using 2 NSDateFormatters: one to "translate" the string representation of a date into an actual NSDate object, and then another to translate that NSDate back into a more desirable string representation.
Obviously, you'll need to adjust the dateParser format to fit your expected input string type. (Preferably, you could just use an input date rather than the string representation of it).
Why not use NSDateComponents to and -[NSString stringWithFormat:] construct a spoken sentence as your string, then speak that?

retaining the time zone in NSDateFormatter

I have to set a format string for a DateFormatter to convert a NSString to a NSDate.
The string is in the format: 2011-01-31 12:45:00 +0200 (y-m-d h:m:s timezone)
I'm using the format: #"yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'Z" , but the timezone is converted to +0000 and the hour is adjusted, so a string like:
2011-01-31 12:45:00 +0200 is converted to:
2011-01-31 10:45:00 +0000
The Code is here:
- (NSDate *) dateForString: (NSString *)dateString
{
NSDateFormatter *dateFormatter;
NSLocale *enUSPOSIXLocale;
NSDate *date;
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'Z"];
// [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
date = [dateFormatter dateFromString:dateString];
return date;
}
This is absolutely expected behaviour when using NSDate - NSDate represents a moment in time so in your case the 2 date/times are equivalent.
Just make sure when you display the date using the a dateFormatter you set the timezone on the date formatter to how you want it displayed.
Have a look at setTimeZone: in
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Some more info can also be found here:
NSDate - Convert Date to GMT (maybe duplicate?)

Resources