How to parse different files depending on the current date in Xcode? - iphone

I have the following problem: I am building an app that is a TV Guide. I am parsing the listing for the channels from a xml file on the internet. This is my code:
-(void)loadListing
{
NSURL *urlListing = [NSURL URLWithString:#"http://pik.bg/TV/bnt1/29.03.2013.xml"];
NSData *webDataListing = [NSData dataWithContentsOfURL:urlListing];
NSString *xPathQueryListing = #"//elem/title";
TFHpple *parserListing = [TFHpple hppleWithXMLData:webDataListing];
NSArray *arrayListing = [parserListing searchWithXPathQuery:xPathQueryListing];
NSMutableArray *newArrayListing = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in arrayListing)
{
Listing *shows = [[Listing alloc] init];
[newArrayListing addObject:shows];
shows.broadcast = [[element firstChild] content];
}
_shows = newArrayListing;
[self.tableView reloadData];
}
Look at the first row - my file's name is /.../01.04.2013.xml
Tomorrow's file will be /.../02.04.2013.xml and etc.
How to make it parse different files depending on the current date? Like this: today parses /.../01.04.2013, tomorrow will parse /.../02.04.2013 and etc. ? Thanks in advance!

First of all, Get today's Date with the same format used in URL. (You have to play with separate date, month and year components)
Then, Convert that date into a NSString object
Form a NSString like NSString *strToDay = [NSString
stringWithFormat:#http://pik.bg/TV/bnt1/%#.xml",strToDay];
Use the string into the NSURL, like;
NSURL *urlListing = [NSURL URLWithString:strToDay];
NOTE This solution will work only if your URL contains the date format as specified by you.

You could use an NSDateFormatter, property configured, to generate a string of the appropriate format. Use the NSDate instance returned by [NSDate date] to get today's date, and use the formatter to generate the string. Finally, insert the string representation of the date into the URL string and build an NSURL from it.
// Assuming the TV schedule is derived from the Gregorian calendar
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Use the user's time zone
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
// Instantiate a date formatter, and set the calendar and time zone appropriately
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setCalendar:gregorianCalendar];
[dateFormatter setTimeZone:localTimeZone];
// set the date format. Handy reference here: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
[dateFormatter setDateFormat:#"dd.MM.yyyy"];
// [NSDate date] returns a date corresponding to 'right now'.
// Since we want to load the schedule for today, use this date.
// stringFromDate: converts the date into the format we have specified
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
// insert the date string into the URL string and build the URL
NSString *URLString = [NSString stringWithFormat:#"http://pik.bg/TV/bnt1/%#.xml", dateString];
NSURL *URL = [NSURL URLWithString:URLString];
NSLog(#"URL = %#", URL);

Related

How do I format a date to a string including milliseconds?

NSString *nameToSave = word;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *fromDateString=[dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
nameToSave = [word stringByAppendingString:fromDateString];
nameToSave = [nameToSave stringByAppendingString:#".txt"];
NSLog(#"Server notes name .............>>>>>>!!!! %#",nameToSave);
for saving filenames i am using dateformatter, my problem is in the above code word is same for two files and it is run within one second then two file names are same. So the files are overriding.., i think with use of milliseconds i can solve my problem, But i don't know how to get current time in milliseconds. [ seconds/1000 or any arithmetic operations is not helpful for me..., because after applying arithmetic operation also we get same name]
You can set a date format like
[dateFormatter setDateFormat:#"yyyy-MM-dd-HH:mm:ss:SSS"];
"SSS" is for milliseconds.
just do like this [[NSDate date] timeIntervalSince1970];

nsdate from string works only with date not time

im using a string to convert it to an nsDate
is working fine,
but when i try to add time to my string I get null on the logs
NSString *dateStringus = [NSString stringWithFormat:#"12-09-2011"];
NSLog(#"%#",dateStringus);
NSDateFormatter *dateFormattero = [[NSDateFormatter alloc] init];
[dateFormattero setDateFormat:#"dd-MM-yyyy"];
NSDate* d = [dateFormattero dateFromString:dateStringus];
NSLog(#"tomad tu string date ::%#",d);
NSString *pingus = [dateFormattero stringFromDate:d];
NSLog(#"transumatada ::%#", pingus);
NSLog(#"\n");
Works fine for only date,
but
if I do
NSString *dateStringus = [NSString stringWithFormat:#"12-09-2011 14:14:00"];
I get null on the logs
how to fix this string to include time? thanks!
The dateFormat property of the NSDateFormatter dateFormattero must match the format of date/time in the string dateStringus.
Change:
[dateFormattero setDateFormat:#"dd-MM-yyyy"];
Into:
[dateFormattero setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
Here is a good reference on date format patterns.

UIImage from a URL that change by date

my question is simple, i want to load a UIImage from an URL, but this URL change programmatically by date.
Example Today the url is http://www.abc.com/2011-10-13/alfa.jpg
tomorrow is http://www.abc.com/2011-10-14/alfa.jpg
the only thing that change is the date part, how can i figure my app load that "alfa.jpg" at current date everytime i start it?
Thanks!
You should use [NSDate date] which returns the current date and time according to the device time (assuming your device time doesn't differ from the actual by more than a day). Then you should format the date according to your url. Something like-
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSString* dateString = [formatter stringFromDate:[NSDate date]];
[formatter release];
Use an NSDateFormatter to generate the part of the string that varies with time.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString * datePart = [dateFormatter stringFromDate:[NSDate date]];
NSString * theURLString = [NSString stringWithFormat:#"http://www.abc.com/%#/alfa.jpg", datePart];
You will have to maintain a mechanism to check whether if the image for the day has been downloaded to avoid downloading it again.
Create a template of the url you are using nstring and a DateFormatter object...
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"yyyy-MM-dd"];
NSString *todayStr = [df stringFromDate:[NSDate date]];
NSString *todayUrl = [NSString stringWithFormat:#"http://www.abc.com/%#/alfa.jpg", todayStr];
todayUrl has the url for today!
Its an easy task just format your URL string using current date like,
NSString *URLString = [[NSString alloc] initWithFormat:#"http://www.abc.com/%#/alfa.jpg", currentDate];
Then use this string for URL request.

iphone NSDate Problem

Following code I can use for storing the value of UILabel into a string. And Value of string is store into NSDate.
NSString *star = [[NSString alloc]init];
star = lbtInDate.text;
NSString *end = [[NSString alloc]init];
end = lblOutDate.text;
NSDateFormatter *dateFormatter1=[[NSDateFormatter alloc]init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *sdate= [dateFormatter1 dateFromString:star];
NSDate *edate=[dateFormatter1 dateFromString:end];
but the value of sdate and edate is shows nil. so please help how can i store the value of UILabel into NSDate for compare.
Your code is perfectly valid only the lbtInDate.text; and lblOutDate.text; respect the date format. If not your NSDate's will be null.
If lblOutDate.text; is something like #"2002-12-23", your date format should be #"yyyy-MM-dd" and so on.
Data Formatting Guide

how to insert NSDate in sqlite?

HELLO i am trying to insert Current date in my database table
i have a column dateVisited of type datetime
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy/MM/dd HH:mm:ss"]; //this is the sqlite's format
NSDate *stringTime = [NSDate date];
NSString *formattedDateStringTime = [formatter stringFromDate:stringTime];
sqlStr = [NSString stringWithFormat:#"Insert into AnwaltHistory (id,dateVisited) values (%d,'%#')",Id formattedDateStringTime];
NSLog(#"%#",sqlStr);
BOOL Res = [appDelegate InsUpdateDelData: sqlStr];
if (Res)
{
NSLog(#"The Following instrunction is excuted successfully !");
}
else
{
NSLog(#"The Following instrunction Failed to execute !");
}
the statement executes successfully but when i actually go and check the table it says invalid date... i dont know why
as i have printed sqlstr i copy paste from the console and paste it in sqlite and run it manually it runs perfectly fine and has the actual date....
can any one guide me where i am mistaken :S.......
please
Why not use:
"Insert into AnwaltHistory (id,dateVisited) values (%d,datetime())",Id
You could try this:
double dateVisited = [[NSDate date] timeIntervalSince1970]; //now
// save dateVisited to your 'dateVisited' field
According to the official documents, the DateTime on SQLITE should be in ISO8601 format (AKA: a string of ("YYYY-MM-DD HH:MM:SS.SSS"))
Therefore, to store the date, you should format your date string, below is a code to help you with this:
NSDate *SourceDate = [NSDate date]; // Or whatever NSDATE you like...
NSDateFormatter *MyDateFormatter = [[NSDateFormatter alloc] init];
[MyDateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSString *SQLiteQuery = [NSString stringWithFormat:#"Insert into YourTable(YourDateField) values ('%#')", [MyDateFormatter stringFromDate:SourceDate]];
This should do it, give's a shout if you need more help,
Cheers
H