Skip to content

Instantly share code, notes, and snippets.

@pgtwitter
Created August 17, 2026 07:56
Show Gist options
  • Select an option

  • Save pgtwitter/c8d799d013d860a26aaa459e098c5fa4 to your computer and use it in GitHub Desktop.

Select an option

Save pgtwitter/c8d799d013d860a26aaa459e098c5fa4 to your computer and use it in GitHub Desktop.
objective-c/cocoa reflectTimestampSourcePath:destPath:
+ (BOOL)reflectTimestampSourcePath:(NSString *)sourcePath destPath:(NSString *)destPath
{
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
// 1. ソースファイルのタイムスタンプを取得
NSDictionary *sourceAttrs = [fm attributesOfItemAtPath:sourcePath error:&error];
if (!sourceAttrs) {
NSLog(@"Failed to get source attributes: %@", error);
return NO;
}
NSDate *creationDate = sourceAttrs[NSFileCreationDate];
NSDate *modificationDate = sourceAttrs[NSFileModificationDate];
// 2. 宛先ファイルにタイムスタンプを反映
NSDictionary *attrsToSet = @{
NSFileCreationDate: creationDate,
NSFileModificationDate: modificationDate
};
BOOL success = [fm setAttributes:attrsToSet ofItemAtPath:destPath error:&error];
if (!success) {
NSLog(@"Failed to set attributes: %@", error);
return NO;
}
return YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment