From 44be1c45a9196af26cdef87f7b8165e7393521d7 Mon Sep 17 00:00:00 2001 From: Max Howell Date: Sat, 26 Jan 2019 13:06:23 -0500 Subject: [PATCH] Add `Path.ctime` --- Sources/Path+Attributes.swift | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Sources/Path+Attributes.swift b/Sources/Path+Attributes.swift index 393d3f2..ffe21f0 100644 --- a/Sources/Path+Attributes.swift +++ b/Sources/Path+Attributes.swift @@ -4,14 +4,28 @@ public extension Path { //MARK: Filesystem Attributes /** - Returns the modification-time. + Returns the creation-time of the file. + - Note: Returns UNIX-time-zero if there is no creation-time, this should only happen if the file doesn’t exist. + */ + var ctime: Date { + do { + let attrs = try FileManager.default.attributesOfItem(atPath: string) + return attrs[.creationDate] as? Date ?? Date(timeIntervalSince1970: 0) + } catch { + //TODO log error + return Date(timeIntervalSince1970: 0) + } + } + + /** + Returns the modification-time of the file. - Note: Returns the creation time if there is no modification time. - - Note: Returns UNIX-time-zero if neither are available, though this *should* be impossible. + - Note: Returns UNIX-time-zero if neither are available, this should only happen if the file doesn’t exist. */ var mtime: Date { do { let attrs = try FileManager.default.attributesOfItem(atPath: string) - return attrs[.modificationDate] as? Date ?? attrs[.creationDate] as? Date ?? Date(timeIntervalSince1970: 0) + return attrs[.modificationDate] as? Date ?? ctime } catch { //TODO log error return Date(timeIntervalSince1970: 0)