Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 13, 2010, 06:11 AM   #1
dimaggioel
macrumors newbie
 
Join Date: Oct 2010
NSMetadataQuery and NSDate: precision problem

Hi,
I was using NSMetadataQuery to find files which creation date is more recent than a NSDate.
It seems that NSMetadataQuery cannot find files which creation date is more recent than the specified NSDate for one hour.
I mean, if the creation date of a file is today at 12:10:50, the query return no result if I set today at 11:10:51 as NSDate, while it founds the file if I set today at 11:10:49.

Can someone explain me why and how can I solve it?
dimaggioel is offline   0 Reply With Quote
Old Oct 13, 2010, 01:07 PM   #2
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
Probable daylight savings time error.
jared_kipe is offline   0 Reply With Quote
Old Oct 13, 2010, 02:58 PM   #3
chown33
macrumors 603
 
Join Date: Aug 2009
Post your code.

There needs to be enough code that someone else can compile it and run it. We can't compile and run descriptions of code.
chown33 is offline   0 Reply With Quote
Old Oct 13, 2010, 03:52 PM   #4
dimaggioel
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Quote:
Originally Posted by chown33 View Post
Post your code.

There needs to be enough code that someone else can compile it and run it. We can't compile and run descriptions of code.
sorry, you're right.
Here's the code I wrote
Code:
	NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
 
	NSDate *date = [NSDate dateWithNaturalLanguageString:@"2010-09-17 23:18:00"];
 
	NSPredicate *predicate = [NSPredicate predicateWithFormat:@"kMDItemFSCreationDate > %@",date];		
	
	[query setSearchScopes:[NSArray arrayWithObject:@"[path to directory]"]];	
	[query setPredicate:predicate];
 
	[query startQuery];
 
	[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
 
        [query stopQuery];
        NSLog(@"results : %@",[query results]);
In the particular directory set as search scope there's only one file, created on 17 Sep at 23:18:36 (saw running stat from terminal).
dimaggioel is offline   0 Reply With Quote
Old Oct 13, 2010, 05:47 PM   #5
chown33
macrumors 603
 
Join Date: Aug 2009
What is your local timezone?

The NSDate may be interpreted as GMT, since no timezone is given. It certainly does this for me. You can observe this by NSLog'ing the date.

From the class reference doc for NSDate, description of dateWithNaturalLanguageString (underline added for emphasis):
Quote:
This method supports only a limited set of colloquial phrases, primarily in English. It may give unexpected results, and its use is strongly discouraged.
http://developer.apple.com/library/m...Reference.html


The actual metadata of a file should be examined using the 'mdls' command, not 'stat'. Example:
Code:
mdls . path/to/file
chown33 is offline   0 Reply With Quote
Old Oct 13, 2010, 06:50 PM   #6
dimaggioel
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Quote:
Originally Posted by chown33 View Post
What is your local timezone?

The NSDate may be interpreted as GMT, since no timezone is given. It certainly does this for me. You can observe this by NSLog'ing the date.

From the class reference doc for NSDate, description of dateWithNaturalLanguageString (underline added for emphasis):

http://developer.apple.com/library/m...Reference.html
I didn't notice that advise, so I must have been lucky because date is translated correctly (I already NSLogged that ).
Even time zone is correct: I'm GMT +2:00 and here's the output of NSLog:
Code:
date = 2010-09-17 23:18:00 +0200
Quote:
Originally Posted by chown33 View Post
The actual metadata of a file should be examined using the 'mdls' command, not 'stat'. Example:
Code:
mdls . path/to/file
I was interested only in creation date, not examining all the metadata However running mdls:
Code:
kMDItemFSCreationDate          = 2010-09-17 23:18:36 +0200
dimaggioel is offline   0 Reply With Quote
Old Oct 13, 2010, 07:47 PM   #7
chown33
macrumors 603
 
Join Date: Aug 2009
I've tried a number of things here and I simply can't replicate the problem. The results always find exactly the files they're supposed to, even when I specify a date and time to the exact second. Procedure:
  • Enter cmd 'touch example' to create a new file.
  • Enter cmd 'mdls example' to get its actual metadata.
  • Paste its kMDItemFSCreationDate date time tzoffset into the source.
  • Increase the pasted-in date time by one second.
  • Compile and run.
    OR
  • Paste the exact date time tzoffset into the source.
  • Change the ">" in the predicate to ">=".
  • Compile and run.

I think you need to post a complete compilable example that demonstrates the problem. It should include the necessary data files, with the appropriate creation-date to cause the malfunction. You should also identify exactly which OS version, and which architecture you're running (ppc, i386, x86_64).

Maybe the problem is caused by your machine somehow delaying when it performs metadata scans. So there may be files that were recently created, but they haven't been scanned by the mdimport daemon yet, so they have no metadata in the metadata store, and thus won't be found with a metadata query. Or maybe the file or folder is excluded from Spotlight searches (System Preferences > Spotlight pane). Those are just guesses.

Last edited by chown33; Oct 13, 2010 at 07:55 PM.
chown33 is offline   0 Reply With Quote
Old Oct 14, 2010, 09:11 AM   #8
dimaggioel
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Here is a complete XCode Project:
http://www.mediafire.com/?3rmj31m042js8ss
(remember to set correctly the search scope of the NSMetadataQuery object).

I'm on x86_64, MacOS 10.6.4. This problem happens with almost all files (or, at least, all that I tried). However here's a file created this morning at 10:22:05
http://www.mediafire.com/?fqxn9vcd0rbddw4

Furthermore I noticed that (I don't know if it's normal) spotlight does not find a file just created with touch. I mean if I create a file through finder or through a program it works, but if I create it through touch for a while it's as it doesn't exist...
dimaggioel is offline   0 Reply With Quote
Old Oct 14, 2010, 01:27 PM   #9
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
Oddly I have not found a date that would NOT give your example.txt

EDIT: Ahh because you created it in the future!! (November 14th)
And a different timezone!

Code:
	for(int i =0;i<[[query results] count];i++) {
		NSMetadataItem *item = [[query results] objectAtIndex:i];
		NSLog(@"%@ , %@",[item valueForAttribute:@"kMDItemFSName"], [item valueForAttribute:@"kMDItemFSCreationDate"]);
	}
EDIT2: Seems to work fine. I changed the minute right around when the file itself says it was created and it seems to work just fine.
Attached Thumbnails
Click image for larger version

Name:	Screen shot 2010-10-14 at 11.37.04 AM.png
Views:	15
Size:	22.9 KB
ID:	255747   Click image for larger version

Name:	Screen shot 2010-10-14 at 11.43.33 AM.png
Views:	10
Size:	14.5 KB
ID:	255749  

Last edited by jared_kipe; Oct 14, 2010 at 01:49 PM.
jared_kipe is offline   0 Reply With Quote
Old Oct 14, 2010, 02:44 PM   #10
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by dimaggioel View Post
Furthermore I noticed that (I don't know if it's normal) spotlight does not find a file just created with touch. I mean if I create a file through finder or through a program it works, but if I create it through touch for a while it's as it doesn't exist...
Yes, I now see this with 'touch' on 10.6.3. The query seems to be automatically excluding files that are zero-length from its results. Append one byte to the file and it will appear in the results for me. YMMV.

Zero-length files are NOT excluded on older OS versions. I happened to be using 10.4.11 when I reported results of my earlier test. YMMV.


And I still don't see any of the originally reported problem. I don't see a creation-date of Nov 14 on the "example.txt" file, either, so I can only guess what might be happening.

When was the last time you restarted? Maybe something is wrong with the mdimporter daemon.

If I were you, I'd submit the test program to Apple's bugreporter. Be sure to include suitable data that reliably causes the problem, or a procedure for creating files that reliably show the problem. The uploaded example.txt doesn't show the problem, because its metadata wasn't included, AFAICT.

You may have trouble archiving a file with the metadata to show the problem, so I suggest concentrating on a series of commands, perhaps using echo or touch, that can create a local file with the proper metadata.

http://developer.apple.com/bugreporter
chown33 is offline   0 Reply With Quote
Old Oct 14, 2010, 06:29 PM   #11
dimaggioel
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Quote:
Originally Posted by jared_kipe View Post
Oddly I have not found a date that would NOT give your example.txt

EDIT: Ahh because you created it in the future!! (November 14th)
And a different timezone!
I don't know how I could create it in the future (it would be nice)
on my computer:
Code:
kMDItemFSCreationDate      = 2010-10-14 10:22:05 +0200

Quote:
Originally Posted by chown33 View Post
Yes, I now see this with 'touch' on 10.6.3. The query seems to be automatically excluding files that are zero-length from its results. Append one byte to the file and it will appear in the results for me. YMMV.

Zero-length files are NOT excluded on older OS versions. I happened to be using 10.4.11 when I reported results of my earlier test. YMMV.
It seems not to be that simple...zero-length files created from a program (for example TextEdit) or files born with a length different from 0 and then emptied (i.e. launch echo "test" > file.txt, then open file.txt with an editor and empty it) are correctly found by spotlight.

Besides, I'm not 100% sure, but I think I have a couple of empty files created with touch and correctly found by spotlight...

Quote:
Originally Posted by chown33 View Post
When was the last time you restarted? Maybe something is wrong with the mdimporter daemon.
Last time...about five minutes ago. However, I turn it off every day when I go to bed.

Quote:
Originally Posted by chown33 View Post
If I were you, I'd submit the test program to Apple's bugreporter. Be sure to include suitable data that reliably causes the problem, or a procedure for creating files that reliably show the problem. The uploaded example.txt doesn't show the problem, because its metadata wasn't included, AFAICT.

You may have trouble archiving a file with the metadata to show the problem, so I suggest concentrating on a series of commands, perhaps using echo or touch, that can create a local file with the proper metadata.

http://developer.apple.com/bugreporter
The problem is that I cannot tell to Apple much more than what I can tell to you...Actually what I did is not so much different from what you did to test the code:
  • create a file calling touch file1.txt
  • try to find it with Test (0 results)
  • try to find it with Spotlight (0 results)
  • launch echo "example" > file1.txt
  • try to find it with Spotlight (found)
  • call mdls file1.txt and get the exact creation date
  • insert the exact creation date less a second as date in Test and search (0 results)
  • insert the exact creation date less an hour and a second in Test and search (found)

same thing if, instead of using NSDatePicker to get the date (even if I don't think there are problems about it), I insert it manually in the code, calling dateWithNaturalLanguageString or even dateWithString (the correct format is the one showed by mdls).
Furthermore, I followed these identical instructions on a friend's mac (10.6.4 if I don't get wrong) with the same results.

Quote:
Originally Posted by jared_kipe View Post
Probable daylight savings time error.
Sorry, I missed your post, but actually is exactly what I'm thinking of...In fact normally I should be GMT+1, not +2 as it's in this period of the year.
But examining the metadata of the file I read
Code:
kMDItemFSCreationDate      = 2010-10-14 10:22:05 +0200
so it should understand that the file has been created during daylight saving time and results should be coherent with that. Besides, setting the date (manually or through NSDatePicker) the date is 2010-10.... +0200, so it should work correctly...
dimaggioel is offline   0 Reply With Quote
Old Oct 20, 2010, 11:28 AM   #12
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
Quote:
Originally Posted by dimaggioel View Post
Sorry, I missed your post, but actually is exactly what I'm thinking of...In fact normally I should be GMT+1, not +2 as it's in this period of the year.
But examining the metadata of the file I read
Code:
kMDItemFSCreationDate      = 2010-10-14 10:22:05 +0200
so it should understand that the file has been created during daylight saving time and results should be coherent with that. Besides, setting the date (manually or through NSDatePicker) the date is 2010-10.... +0200, so it should work correctly...
LOL You missed the first reply to your own thread?!

Anywho, your file seems to be messed up at least after I downloaded it. Your program though seems to run and behave perfectly on my computer.
I'm running 10.6.3 how about you?
Maybe copy your program into /Applications and make a new user on your computer to test it out.
jared_kipe is offline   0 Reply With Quote
Old Nov 1, 2010, 06:39 AM   #13
dimaggioel
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Sorry for the late, I have been away

Yes I missed your post because I went directly to the last message and jumped over your...

However probably you were right! I tried to run the application on a friend's mac (running 10.6.3) and the result was the same as mine.
Yesterday we changed to winter time; today I tried again to execute the program on my mac (10.6.4) and it works perfectly.

I think now there's very few I can do...
When chown suggested me to I submitted the test program to Apple, explaining also that the problem may be caused by daylight saving time. Only hope they'll answer soon...
Isn't there a way to know if they read it or not?
dimaggioel is offline   0 Reply With Quote
Old Nov 1, 2010, 01:56 PM   #14
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
I don't know, but maybe you could fix it yourself by doing some appropriate testing of the runtime computer. Possibly add a category to NSDate to do that testing to make it easy.
jared_kipe is offline   0 Reply With Quote
Old Nov 4, 2010, 09:07 PM   #15
dimaggioel
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Well, if I have to be sincere, I don't know where to start from for try solving it on my own (except some kind of workaround but, of course, that's not a fix). For how I see it, the problem is somewhere in the way NSMetadataQuery perform search, since metadata seems to be stored correctly and I did not found problems in comparisons between NSDate objects...
dimaggioel is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: NSDate singleton. IDMah iPhone/iPad Programming 3 Mar 18, 2014 11:16 AM
Precision tools? forza69 iPhone 3 Oct 30, 2013 12:12 AM
Why can't convert NSString to NSDate? mikezang iPhone/iPad Programming 4 Jan 22, 2013 06:56 PM
sorting an array of NSdate daproject85 iPhone/iPad Programming 5 Sep 15, 2012 08:58 AM
NSDate Update? xArtx iPhone/iPad Programming 4 Jul 22, 2012 07:42 AM

Forum Jump

All times are GMT -5. The time now is 10:32 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC