Skip to content

Commit

Permalink
Add utils to convert from/to .NET DateTime,TimeSpan,byte[]
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 11, 2021
1 parent 14d1a76 commit 2e023eb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
44 changes: 36 additions & 8 deletions Sources/ServiceStack/CoreTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,22 @@ extension Double {
}
}

// From .NET TimeSpan (XSD Duration) to Swift TimeInterval
func fromTimeSpan(timeSpan:String) -> TimeInterval? {
return TimeInterval.fromString(timeSpan)
}

// From Swift TimeInterval to .NET TimeSpan (XSD Duration)
func toTimeSpan(timeInterval:TimeInterval) -> String {
return timeInterval.toXsdDuration()
}

@propertyWrapper
public struct TimeSpan {
public var wrappedValue: TimeInterval? = 0
public init(wrappedValue: TimeInterval?) {
self.wrappedValue = wrappedValue
}
public static func parse(_ timeSpan:String) -> TimeInterval? {
return TimeInterval.fromString(timeSpan)
}
}

extension TimeSpan : Codable {
Expand Down Expand Up @@ -195,18 +202,39 @@ public class TimeIntervalConveter : StringConvertible {
}
}


// From .NET Guid to Guid string
func fromGuid(_ guid:String) -> String {
return guid
}

// From Guid string to .NET Guid
func toGuid(_ guid:String) -> String {
return guid
}

// From .NET byte[] (Base64 String) to Swift [UInt8]
func fromByteArray(_ base64String:String) -> [UInt8] {
if let data = Data(base64Encoded: base64String) {
return [UInt8](data)
}
return []
}

// From Swift [UInt8] to .NET byte[] (Base64 String)
func toByteArray(_ bytes:[UInt8]) -> String {
return Data(bytes).base64EncodedString()
}

public class UInt8Base64Converter : StringConvertible {
public var forType = Reflect<[UInt8]>.typeName

public func fromString<T>(_ type: T.Type, _ string: String) -> T? {
if let data = Data(base64Encoded: string) {
return [UInt8](data) as? T
}
return [] as? T
return fromByteArray(string) as? T
}
public func toString<T>(instance: T) -> String? {
if let bytes = instance as? [UInt8] {
let to = Data(bytes).base64EncodedString()
let to = toByteArray(bytes)
return to
}
return nil
Expand Down
12 changes: 8 additions & 4 deletions Sources/ServiceStack/DateExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
#endif
import Foundation

public class DateTime {
public static func parse(_ dateTime:String) -> Date? {
return Date.fromString(dateTime)
}
// From .NET DateTime (WCF JSON or ISO Date) to Swift Date
func fromDateTime(_ jsonDate:String) -> Date? {
return Date.fromString(jsonDate)
}

// From Swift Date to .NET DateTime (WCF JSON Date)
func toDateTime(_ dateTime:Date) -> String {
return dateTime.jsonDate;
}

public extension Date {
Expand Down
2 changes: 1 addition & 1 deletion Tests/ServiceStackTests/JsonServiceClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ import XCTest
to.intList = [4, 5, 6]
to.stringArray = ["A", "B", "C"]
to.stringList = ["D", "E", "F"]
to.byteArray = [65,66,67]
to.byteArray = fromByteArray("QUJD") //base64(ABC)
to.pocoArray.append(createPoco("pocoArray"))
to.pocoList.append(createPoco("pocoList"))
to.pocoLookup["A"] = [createPoco("B"), createPoco("C")]
Expand Down

0 comments on commit 2e023eb

Please sign in to comment.