Skip to content

Commit 3c05641

Browse files
committed
Merge pull request #225 from kostiakoval/posix-time
add posix time formatting
2 parents 5e3a6d3 + e5d826c commit 3c05641

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

Sources/POSIX/Error.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public enum SystemError: ErrorProtocol {
3333
case symlinkat(Int32, String)
3434
case unlink(Int32, String)
3535
case waitpid(Int32)
36+
case time(Int32)
37+
case gmtime_r(Int32)
38+
case ctime_r(Int32)
39+
case strftime
3640
}
3741

3842
import func libc.strerror
@@ -96,6 +100,14 @@ extension SystemError: CustomStringConvertible {
96100
return "unlink error: \(strerror(errno)): \(path)"
97101
case waitpid(let errno):
98102
return "waitpid error: \(strerror(errno))"
103+
case time(let errno):
104+
return "time error: \(strerror(errno))"
105+
case gmtime_r(let errno):
106+
return "gmtime_r error: \(strerror(errno))"
107+
case ctime_r(let errno):
108+
return "ctime_r error: \(strerror(errno))"
109+
case strftime:
110+
return "strftime error."
99111
}
100112
}
101113
}

Sources/POSIX/ctime_r.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import func libc.ctime_r
12+
import var libc.errno
13+
14+
public func ctime_r() throws -> String {
15+
// The string result that is produced by the ctime_r() function contains exactly 26 characters.
16+
let buffer = UnsafeMutablePointer<Int8>(allocatingCapacity: 50)
17+
defer {
18+
buffer.deallocateCapacity(50)
19+
}
20+
var t = try time()
21+
let result = libc.ctime_r(&t, buffer)
22+
guard result != nil else {
23+
throw SystemError.ctime_r(errno)
24+
}
25+
26+
return String(cString: result)
27+
}

Sources/POSIX/gmtime_r.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import func libc.gmtime_r
12+
import struct libc.tm
13+
import struct libc.time_t
14+
import var libc.errno
15+
16+
public func gmtime_r() throws -> tm {
17+
var t = try time()
18+
var tmTime: tm = tm()
19+
let result = libc.gmtime_r(&t, &tmTime)
20+
guard result != nil else {
21+
throw SystemError.gmtime_r(errno)
22+
}
23+
24+
return tmTime
25+
}

Sources/POSIX/strftime.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import func libc.strftime
12+
import struct libc.tm
13+
14+
public func strftime(format: String, time: tm) throws -> String {
15+
let resultSize = format.characters.count + 200
16+
let result = UnsafeMutablePointer<Int8>(allocatingCapacity: resultSize)
17+
defer {
18+
result.deallocateCapacity(resultSize)
19+
}
20+
var time = time
21+
guard libc.strftime(result, resultSize, format, &time) != 0 else {
22+
throw SystemError.strftime
23+
}
24+
25+
return String(cString: result)
26+
}

Sources/POSIX/time.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import func libc.time
12+
import typealias libc.time_t
13+
import var libc.errno
14+
15+
public func time() throws -> time_t {
16+
let time = libc.time(nil)
17+
guard time != -1 else {
18+
throw SystemError.time(errno)
19+
}
20+
return time
21+
}

0 commit comments

Comments
 (0)