Skip to content

Instantly share code, notes, and snippets.

@martinhoeller
Created April 18, 2020 08:13
Show Gist options
  • Save martinhoeller/4bf05d5a647cd465d736d319e17b2bec to your computer and use it in GitHub Desktop.
Save martinhoeller/4bf05d5a647cd465d736d319e17b2bec to your computer and use it in GitHub Desktop.
A URL extension to insert a timestamp in the URL's file name
extension URL {
// Inserts a timestamp into a URL's filename
// e.g. "/path/to/file.ext" -> "/path/to/file~1587197384.ext"
func addingTimestamp(separator: String = "~") -> URL {
let timestamp = Int(Date().timeIntervalSince1970)
let filename = deletingPathExtension().lastPathComponent + "\(separator)\(timestamp).\(pathExtension)"
return deletingLastPathComponent().appendingPathComponent(filename)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment