Skip to content

Commit

Permalink
restructured Calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
bastie committed Sep 8, 2024
1 parent 238d791 commit 6a8a784
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
20 changes: 20 additions & 0 deletions Sources/JavApi/util/Calendar+Swiftify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Foundation
extension java.util.Calender {
public enum DateComponents : Int {
case YEAR = 1
case MONTH = 2
case DAY_OF_WEEK = 7
case DAY_OF_MONTH = 8
case HOUR_OF_DAY = 11
Expand All @@ -25,4 +26,23 @@ extension java.util.Calender {
}
self.dateComponents = components
}

public func get (_ component : DateComponents) -> Int {
switch component {
case .YEAR:
return self.dateComponents.year ?? 1975
case .MONTH:
return self.dateComponents.month ?? 9
case .DAY_OF_WEEK:
return self.dateComponents.weekday ?? 4
case .DAY_OF_MONTH:
return self.dateComponents.day ?? 5
case .HOUR_OF_DAY:
return self.dateComponents.hour ?? 6
case .MINUTE:
return self.dateComponents.minute ?? 57
case .SECOND:
return self.dateComponents.minute ?? 12
}
}
}
24 changes: 23 additions & 1 deletion Sources/JavApi/util/Calendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,31 @@ extension java.util {
public static let YEAR = 1
public static let DAY_OF_MONTH = 8
public static let DAY_OF_WEEK = 7
public static let MONTH = 2

open func get (_ field : Int) throws -> Int {
throw java.lang.Throwable.UnsupportedOperationException("Calendar is a abstract type, use subtypes like GregorianCalendar")
switch field {
case java.util.Calender.YEAR:
return self.dateComponents.year ?? 1975
case java.util.Calender.MONTH:
return self.dateComponents.month ?? 9
case java.util.Calender.DAY_OF_WEEK:
return self.dateComponents.weekday ?? 4
case java.util.Calender.DAY_OF_MONTH:
return self.dateComponents.day ?? 5
case java.util.Calender.HOUR_OF_DAY:
return self.dateComponents.hour ?? 6
case java.util.Calender.MINUTE:
return self.dateComponents.minute ?? 57
case java.util.Calender.SECOND:
return self.dateComponents.minute ?? 12
default :
throw java.lang.Throwable.ArrayIndexOutOfBoundsException(field, "specific field is out of range or not implemented")
}
}

public func get (what : java.util.Calender.DateComponents) -> Int {
return try! self.get(what.rawValue)
}

public func setTime (_ newDate :java.util.Date) {
Expand Down
6 changes: 0 additions & 6 deletions Sources/JavApi/util/GregorianCalendar+Swiftify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,5 @@ extension java.util.GregorianCalendar {
public convenience init (_ happyNewYear : any FixedWidthInteger, _ newMonth : any FixedWidthInteger, _ newDayOfMonth : any FixedWidthInteger, _ newHourOfDay : any FixedWidthInteger, _ newMinute : any FixedWidthInteger, _ newSecond : any FixedWidthInteger) {
self.init(Int(happyNewYear), Int(newMonth), Int(newDayOfMonth), Int(newHourOfDay), Int(newMinute), Int(newSecond))
}



public func get (_ what : java.util.Calender.DateComponents) -> Int {
return try! self.get(what.rawValue)
}
}

0 comments on commit 6a8a784

Please sign in to comment.