Skip to content

add posix time formatting #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Sources/POSIX/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public enum SystemError: ErrorProtocol {
case symlinkat(Int32, String)
case unlink(Int32, String)
case waitpid(Int32)
case time(Int32)
case gmtime_r(Int32)
case ctime_r(Int32)
case strftime
}

import func libc.strerror
Expand Down Expand Up @@ -96,6 +100,14 @@ extension SystemError: CustomStringConvertible {
return "unlink error: \(strerror(errno)): \(path)"
case waitpid(let errno):
return "waitpid error: \(strerror(errno))"
case time(let errno):
return "time error: \(strerror(errno))"
case gmtime_r(let errno):
return "gmtime_r error: \(strerror(errno))"
case ctime_r(let errno):
return "ctime_r error: \(strerror(errno))"
case strftime:
return "strftime error."
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions Sources/POSIX/ctime_r.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
This source file is part of the Swift.org open source project

Copyright 2015 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import func libc.ctime_r
import var libc.errno

public func ctime_r() throws -> String {
// The string result that is produced by the ctime_r() function contains exactly 26 characters.
let buffer = UnsafeMutablePointer<Int8>(allocatingCapacity: 50)
defer {
buffer.deallocateCapacity(50)
}
var t = try time()
let result = libc.ctime_r(&t, buffer)
guard result != nil else {
throw SystemError.ctime_r(errno)
}

return String(cString: result)
}
25 changes: 25 additions & 0 deletions Sources/POSIX/gmtime_r.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
This source file is part of the Swift.org open source project

Copyright 2015 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import func libc.gmtime_r
import struct libc.tm
import struct libc.time_t
import var libc.errno

public func gmtime_r() throws -> tm {
var t = try time()
var tmTime: tm = tm()
let result = libc.gmtime_r(&t, &tmTime)
guard result != nil else {
throw SystemError.gmtime_r(errno)
}

return tmTime
}
26 changes: 26 additions & 0 deletions Sources/POSIX/strftime.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
This source file is part of the Swift.org open source project

Copyright 2015 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import func libc.strftime
import struct libc.tm

public func strftime(format: String, time: tm) throws -> String {
let resultSize = format.characters.count + 200
let result = UnsafeMutablePointer<Int8>(allocatingCapacity: resultSize)
defer {
result.deallocateCapacity(resultSize)
}
var time = time
guard libc.strftime(result, resultSize, format, &time) != 0 else {
throw SystemError.strftime
}

return String(cString: result)
}
21 changes: 21 additions & 0 deletions Sources/POSIX/time.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
This source file is part of the Swift.org open source project

Copyright 2015 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import func libc.time
import typealias libc.time_t
import var libc.errno

public func time() throws -> time_t {
let time = libc.time(nil)
guard time != -1 else {
throw SystemError.time(errno)
}
return time
}