diff --git a/DXFeedFramework/Native/Schedule/NativeSchedule.swift b/DXFeedFramework/Native/Schedule/NativeSchedule.swift index 14cde87ea..eae8ddf31 100644 --- a/DXFeedFramework/Native/Schedule/NativeSchedule.swift +++ b/DXFeedFramework/Native/Schedule/NativeSchedule.swift @@ -85,6 +85,12 @@ class NativeSchedule { return try createDay(thread, day) } + public func getDayByYearMonthDay(yearMonthDay: Int32) throws -> ScheduleDay { + let thread = currentThread() + let day = try ErrorCheck.nativeCall(thread, dxfg_Schedule_getDayByYearMonthDay(thread, schedule, yearMonthDay)) + return try createDay(thread, day) + } + private func createDay(_ thread: OpaquePointer?, _ day: UnsafeMutablePointer) throws -> ScheduleDay { let scheduleDay = ScheduleDay() diff --git a/DXFeedFramework/Schedule/DXSchedule.swift b/DXFeedFramework/Schedule/DXSchedule.swift index 00586d0a0..128e2bb09 100644 --- a/DXFeedFramework/Schedule/DXSchedule.swift +++ b/DXFeedFramework/Schedule/DXSchedule.swift @@ -96,6 +96,23 @@ public class DXSchedule { return day } + /// Returns day for specified year, month and day numbers. + /// + /// Year, month, and day numbers shall be decimally packed in the following way: + /// YearMonthDay = year * 10000 + month * 100 + day + /// For example, September 28, 1977 has value 19770928. + /// If specified day does not exist then this method returns day with + /// the lowest valid YearMonthDay that is greater than specified one. + /// This method will throw {@link IllegalArgumentException} if specified year, month and day numbers + /// fall outside of valid date range from 0001-01-02 to 9999-12-30. + /// - Parameters: + /// - yearMonthDay: year, month and day numbers to search for + /// - Throws: GraalException. Rethrows exception from Java.recore + public func getDayByYearMonthDay(yearMonthDay: Int32) throws -> ScheduleDay { + let day = try native.getDayByYearMonthDay(yearMonthDay: yearMonthDay) + return day + } + /// Returns session that contains specified time. /// /// This method will throw exception diff --git a/DXFeedFrameworkTests/ScheduleTest.swift b/DXFeedFrameworkTests/ScheduleTest.swift index 22e597f1a..572b31665 100644 --- a/DXFeedFrameworkTests/ScheduleTest.swift +++ b/DXFeedFrameworkTests/ScheduleTest.swift @@ -234,4 +234,31 @@ final class ScheduleTest: XCTestCase { } } + func testSetDefaults() throws { + let goodHoliday = 20170111 + let badHoliday = 20170118 + let worstHoliday = 20170125 + let def = "date=30000101-000000+0000\n\nhd.GOOD=\\\n" + "\(goodHoliday)" + ",\\\n\n" + // test that changing defaults works by adding new holidays list + try DXSchedule.setDefaults(def.data(using: .utf8)!) + let goodSchedule = try DXSchedule(scheduleDefinition: "(tz=GMT;0=;hd=GOOD)") + try checkHoliday(goodSchedule, goodHoliday) + // test that changing defaults works again by adding yet another holidays list + try DXSchedule.setDefaults((def + "hd.BAD=\\\n" + "\(badHoliday)" + ",\\").data(using: .utf8)!) + let badSchedule = try DXSchedule(scheduleDefinition: "(tz=GMT;0=;hd=BAD)") + try checkHoliday(goodSchedule, goodHoliday) + try checkHoliday(badSchedule, badHoliday) + // test that replacing holidays list with new value works and affects old schedule instance + try DXSchedule.setDefaults((def + "hd.BAD=\\\n" + "\(worstHoliday)" + ",\\").data(using: .utf8)!) + try checkHoliday(goodSchedule, goodHoliday) + XCTAssertFalse(try badSchedule.getDayByYearMonthDay(yearMonthDay: Int32(badHoliday)).holiday == 1) + try checkHoliday(badSchedule, worstHoliday) + } + + private func checkHoliday(_ schedule: DXSchedule, _ holiday: Int) throws { + for index in holiday-1...holiday+1 { + let day = try schedule.getDayByYearMonthDay(yearMonthDay: Int32(index)) + XCTAssert((index == holiday) == (day.holiday == 1)) + } + } }