Skip to content

Commit

Permalink
Time Util: ✅
Browse files Browse the repository at this point in the history
add parse and format
  • Loading branch information
kosyloa committed Nov 21, 2023
1 parent 0fc2634 commit 93ec5f5
Show file tree
Hide file tree
Showing 28 changed files with 446 additions and 228 deletions.
4 changes: 0 additions & 4 deletions DXFeedFramework.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@
648BD56B2AC4576F004A3A95 /* HelpTool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 648BD56A2AC4576F004A3A95 /* HelpTool.swift */; };
648BD56D2AC56A04004A3A95 /* SubscriptionUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 648BD56C2AC56A04004A3A95 /* SubscriptionUtils.swift */; };
648BD56F2AC582AB004A3A95 /* DateTimeParserTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 648BD56E2AC582AB004A3A95 /* DateTimeParserTest.swift */; };
648BD5712AC583AC004A3A95 /* TimeFormat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 648BD5702AC583AC004A3A95 /* TimeFormat.swift */; };
648E98AA2AAF625800BFD219 /* IIndexedEvent+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 648E98A92AAF625800BFD219 /* IIndexedEvent+Ext.swift */; };
649282E72AD54919008F0F04 /* ScheduleUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 649282E62AD54919008F0F04 /* ScheduleUtils.swift */; };
649282E82AD54919008F0F04 /* ScheduleUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 649282E62AD54919008F0F04 /* ScheduleUtils.swift */; };
Expand Down Expand Up @@ -685,7 +684,6 @@
648BD56A2AC4576F004A3A95 /* HelpTool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HelpTool.swift; sourceTree = "<group>"; };
648BD56C2AC56A04004A3A95 /* SubscriptionUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionUtils.swift; sourceTree = "<group>"; };
648BD56E2AC582AB004A3A95 /* DateTimeParserTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateTimeParserTest.swift; sourceTree = "<group>"; };
648BD5702AC583AC004A3A95 /* TimeFormat.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeFormat.swift; sourceTree = "<group>"; };
648E98A92AAF625800BFD219 /* IIndexedEvent+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "IIndexedEvent+Ext.swift"; sourceTree = "<group>"; };
649282E62AD54919008F0F04 /* ScheduleUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScheduleUtils.swift; sourceTree = "<group>"; };
649282E92AD55323008F0F04 /* IpfConnect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IpfConnect.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1154,7 +1152,6 @@
64C771F12A94A224009868C2 /* Character+Ext.swift */,
64C772002A975102009868C2 /* Reference+Util.swift */,
6464074E2A9F62D4006FF769 /* Date+Ext.swift */,
648BD5702AC583AC004A3A95 /* TimeFormat.swift */,
64A42F3A2B07A7A3001C3ACC /* SymbolParser.swift */,
);
path = Utils;
Expand Down Expand Up @@ -2327,7 +2324,6 @@
640C3FD62A6179E300555161 /* CandleAlignment.swift in Sources */,
640C3FCC2A616B6200555161 /* ArgumentException.swift in Sources */,
64B436442AB88EA40003919E /* NativeDay.swift in Sources */,
648BD5712AC583AC004A3A95 /* TimeFormat.swift in Sources */,
642BE4C42A2E1AFF0052340A /* Quote.swift in Sources */,
64656F602A1B9EC2006A0B19 /* EnumUtil.swift in Sources */,
64A42F492B0B9683001C3ACC /* NativeTimeFormat.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class TimeSeriesSubscriptionSymbol: GenericIndexedEventSubscriptionSymbol

/// Custom symbol has to return string representation.
public override var stringValue: String {
return "\(symbol){fromTime=\(TimeUtil.toLocalDateString(millis: fromTime))}"
return "\(symbol){fromTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: fromTime)) ?? "")}"
}
}

Expand Down
4 changes: 2 additions & 2 deletions DXFeedFramework/Events/Market/Candles/Candle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ extension Candle {
func baseFieldsToString() -> String {
return """
\(eventSymbol), \
eventTime=\(TimeUtil.toLocalDateString(millis: eventTime)), \
eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \
eventFlags=\(eventFlags.toHexString()), \
time=\(TimeUtil.toLocalDateString(millis: time)), \
time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \
sequence=\(getSequence()), \
count=\(count), \
open=\(open), \
Expand Down
7 changes: 4 additions & 3 deletions DXFeedFramework/Events/Market/Extra/OrderBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,19 @@ extension OrderBase {

/// Returns string representation of this candle fields.
func baseFieldsToString() -> String {

return
"""
\(eventSymbol), \
eventTime=\(TimeUtil.toLocalDateString(millis: eventTime)), \
eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \
source=\(eventSource.name), \
eventFlags=\(eventFlags.toHexString()), \
index=\(index.toHexString()), \
time=\(TimeUtil.toLocalDateString(millis: time)), \
time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \
sequence=\(getSequence()), \
timeNanoPart=\(timeNanoPart), \
action=\(action), \
actionTime=\(TimeUtil.toLocalDateString(millis: actionTime)), \
actionTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: actionTime)) ?? ""), \
orderId=\(orderId), \
auxOrderId=\(auxOrderId), \
price=\(price), \
Expand Down
4 changes: 2 additions & 2 deletions DXFeedFramework/Events/Market/Extra/TradeBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ extension TradeBase {
func baseFieldsToString() -> String {
return """
\(eventSymbol), \
eventTime=\(TimeUtil.toLocalDateString(millis: eventTime)), \
time=\(TimeUtil.toLocalDateString(millis: time)), \
eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \
time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \
timeNanoPart=\(timeNanoPart), \
sequence=\(self.getSequence()), \
exchange=\(StringUtil.encodeChar(char: exchangeCode)), \
Expand Down
4 changes: 2 additions & 2 deletions DXFeedFramework/Events/Market/Greeks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ extension Greeks {
public func toString() -> String {
return """
Greeks{\(eventSymbol), \
eventTime=\(TimeUtil.toLocalDateString(millis: eventTime)), \
eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \
eventFlags=\(eventFlags.toHexString()), \
time=\(TimeUtil.toLocalDateString(millis: time)), \
time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \
sequence=\(self.getSequence()), \
price=\(price), \
volatility=\(volatility), \
Expand Down
4 changes: 2 additions & 2 deletions DXFeedFramework/Events/Market/OptionSale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ extension OptionSale {
public func toString() -> String {
return """
OptionSale{\(eventSymbol), \
eventTime=\(TimeUtil.toLocalDateString(millis: eventTime)), \
eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \
eventFlags=\(eventFlags.toHexString()), \
index=\(index.toHexString()), \
time=\(TimeUtil.toLocalDateString(millis: time)), \
time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \
timeNanoPart=\(timeNanoPart), \
sequence=\(getSequence()), \
exchange=\(StringUtil.encodeChar(char: exchangeCode)), \
Expand Down
6 changes: 3 additions & 3 deletions DXFeedFramework/Events/Market/Profile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ extension Profile {
func baseFieldsToString() -> String {
return """
\(eventSymbol), \
eventTime=\(TimeUtil.toLocalDateString(millis: eventTime)), \
eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \
description='\(descriptionStr ?? "null")', \
SSR=\(shortSaleRestriction), \
status=\(tradingStatus), \
statusReason='\(statusReason ?? "null")', \
haltStartTime=\(TimeUtil.toLocalDateStringWithoutMillis(millis: haltStartTime)), \
haltEndTime=\(TimeUtil.toLocalDateStringWithoutMillis(millis: haltEndTime)), \
haltStartTime=\((try? DXTimeFormat.defaultTimeFormat?.format(value: haltStartTime)) ?? ""), \
haltEndTime=\((try? DXTimeFormat.defaultTimeFormat?.format(value: haltEndTime)) ?? ""), \
highLimitPrice=\(highLimitPrice), \
lowLimitPrice=\(lowLimitPrice), \
high52WeekPrice=\(high52WeekPrice), \
Expand Down
8 changes: 4 additions & 4 deletions DXFeedFramework/Events/Market/Quote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ extension Quote {
func baseFieldsToString() -> String {
return """
\(eventSymbol), \
eventTime=\(TimeUtil.toLocalDateString(millis: eventTime)), \
time=\(TimeUtil.toLocalDateString(millis: time)), \
eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \
time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \
timeNanoPart=\(timeNanoPart), \
sequence=\(getSequence()), \
bidTime=\(TimeUtil.toLocalDateStringWithoutMillis(millis: bidTime)), \
bidTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: bidTime)) ?? ""), \
bidExchange=\(StringUtil.encodeChar(char: bidExchangeCode)), \
bidPrice=\(bidPrice), \
bidSize=\(bidSize), \
askTime=\(TimeUtil.toLocalDateStringWithoutMillis(millis: askTime)), \
askTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: askTime)) ?? ""), \
askExchange=\(StringUtil.encodeChar(char: askExchangeCode)), \
askPrice=\(askPrice), \
askSize=\(askSize)
Expand Down
4 changes: 2 additions & 2 deletions DXFeedFramework/Events/Market/Series.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ extension Series {
return
"""
Series{\(eventSymbol), \
eventTime=\(TimeUtil.toLocalDateString(millis: eventTime)), \
eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \
eventFlags=\(eventFlags.toHexString()), \
index=\(index.toHexString()), \
time=\(TimeUtil.toLocalDateString(millis: time)), \
time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \
sequence=\(self.getSequence()), \
expiration=\(DayUtil.getYearMonthDayByDayId(Int(expiration))), \
volatility=\(volatility), \
Expand Down
2 changes: 1 addition & 1 deletion DXFeedFramework/Events/Market/Summary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ extension Summary {
return
"""
Summary{\(eventSymbol), \
eventTime=\(TimeUtil.toLocalDateString(millis: eventTime)), \
eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \
day=\(DayUtil.getYearMonthDayByDayId(dayId)), \
dayOpen=\(dayOpenPrice), \
dayHigh=\(dayHighPrice), \
Expand Down
4 changes: 2 additions & 2 deletions DXFeedFramework/Events/Market/TheoPrice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ extension TheoPrice {
public func toString() -> String {
return """
TheoPrice{\(eventSymbol) \
eventTime=\(TimeUtil.toLocalDateString(millis: eventTime)), \
eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \
eventFlags=\(eventFlags.toHexString()), \
time=\(TimeUtil.toLocalDateString(millis: time)), \
time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \
sequence=\(self.getSequence()), \
price=\(price) \
underlyingPrice=\(underlyingPrice), \
Expand Down
4 changes: 2 additions & 2 deletions DXFeedFramework/Events/Market/TimeAndSale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ extension TimeAndSale {
public func toString() -> String {
return """
TimeAndSale{\(eventSymbol), \
eventTime=\(TimeUtil.toLocalDateString(millis: eventTime)), \
eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \
eventFlags=\(eventFlags.toHexString()), \
time=\(TimeUtil.toLocalDateString(millis: time)), \
time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \
timeNanoPart=\(timeNanoPart), \
sequence=\(getSequence()), \
exchange=\(StringUtil.encodeChar(char: exchangeCode)), \
Expand Down
4 changes: 2 additions & 2 deletions DXFeedFramework/Events/Market/Underlying.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ extension Underlying {
public func toString() -> String {
return """
Underlying{"\(eventSymbol) \
eventTime=\(TimeUtil.toLocalDateString(millis: eventTime)), \
eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \
eventFlags=\(eventFlags.toHexString()), \
time=\(TimeUtil.toLocalDateString(millis: time)), \
time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \
sequence=\(self.getSequence()), \
volatility=\(volatility), \
frontVolatility=\(frontVolatility), \
Expand Down
39 changes: 32 additions & 7 deletions DXFeedFramework/Native/ErrorHandling/ErrorCheck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,35 @@ import Foundation
/// Utility class for check native calls.
/// The location of the imported functions is in the header files "dxfg_catch_exception.h".
class ErrorCheck {

enum Result: Int32 {
case success = 0
}

static func test() throws {
let exception = GraalException.undefined
throw exception
}

@discardableResult
static func nativeCall(_ thread: OpaquePointer!, _ code: Int64) throws -> Int64 {
if code < Result.success.rawValue {
if let exception = fetchException(thread) {
throw exception
}
return code
} else {
return code
}
}

@discardableResult
static func nativeCall(_ thread: OpaquePointer!, _ code: Int32) throws -> Int32 {
if code < 0 {
throw fetchException(thread)
if code < Result.success.rawValue {
if let exception = fetchException(thread) {
throw exception
}
return code
} else {
return code
}
Expand All @@ -28,7 +49,13 @@ class ErrorCheck {
if let result = result {
return result
} else {
throw fetchException(thread)
if let exception = fetchException(thread) {
throw exception
} else {
throw GraalException.fail(message: "Something went wrong. Graal exception is empty",
className: "",
stack: "")
}
}
}

Expand All @@ -39,7 +66,7 @@ class ErrorCheck {
}
}

private static func fetchException(_ thread: OpaquePointer!) -> GraalException {
private static func fetchException(_ thread: OpaquePointer!) -> GraalException? {
let exception = dxfg_get_and_clear_thread_exception_t(thread)
if let pointee = exception?.pointee {
let message = String(pointee: pointee.message, default: "Graall Exception")
Expand All @@ -51,10 +78,8 @@ class ErrorCheck {
dxfg_Exception_release(thread, exception)
return gException
} else {
return GraalException.fail(message: "Something went wrong. Graal exception is empty",
className: "", stack: "")
return nil
}

}

}
1 change: 1 addition & 0 deletions DXFeedFramework/Native/Utils/NativeTimeFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ class NativeTimeFormat: NativeBox<dxfg_time_format_t> {
let timeFormat = try ErrorCheck.nativeCall(thread, dxfg_TimeFormat_asFullIso(thread, timeFormat.native))
self.init(native: timeFormat)
}

}
18 changes: 18 additions & 0 deletions DXFeedFramework/Native/Utils/NativeTimePeriod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,22 @@ class NativeTimePeriod: NativeBox<dxfg_time_period_t> {
let timePeriod = try ErrorCheck.nativeCall(thread, dxfg_TimePeriod_valueOf2(thread, value.toCStringRef()))
self.init(native: timePeriod)
}

func getTime() throws -> Long {
let thread = currentThread()
let result = try ErrorCheck.nativeCall(thread, dxfg_TimePeriod_getTime(thread, native))
return result
}

func getSeconds() throws -> Int32 {
let thread = currentThread()
let result = try ErrorCheck.nativeCall(thread, dxfg_TimePeriod_getSeconds(thread, native))
return result
}

func getNanos() throws -> Long {
let thread = currentThread()
let result = try ErrorCheck.nativeCall(thread, dxfg_TimePeriod_getNanos(thread, native))
return result
}
}
14 changes: 13 additions & 1 deletion DXFeedFramework/Native/Utils/NativeTimeUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,17 @@ import Foundation
@_implementationOnly import graal_api

class NativeTimeUtil {


static func parse(timeFormat: NativeTimeFormat, value: String) throws -> Long {
let thread = currentThread()
let result = try ErrorCheck.nativeCall(thread, dxfg_TimeFormat_parse(thread, timeFormat.native, value.toCStringRef()))
return result
}

static func format(timeFormat: NativeTimeFormat, value: Long) throws -> String {
let thread = currentThread()
let result = try ErrorCheck.nativeCall(thread, dxfg_TimeFormat_format(thread, timeFormat.native, value))
return String(pointee: result)
}

}
Loading

0 comments on commit 93ec5f5

Please sign in to comment.