Skip to content

Commit 99f893a

Browse files
committed
split gmtime and strftime functions.
1 parent e7be4e2 commit 99f893a

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

Sources/POSIX/ctime.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010

1111
import func libc.time
1212
import func libc.ctime
13-
import typealias libc.time_t
1413

1514
public func ctime() throws -> String {
16-
var time = 0
17-
libc.time(&time)
15+
var time = libc.time(nil)
1816
let result = libc.ctime(&time)
1917

2018
return String(cString: result)
21-
}
19+
}

Sources/POSIX/gmtime.swift

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,13 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
import typealias libc.time_t
12-
import func libc.strftime
1311
import func libc.gmtime
1412
import func libc.time
13+
import struct libc.tm
1514

16-
public enum StringError: ErrorProtocol {
17-
case NotEnoughSpace
18-
}
19-
20-
extension StringError: CustomStringConvertible {
21-
public var description: String {
22-
switch self {
23-
case .NotEnoughSpace:
24-
return "gmtime error: not enough space, increase resultSize."
25-
}
26-
}
27-
}
28-
29-
public func gmtime(format: String, resultSize: Int = 200) throws -> String {
30-
var time = 0
31-
libc.time(&time)
32-
33-
let result = UnsafeMutablePointer<Int8>(allocatingCapacity: resultSize)
34-
defer {
35-
result.deallocateCapacity(resultSize)
36-
}
37-
15+
public func gmtime() -> tm {
16+
var time = libc.time(nil)
3817
let gmTime = gmtime(&time)
39-
guard libc.strftime(result, resultSize, format, gmTime) != 0 else {
40-
throw StringError.NotEnoughSpace
41-
}
4218

43-
return String(cString: result)
19+
return gmTime.pointee
4420
}

Sources/POSIX/strftime.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 enum StringError: ErrorProtocol {
15+
case NotEnoughSpace
16+
}
17+
18+
extension StringError: CustomStringConvertible {
19+
public var description: String {
20+
switch self {
21+
case .NotEnoughSpace:
22+
return "gmtime error: not enough space, increase resultSize."
23+
}
24+
}
25+
}
26+
27+
public func strftime(format: String, time: tm, resultSize: Int = 200) throws -> String {
28+
var time = time
29+
let result = UnsafeMutablePointer<Int8>(allocatingCapacity: resultSize)
30+
defer {
31+
result.deallocateCapacity(resultSize)
32+
}
33+
guard libc.strftime(result, resultSize, format, &time) != 0 else {
34+
throw StringError.NotEnoughSpace
35+
}
36+
37+
return String(cString: result)
38+
}

0 commit comments

Comments
 (0)