Skip to content

[gardening] Remove duplicate code in NSCalendar #1628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 15 additions & 105 deletions Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -623,41 +623,11 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
*/
open func getEra(_ eraValuePointer: UnsafeMutablePointer<Int>?, year yearValuePointer: UnsafeMutablePointer<Int>?, month monthValuePointer: UnsafeMutablePointer<Int>?, day dayValuePointer: UnsafeMutablePointer<Int>?, from date: Date) {
let comps = components([.era, .year, .month, .day], from: date)
if let value = comps.era {
eraValuePointer?.pointee = value
} else {
eraValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.year {
yearValuePointer?.pointee = value
} else {
yearValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.month {
monthValuePointer?.pointee = value
} else {
monthValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.day {
dayValuePointer?.pointee = value
} else {
dayValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.year {
yearValuePointer?.pointee = value
} else {
yearValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.month {
monthValuePointer?.pointee = value
} else {
monthValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.day {
dayValuePointer?.pointee = value
} else {
dayValuePointer?.pointee = NSDateComponentUndefined
}

eraValuePointer?.pointee = comps.era ?? NSDateComponentUndefined
yearValuePointer?.pointee = comps.year ?? NSDateComponentUndefined
monthValuePointer?.pointee = comps.month ?? NSDateComponentUndefined
dayValuePointer?.pointee = comps.day ?? NSDateComponentUndefined
}

/*
Expand All @@ -666,41 +636,11 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
*/
open func getEra(_ eraValuePointer: UnsafeMutablePointer<Int>?, yearForWeekOfYear yearValuePointer: UnsafeMutablePointer<Int>?, weekOfYear weekValuePointer: UnsafeMutablePointer<Int>?, weekday weekdayValuePointer: UnsafeMutablePointer<Int>?, from date: Date) {
let comps = components([.era, .yearForWeekOfYear, .weekOfYear, .weekday], from: date)
if let value = comps.era {
eraValuePointer?.pointee = value
} else {
eraValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.yearForWeekOfYear {
yearValuePointer?.pointee = value
} else {
yearValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.weekOfYear {
weekValuePointer?.pointee = value
} else {
weekValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.weekday {
weekdayValuePointer?.pointee = value
} else {
weekdayValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.yearForWeekOfYear {
yearValuePointer?.pointee = value
} else {
yearValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.weekOfYear {
weekValuePointer?.pointee = value
} else {
weekValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.weekday {
weekdayValuePointer?.pointee = value
} else {
weekdayValuePointer?.pointee = NSDateComponentUndefined
}

eraValuePointer?.pointee = comps.era ?? NSDateComponentUndefined
yearValuePointer?.pointee = comps.year ?? NSDateComponentUndefined
weekValuePointer?.pointee = comps.weekOfYear ?? NSDateComponentUndefined
weekdayValuePointer?.pointee = comps.weekday ?? NSDateComponentUndefined
}

/*
Expand All @@ -709,41 +649,11 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
*/
open func getHour(_ hourValuePointer: UnsafeMutablePointer<Int>?, minute minuteValuePointer: UnsafeMutablePointer<Int>?, second secondValuePointer: UnsafeMutablePointer<Int>?, nanosecond nanosecondValuePointer: UnsafeMutablePointer<Int>?, from date: Date) {
let comps = components([.hour, .minute, .second, .nanosecond], from: date)
if let value = comps.hour {
hourValuePointer?.pointee = value
} else {
hourValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.minute {
minuteValuePointer?.pointee = value
} else {
minuteValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.second {
secondValuePointer?.pointee = value
} else {
secondValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.nanosecond {
nanosecondValuePointer?.pointee = value
} else {
nanosecondValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.minute {
minuteValuePointer?.pointee = value
} else {
minuteValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.second {
secondValuePointer?.pointee = value
} else {
secondValuePointer?.pointee = NSDateComponentUndefined
}
if let value = comps.nanosecond {
nanosecondValuePointer?.pointee = value
} else {
nanosecondValuePointer?.pointee = NSDateComponentUndefined
}

hourValuePointer?.pointee = comps.hour ?? NSDateComponentUndefined
minuteValuePointer?.pointee = comps.minute ?? NSDateComponentUndefined
secondValuePointer?.pointee = comps.second ?? NSDateComponentUndefined
nanosecondValuePointer?.pointee = comps.nanosecond ?? NSDateComponentUndefined
}

/*
Expand Down