Skip to content

Commit

Permalink
Merge pull request JohnSundell#16 from JohnSundell/item-modified-date
Browse files Browse the repository at this point in the history
FileSystem.Item: Add modifiedDate property
  • Loading branch information
JohnSundell authored Jun 3, 2017
2 parents 03d4fb2 + 3bdd4bd commit f7b8537
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ public class FileSystem {

return components.last
}


/// The date when the item was last modified
public private(set) lazy var modificationDate: Date = self.loadModificationDate()

/// The folder that the item is contained in, or `nil` if this item is the root folder of the file system
public var parent: Folder? {
return fileManager.parentPath(for: path).flatMap { parentPath in
Expand Down Expand Up @@ -853,6 +856,11 @@ private extension FileSystem.Item {
}
}
}

func loadModificationDate() -> Date {
let attributes = try! fileManager.attributesOfItem(atPath: path)
return attributes[FileAttributeKey.modificationDate] as! Date
}
}

private extension FileManager {
Expand Down
10 changes: 10 additions & 0 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ class FilesTests: XCTestCase {
XCTAssertEqual(folder.files.last?.name, "C")
}
}

func testModificationDate() {
performTest {
let subfolder = try folder.createSubfolder(named: "Folder")
XCTAssertTrue(Calendar.current.isDateInToday(subfolder.modificationDate))

let file = try folder.createFile(named: "File")
XCTAssertTrue(Calendar.current.isDateInToday(file.modificationDate))
}
}

func testParent() {
performTest {
Expand Down

0 comments on commit f7b8537

Please sign in to comment.