Skip to content

Commit 2e023eb

Browse files
committed
Add utils to convert from/to .NET DateTime,TimeSpan,byte[]
1 parent 14d1a76 commit 2e023eb

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

Sources/ServiceStack/CoreTypes.swift

+36-8
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,22 @@ extension Double {
149149
}
150150
}
151151

152+
// From .NET TimeSpan (XSD Duration) to Swift TimeInterval
153+
func fromTimeSpan(timeSpan:String) -> TimeInterval? {
154+
return TimeInterval.fromString(timeSpan)
155+
}
156+
157+
// From Swift TimeInterval to .NET TimeSpan (XSD Duration)
158+
func toTimeSpan(timeInterval:TimeInterval) -> String {
159+
return timeInterval.toXsdDuration()
160+
}
161+
152162
@propertyWrapper
153163
public struct TimeSpan {
154164
public var wrappedValue: TimeInterval? = 0
155165
public init(wrappedValue: TimeInterval?) {
156166
self.wrappedValue = wrappedValue
157167
}
158-
public static func parse(_ timeSpan:String) -> TimeInterval? {
159-
return TimeInterval.fromString(timeSpan)
160-
}
161168
}
162169

163170
extension TimeSpan : Codable {
@@ -195,18 +202,39 @@ public class TimeIntervalConveter : StringConvertible {
195202
}
196203
}
197204

205+
206+
// From .NET Guid to Guid string
207+
func fromGuid(_ guid:String) -> String {
208+
return guid
209+
}
210+
211+
// From Guid string to .NET Guid
212+
func toGuid(_ guid:String) -> String {
213+
return guid
214+
}
215+
216+
// From .NET byte[] (Base64 String) to Swift [UInt8]
217+
func fromByteArray(_ base64String:String) -> [UInt8] {
218+
if let data = Data(base64Encoded: base64String) {
219+
return [UInt8](data)
220+
}
221+
return []
222+
}
223+
224+
// From Swift [UInt8] to .NET byte[] (Base64 String)
225+
func toByteArray(_ bytes:[UInt8]) -> String {
226+
return Data(bytes).base64EncodedString()
227+
}
228+
198229
public class UInt8Base64Converter : StringConvertible {
199230
public var forType = Reflect<[UInt8]>.typeName
200231

201232
public func fromString<T>(_ type: T.Type, _ string: String) -> T? {
202-
if let data = Data(base64Encoded: string) {
203-
return [UInt8](data) as? T
204-
}
205-
return [] as? T
233+
return fromByteArray(string) as? T
206234
}
207235
public func toString<T>(instance: T) -> String? {
208236
if let bytes = instance as? [UInt8] {
209-
let to = Data(bytes).base64EncodedString()
237+
let to = toByteArray(bytes)
210238
return to
211239
}
212240
return nil

Sources/ServiceStack/DateExtensions.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
#endif
1212
import Foundation
1313

14-
public class DateTime {
15-
public static func parse(_ dateTime:String) -> Date? {
16-
return Date.fromString(dateTime)
17-
}
14+
// From .NET DateTime (WCF JSON or ISO Date) to Swift Date
15+
func fromDateTime(_ jsonDate:String) -> Date? {
16+
return Date.fromString(jsonDate)
17+
}
18+
19+
// From Swift Date to .NET DateTime (WCF JSON Date)
20+
func toDateTime(_ dateTime:Date) -> String {
21+
return dateTime.jsonDate;
1822
}
1923

2024
public extension Date {

Tests/ServiceStackTests/JsonServiceClientTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ import XCTest
419419
to.intList = [4, 5, 6]
420420
to.stringArray = ["A", "B", "C"]
421421
to.stringList = ["D", "E", "F"]
422-
to.byteArray = [65,66,67]
422+
to.byteArray = fromByteArray("QUJD") //base64(ABC)
423423
to.pocoArray.append(createPoco("pocoArray"))
424424
to.pocoList.append(createPoco("pocoList"))
425425
to.pocoLookup["A"] = [createPoco("B"), createPoco("C")]

0 commit comments

Comments
 (0)