Skip to content

Commit e4c52f5

Browse files
authored
Merge pull request #1298 from sashabelonogov/sashabelonogov/SR-6303
2 parents 03e2846 + 491f832 commit e4c52f5

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

CoreFoundation/Locale.subproj/CFCalendar.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -868,10 +868,12 @@ Boolean _CFCalendarComposeAbsoluteTimeV(CFCalendarRef calendar, /* out */ CFAbso
868868
ch = *desc;
869869
while (ch) {
870870
UCalendarDateFields field = __CFCalendarGetICUFieldCodeFromChar(ch);
871-
int value = *vector;
872-
if (UCAL_YEAR == field && doWOY) field = UCAL_YEAR_WOY;
873-
if (UCAL_MONTH == field) value--;
874-
ucal_set(calendar->_cal, field, value);
871+
if (field != (UCalendarDateFields)-1) {
872+
int value = *vector;
873+
if (UCAL_YEAR == field && doWOY) field = UCAL_YEAR_WOY;
874+
if (UCAL_MONTH == field) value--;
875+
ucal_set(calendar->_cal, field, value);
876+
}
875877
vector++;
876878
desc++;
877879
ch = *desc;
@@ -894,9 +896,13 @@ Boolean _CFCalendarDecomposeAbsoluteTimeV(CFCalendarRef calendar, CFAbsoluteTime
894896
char ch = *componentDesc;
895897
while (ch) {
896898
UCalendarDateFields field = __CFCalendarGetICUFieldCodeFromChar(ch);
897-
int value = ucal_get(calendar->_cal, field, &status);
898-
if (UCAL_MONTH == field) value++;
899-
*(*vector) = value;
899+
if (field == (UCalendarDateFields)-1) {
900+
*(*vector) = -1;
901+
} else {
902+
int value = ucal_get(calendar->_cal, field, &status);
903+
if (UCAL_MONTH == field) value++;
904+
*(*vector) = value;
905+
}
900906
vector++;
901907
componentDesc++;
902908
ch = *componentDesc;

Foundation/NSCalendar.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
449449
private func _convert(_ comps: DateComponents) -> (Array<Int32>, Array<Int8>) {
450450
var vector = [Int32]()
451451
var compDesc = [Int8]()
452-
_convert(comps.era, type: "E", vector: &vector, compDesc: &compDesc)
452+
_convert(comps.era, type: "G", vector: &vector, compDesc: &compDesc)
453453
_convert(comps.year, type: "y", vector: &vector, compDesc: &compDesc)
454454
_convert(comps.quarter, type: "Q", vector: &vector, compDesc: &compDesc)
455455
if comps.weekOfYear != NSDateComponentUndefined {
@@ -520,7 +520,9 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
520520

521521
private func _setComp(_ unitFlags: Unit, field: Unit, vector: [Int32], compIndex: inout Int, setter: (Int32) -> Void) {
522522
if unitFlags.contains(field) {
523-
setter(vector[compIndex])
523+
if vector[compIndex] != -1 {
524+
setter(vector[compIndex])
525+
}
524526
compIndex += 1
525527
}
526528
}

TestFoundation/TestDate.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class TestDate : XCTestCase {
3636
("test_Compare", test_Compare),
3737
("test_IsEqualToDate", test_IsEqualToDate),
3838
("test_timeIntervalSinceReferenceDate", test_timeIntervalSinceReferenceDate),
39+
("test_recreateDateComponentsFromDate", test_recreateDateComponentsFromDate),
3940
]
4041
}
4142

@@ -123,4 +124,40 @@ class TestDate : XCTestCase {
123124
XCTAssertTrue(d1 <= sinceReferenceDate)
124125
XCTAssertTrue(d2 >= sinceReferenceDate)
125126
}
127+
128+
func test_recreateDateComponentsFromDate() {
129+
let components = DateComponents(calendar: Calendar(identifier: .gregorian),
130+
timeZone: .current,
131+
era: 1,
132+
year: 2017,
133+
month: 11,
134+
day: 5,
135+
hour: 20,
136+
minute: 38,
137+
second: 11,
138+
nanosecond: 40)
139+
guard let date = Calendar(identifier: .gregorian).date(from: components) else {
140+
XCTFail()
141+
return
142+
}
143+
let recreatedComponents = Calendar(identifier: .gregorian).dateComponents(in: .current, from: date)
144+
XCTAssertEqual(recreatedComponents.era, 1)
145+
XCTAssertEqual(recreatedComponents.year, 2017)
146+
XCTAssertEqual(recreatedComponents.month, 11)
147+
XCTAssertEqual(recreatedComponents.hour, 20)
148+
XCTAssertEqual(recreatedComponents.minute, 38)
149+
150+
// Nanoseconds are currently not supported by UCalendar C API, returns nil
151+
// XCTAssertEqual(recreatedComponents.nanosecond, 40)
152+
153+
XCTAssertEqual(recreatedComponents.weekday, 1)
154+
XCTAssertEqual(recreatedComponents.weekdayOrdinal, 1)
155+
156+
// Quarter is currently not supported by UCalendar C API, returns nil
157+
// XCTAssertEqual(recreatedComponents.quarter, 3)
158+
159+
XCTAssertEqual(recreatedComponents.weekOfMonth, 2)
160+
XCTAssertEqual(recreatedComponents.weekOfYear, 45)
161+
XCTAssertEqual(recreatedComponents.yearForWeekOfYear, 2017)
162+
}
126163
}

0 commit comments

Comments
 (0)