Iphone sdk中的dateformatter问题

时间:2010-05-12 07:21:02

标签: objective-c

这里我遇到了日期格式化程序的问题,实际上我有一个基于日期搜索事件的选项。为此我将日期与数据库中的日期存储进行比较,并根据我写的日期获取事件代码如下:

-(NSMutableArray*)getSearchAllLists:(EventsList*)aEvent
{
[searchList removeAllObjects];
EventsList *searchEvent = nil;
const char* sql;
NSString *conditionStr = @" where ";
if([aEvent.eventName length] > 0)
{
    NSString *str = @"'%";
    str = [str stringByAppendingString:aEvent.eventName];
    str = [str stringByAppendingString:@"%'"];
    conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" eventName like %s",[str UTF8String]]];
}
if([aEvent.eventWineName length]>0)
{
    NSString *str = @"'%";
    str = [str stringByAppendingString:aEvent.eventWineName];
    str = [str stringByAppendingString:@"%'"];
    if([aEvent.eventName length]>0)
    {
        conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" and (wineName like %s)",[str UTF8String]]];
    }
    else
    {
        conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@"wineName like %s",[str UTF8String]]];
    }
}
if([aEvent.eventVariety length]>0)
{
    NSString *str = @"'%";
    str = [str stringByAppendingString:aEvent.eventVariety];
    str = [str stringByAppendingString:@"%'"];
    if([aEvent.eventName length]>0 || [aEvent.eventWineName length]>0)
    {
        conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" and (variety like %s)",[str UTF8String]]];
    }
    else
    {
        conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@"variety like %s" ,[str UTF8String]]];
    }
}
if([aEvent.eventWinery length]>0)
{
    NSString *str = @"'%";
    str = [str stringByAppendingString:aEvent.eventWinery];
    str = [str stringByAppendingString:@"%'"];
    if([aEvent.eventName length]>0 || [aEvent.eventWineName length]>0 || [aEvent.eventVariety length]>0)
    {
        conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" and (winery like %s)",[str UTF8String]]];
    }
    else
    {
        conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@"winery like %s" ,[str UTF8String]]];
    }
}
if(aEvent.eventRatings >0)
{
    if([aEvent.eventName length]>0 || [aEvent.eventWineName length]>0 || [aEvent.eventWinery length]>0 || [aEvent.eventVariety length]>0)
    {
        conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" and ratings = %d",aEvent.eventRatings]];
    }
    else
    {
        conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" ratings = %d",aEvent.eventRatings]];
    }
}
if(aEvent.eventDate >0)
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"dd-MMM-yy 23:59:59"];
    NSString *dateStr = [dateFormatter stringFromDate:aEvent.eventDate]; 
    NSDate *date = [dateFormatter dateFromString:dateStr];
    [dateFormatter release];
    NSTimeInterval interval = [date timeIntervalSinceReferenceDate];
    printf("\n Interval in advance search:%f",interval);
    if([aEvent.eventName length]>0 || [aEvent.eventWineName length]>0 || [aEvent.eventWinery length]>0 || aEvent.eventRatings>0 || [aEvent.eventVariety length]>0)
    {
        conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" and eventDate = %f",interval]];
    }
    else
    {           
        conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" eventDate = %f",interval]];
    }
}

NSString* queryString= @" select * from event";
queryString = [queryString stringByAppendingString:conditionStr];
sqlite3_stmt* statement;
sql = (char*)[queryString UTF8String];
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK)
{
    NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
sqlite3_bind_text(statement, 1, [aEvent.eventName UTF8String],  -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [aEvent.eventWineName UTF8String],  -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, [aEvent.eventVariety UTF8String],  -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 4, [aEvent.eventWinery UTF8String],  -1, SQLITE_TRANSIENT);
sqlite3_bind_int(statement, 5,aEvent.eventRatings);
sqlite3_bind_double(statement,6, [[aEvent eventDate]  timeIntervalSinceReferenceDate]);
while (sqlite3_step(statement) == SQLITE_ROW) 
{
    primaryKey = sqlite3_column_int(statement, 0);      
    searchEvent = [[EventsList alloc] initWithPrimaryKey:primaryKey database:database];
    [searchList addObject:searchEvent];
    [searchEvent release];
}
sqlite3_finalize(statement);
return searchList;
}

在这里,我比较了数据库中的区间值和我们搜索的日期,我得到了不同的值和结果。

盖伊帮助我摆脱这个。

任何人的帮助将不胜感激。

谢谢 Monish Calapatapu。

1 个答案:

答案 0 :(得分:0)

我发现很难解决这个问题。您是否可以将示例代码重新编辑为显示问题的行?此外,您输入和退出的值的示例也会有所帮助。