Search code examples
iosevernote

Evernote search with date range


My objective is to display all notes created on date A, date B, date C etc.

I'm building an Evernote Query as such:

//for all notes created on 2015 May 11 
ENNoteSearch *searchMayEleven = [ENNoteSearch noteSearchWithSearchString: @"created:20150511 -created:20150512"];

[[ENSession sharedSession] findNotesWithSearch:searchMayEleven
 inNotebook:nil 
orScope:ENSessionSearchScopeAll 
sortOrder:ENSessionSortOrderRecentlyCreated 
maxResults:100 
completion:^(NSArray *findNotesResults, NSError *findNotesError) {
//completion block
}]];

My results, however, fetch notes that are created on 12 May as well as 11 May.

1) I deduce that I have to set a timezone in my Evernote Session. Based on your experience, is this a valid deduction?

2) If so, I haven't been able to find a way to do so after reading through the documentation. Is it even possible?

3) Would you advice an alternative approach? Perhaps using the notestore instead?


Solution

  • In Evernote your dates are being kept in UTC.

    When you make the search you need to create an Evernote search grammar that's relative to the timezone that you're interested in. In your case the timezone of the client or of the iPhone.

    To get the user timezone:

    ENSession * session = [ENSession sharedSession];
    EDAMUser * user = session.user;
    

    where the EDAMUser class has this structure:

    @interface EDAMUser : FATObject 
    @property (nonatomic, strong) NSNumber * id; // EDAMUserID
    @property (nonatomic, strong) NSString * username;
    ...
    @property (nonatomic, strong) NSString * timezone;
    

    For example my user timezone is: America/Montreal so on EST.

    In order to get all the notes created May 11th you need to construct this Evernote search grammar:

     @"created:20150511T040000Z -created:20150512T040000Z"
    

    notice the ...T040000Z at the end.

    So the conclusion is that you need to include the "definition" of the date from the client's perspective otherwise the query will work on UTC.

    Here is an example of how to build the Evernote grammar search for the current day:

    -(NSString *)buildQueryStringForDate: (NSDate *)date {
        NSDateFormatter * formatter = [NSDateFormatter new];
        [formatter setDateFormat:@"yyyyMMdd'T'HHmmss'Z'"];
        formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
        formatter.locale = [NSLocale systemLocale];
    
        DateRange * dateRange = [DateRange rangeForDayContainingDate:[NSDate new]];
        return [NSString stringWithFormat:@"created:%@ -created:%@", [formatter stringFromDate:dateRange.startDate], [formatter stringFromDate:dateRange.endDate]];
    }
    

    The code for [DateRange rangeForDayContainingDate:[NSDate new]] can be found here: How can I generate convenient date ranges based on a given NSDate?

    I hope this helps.