Re: GMT offset of Created file date
Re: GMT offset of Created file date
- Subject: Re: GMT offset of Created file date
- From: "James J. Merkel" <email@hidden>
- Date: Tue, 11 May 2004 21:16:40 -0700
To answer my own question -- it looks like the following code gives the
correct file creation date:
-(NSDate *)creationDateForFile:(NSString *)filename{
// Returns the creation date for 'filename' as an NSDate.
FSRef ref;
FSCatalogInfo catalogInfo;
OSStatus status;
OSErr err;
NSTimeInterval seconds;
NSCalendarDate *theDateTime;
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSCalendarDate *epoch = [NSCalendarDate dateWithYear:1904 month:1
day:1 hour:0 minute:0 second:0 timeZone:gmt];
status = FSPathMakeRef([filename fileSystemRepresentation], &ref,
nil);
if(status != noErr){
return nil;
}
err = FSGetCatalogInfo(&ref, kFSCatInfoCreateDate, &catalogInfo,
NULL, NULL, NULL);
if(err != noErr){
return nil;
}
seconds = (double)((unsigned long
long)catalogInfo.createDate.highSeconds << 32) +
(double)catalogInfo.createDate.lowSeconds +
(double)catalogInfo.createDate.fraction / 0xffff;
theDateTime = [epoch addTimeInterval:seconds];
NSCalendarDate *date = [[NSCalendarDate alloc]
initWithYear:[theDateTime yearOfCommonEra]
month:[theDateTime monthOfYear] day:[theDateTime dayOfMonth]
hour:[theDateTime hourOfDay] minute:[theDateTime minuteOfHour]
second:[theDateTime secondOfMinute] timeZone: gmt];
[date setTimeZone:[NSTimeZone localTimeZone]];
[date setCalendarFormat:@"%Y:%m:%d %H:%M:%S"];
[date autorelease];
return date;
}
On 11 May 2004 07:11:54 -0700 James J. Merkel wrote:
I want to get the created file date of a file so I used some code which
is very similar to a code snippet which has been posted here before,
namely:
-(NSDate *)creationDateForFile:(NSString *)filename{
// Returns the creation date for 'filename' as an NSDate.
FSRef ref;
FSCatalogInfo catalogInfo;
OSStatus status;
OSErr err;
NSTimeInterval seconds;
NSCalendarDate *epoch = [NSCalendarDate dateWithYear:1904 month:1
day:1 hour:0 minute:0 second:0 timeZone:[NSTimeZone
timeZoneForSecondsFromGMT:0]];
[epoch setCalendarFormat:@"%Y:%m:%d %H:%M:%S"];
status = FSPathMakeRef([filename fileSystemRepresentation], &ref,
nil);
if(status != noErr){
return nil;
}
err = FSGetCatalogInfo(&ref, kFSCatInfoCreateDate, &catalogInfo,
NULL, NULL, NULL);
if(err != noErr){
return nil;
}
seconds = (double)((unsigned long
long)catalogInfo.createDate.highSeconds << 32) +
(double)catalogInfo.createDate.lowSeconds +
(double)catalogInfo.createDate.fraction / 0xffff;
return [epoch addTimeInterval:seconds];
}
However, the date I get returned by this code is offset by my time zone
difference from GMT. For example I get:
2004:04:11 21:32:00 returned whereas the created date in a Finder get
info window is: Sunday, April 11, 2004 2:32 PM, or seven hours earlier.
I have also tried other time zone constants like system time zone etc,
but get the same results.
Has anyone been able to get the correct time of a file created date?
Thanks,
Jim Merkel
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.