An odd discrepancy occurs between enumerating a day of the month between two dates ahead or backward. I get the right reply when enumerating ahead, however enumerateDates jumps inexplicably when enumerating backward.
Lets attempt to enumerate dates between 2020 and 1980:
let cal = Calendar.present
var dateComponents = DateComponents()
dateComponents.12 months = 1980
dateComponents.month = 1
dateComponents.day = 1
dateComponents.timeZone = TimeZone(abbreviation: "CET")
dateComponents.hour = 8
dateComponents.minute = 0
let stopDate = cal.date(from: dateComponents)
dateComponents.12 months = 2020
dateComponents.month = 1
dateComponents.day = 1
let startDate = cal.date(from: dateComponents)
if let stopDate = stopDate {
if let startDate = startDate {
var comps = DateComponents()
//Lets enumerate ahead first
print("Enumerating ahead")
for dotm in 1..<32 {
var counter = 0
comps.day = dotm
comps.hour = 8
cal.enumerateDates(startingAfter: stopDate, matching: comps, matchingPolicy: .strict, repeatedTimePolicy: .first, course: .ahead) { (date, match, cease) in
if let date = date {
counter += 1
if date > startDate {
print("variety of months with day (dotm) in month: (counter)")
counter = 0
cease = true
}
}
}
}
//And do the identical factor backward
print("Enumerating backward")
for dotm in 1..<32 {
var counter = 0
comps.day = dotm
comps.hour = 8
cal.enumerateDates(startingAfter: startDate, matching: comps, matchingPolicy: .strict, repeatedTimePolicy: .first, course: .backward) { (date, match, cease) in
if let date = date {
counter += 1
if date < stopDate {
print("variety of months with day (dotm) in month: (counter)")
counter = 0
cease = true
}
}
}
}
//Lets examine nearer
print("What is going on?")
comps.day = 1
cal.enumerateDates(startingAfter: startDate, matching: comps, matchingPolicy: .strict, repeatedTimePolicy: .first, course: .backward) { (date, match, cease) in
if let date = date {
print("Date discovered: (date)")
if date < stopDate {
cease = true
}
}
}
}
}
The output of this code is:
Enumerating ahead
variety of months with day 1 in month: 481
variety of months with day 2 in month: 481
variety of months with day 3 in month: 481
variety of months with day 4 in month: 481
variety of months with day 5 in month: 481
variety of months with day 6 in month: 481
variety of months with day 7 in month: 481
variety of months with day 8 in month: 481
variety of months with day 9 in month: 481
variety of months with day 10 in month: 481
variety of months with day 11 in month: 481
variety of months with day 12 in month: 481
variety of months with day 13 in month: 481
variety of months with day 14 in month: 481
variety of months with day 15 in month: 481
variety of months with day 16 in month: 481
variety of months with day 17 in month: 481
variety of months with day 18 in month: 481
variety of months with day 19 in month: 481
variety of months with day 20 in month: 481
variety of months with day 21 in month: 481
variety of months with day 22 in month: 481
variety of months with day 23 in month: 481
variety of months with day 24 in month: 481
variety of months with day 25 in month: 481
variety of months with day 26 in month: 481
variety of months with day 27 in month: 481
variety of months with day 28 in month: 481
variety of months with day 29 in month: 451
variety of months with day 30 in month: 441
variety of months with day 31 in month: 281
Enumerating backward
variety of months with day 1 in month: 189
variety of months with day 2 in month: 189
variety of months with day 3 in month: 189
variety of months with day 4 in month: 189
variety of months with day 5 in month: 189
variety of months with day 6 in month: 189
variety of months with day 7 in month: 189
variety of months with day 8 in month: 189
variety of months with day 9 in month: 189
variety of months with day 10 in month: 189
variety of months with day 11 in month: 189
variety of months with day 12 in month: 189
variety of months with day 13 in month: 189
variety of months with day 14 in month: 189
variety of months with day 15 in month: 189
variety of months with day 16 in month: 189
variety of months with day 17 in month: 189
variety of months with day 18 in month: 189
variety of months with day 19 in month: 189
variety of months with day 20 in month: 189
variety of months with day 21 in month: 189
variety of months with day 22 in month: 189
variety of months with day 23 in month: 189
variety of months with day 24 in month: 189
variety of months with day 25 in month: 189
variety of months with day 26 in month: 189
variety of months with day 27 in month: 189
variety of months with day 28 in month: 189
variety of months with day 29 in month: 177
variety of months with day 30 in month: 174
variety of months with day 31 in month: 111
What is going on?
Date discovered: 2019-12-01 07:00:00 +0000
Date discovered: 2019-11-01 07:00:00 +0000
Date discovered: 2019-10-01 06:00:00 +0000
Date discovered: 1995-09-01 06:00:00 +0000
Date discovered: 1995-08-01 06:00:00 +0000
Date discovered: 1995-07-01 06:00:00 +0000
Date discovered: 1995-06-01 06:00:00 +0000
Date discovered: 1995-05-01 06:00:00 +0000
Date discovered: 1995-04-01 06:00:00 +0000
Date discovered: 1995-03-01 07:00:00 +0000
Date discovered: 1995-02-01 07:00:00 +0000
Date discovered: 1995-01-01 07:00:00 +0000
Date discovered: 1994-12-01 07:00:00 +0000
Date discovered: 1994-11-01 07:00:00 +0000
Date discovered: 1994-10-01 07:00:00 +0000
Date discovered: 1994-09-01 06:00:00 +0000
Date discovered: 1994-08-01 06:00:00 +0000
Date discovered: 1994-07-01 06:00:00 +0000
Date discovered: 1994-06-01 06:00:00 +0000
Date discovered: 1994-05-01 06:00:00 +0000
Date discovered: 1994-04-01 06:00:00 +0000
Date discovered: 1994-03-01 07:00:00 +0000
Date discovered: 1994-02-01 07:00:00 +0000
Date discovered: 1994-01-01 07:00:00 +0000
Date discovered: 1993-12-01 07:00:00 +0000
Date discovered: 1993-11-01 07:00:00 +0000
Date discovered: 1993-10-01 07:00:00 +0000
Date discovered: 1993-09-01 06:00:00 +0000
Date discovered: 1993-08-01 06:00:00 +0000
Date discovered: 1993-07-01 06:00:00 +0000
Date discovered: 1993-06-01 06:00:00 +0000
Date discovered: 1993-05-01 06:00:00 +0000
Date discovered: 1993-04-01 06:00:00 +0000
Date discovered: 1993-03-01 07:00:00 +0000
Date discovered: 1993-02-01 07:00:00 +0000
Date discovered: 1993-01-01 07:00:00 +0000
Date discovered: 1992-12-01 07:00:00 +0000
Date discovered: 1992-11-01 07:00:00 +0000
Date discovered: 1992-10-01 07:00:00 +0000
Date discovered: 1992-09-01 06:00:00 +0000
Date discovered: 1992-08-01 06:00:00 +0000
Date discovered: 1992-07-01 06:00:00 +0000
Date discovered: 1992-06-01 06:00:00 +0000
Date discovered: 1992-05-01 06:00:00 +0000
Date discovered: 1992-04-01 06:00:00 +0000
Date discovered: 1992-03-01 07:00:00 +0000
Date discovered: 1992-02-01 07:00:00 +0000
Date discovered: 1992-01-01 07:00:00 +0000
Date discovered: 1991-12-01 07:00:00 +0000
Date discovered: 1991-11-01 07:00:00 +0000
Date discovered: 1991-10-01 07:00:00 +0000
Date discovered: 1991-09-01 06:00:00 +0000
Date discovered: 1991-08-01 06:00:00 +0000
Date discovered: 1991-07-01 06:00:00 +0000
Date discovered: 1991-06-01 06:00:00 +0000
Date discovered: 1991-05-01 06:00:00 +0000
Date discovered: 1991-04-01 06:00:00 +0000
Date discovered: 1991-03-01 07:00:00 +0000
Date discovered: 1991-02-01 07:00:00 +0000
Date discovered: 1991-01-01 07:00:00 +0000
Date discovered: 1990-12-01 07:00:00 +0000
Date discovered: 1990-11-01 07:00:00 +0000
Date discovered: 1990-10-01 07:00:00 +0000
Date discovered: 1990-09-01 06:00:00 +0000
Date discovered: 1990-08-01 06:00:00 +0000
Date discovered: 1990-07-01 06:00:00 +0000
Date discovered: 1990-06-01 06:00:00 +0000
Date discovered: 1990-05-01 06:00:00 +0000
Date discovered: 1990-04-01 06:00:00 +0000
Date discovered: 1990-03-01 07:00:00 +0000
Date discovered: 1990-02-01 07:00:00 +0000
Date discovered: 1990-01-01 07:00:00 +0000
Date discovered: 1989-12-01 07:00:00 +0000
Date discovered: 1989-11-01 07:00:00 +0000
Date discovered: 1989-10-01 07:00:00 +0000
Date discovered: 1989-09-01 06:00:00 +0000
Date discovered: 1989-08-01 06:00:00 +0000
Date discovered: 1989-07-01 06:00:00 +0000
Date discovered: 1989-06-01 06:00:00 +0000
Date discovered: 1989-05-01 06:00:00 +0000
Date discovered: 1989-04-01 06:00:00 +0000
Date discovered: 1989-03-01 07:00:00 +0000
Date discovered: 1989-02-01 07:00:00 +0000
Date discovered: 1989-01-01 07:00:00 +0000
Date discovered: 1988-12-01 07:00:00 +0000
Date discovered: 1988-11-01 07:00:00 +0000
Date discovered: 1988-10-01 07:00:00 +0000
Date discovered: 1988-09-01 06:00:00 +0000
Date discovered: 1988-08-01 06:00:00 +0000
Date discovered: 1988-07-01 06:00:00 +0000
Date discovered: 1988-06-01 06:00:00 +0000
Date discovered: 1988-05-01 06:00:00 +0000
Date discovered: 1988-04-01 06:00:00 +0000
Date discovered: 1988-03-01 07:00:00 +0000
Date discovered: 1988-02-01 07:00:00 +0000
Date discovered: 1988-01-01 07:00:00 +0000
Date discovered: 1987-12-01 07:00:00 +0000
Date discovered: 1987-11-01 07:00:00 +0000
Date discovered: 1987-10-01 07:00:00 +0000
Date discovered: 1987-09-01 06:00:00 +0000
Date discovered: 1987-08-01 06:00:00 +0000
Date discovered: 1987-07-01 06:00:00 +0000
Date discovered: 1987-06-01 06:00:00 +0000
Date discovered: 1987-05-01 06:00:00 +0000
Date discovered: 1987-04-01 06:00:00 +0000
Date discovered: 1987-03-01 07:00:00 +0000
Date discovered: 1987-02-01 07:00:00 +0000
Date discovered: 1987-01-01 07:00:00 +0000
Date discovered: 1986-12-01 07:00:00 +0000
Date discovered: 1986-11-01 07:00:00 +0000
Date discovered: 1986-10-01 07:00:00 +0000
Date discovered: 1986-09-01 06:00:00 +0000
Date discovered: 1986-08-01 06:00:00 +0000
Date discovered: 1986-07-01 06:00:00 +0000
Date discovered: 1986-06-01 06:00:00 +0000
Date discovered: 1986-05-01 06:00:00 +0000
Date discovered: 1986-04-01 06:00:00 +0000
Date discovered: 1986-03-01 07:00:00 +0000
Date discovered: 1986-02-01 07:00:00 +0000
Date discovered: 1986-01-01 07:00:00 +0000
Date discovered: 1985-12-01 07:00:00 +0000
Date discovered: 1985-11-01 07:00:00 +0000
Date discovered: 1985-10-01 07:00:00 +0000
Date discovered: 1985-09-01 06:00:00 +0000
Date discovered: 1985-08-01 06:00:00 +0000
Date discovered: 1985-07-01 06:00:00 +0000
Date discovered: 1985-06-01 06:00:00 +0000
Date discovered: 1985-05-01 06:00:00 +0000
Date discovered: 1985-04-01 06:00:00 +0000
Date discovered: 1985-03-01 07:00:00 +0000
Date discovered: 1985-02-01 07:00:00 +0000
Date discovered: 1985-01-01 07:00:00 +0000
Date discovered: 1984-12-01 07:00:00 +0000
Date discovered: 1984-11-01 07:00:00 +0000
Date discovered: 1984-10-01 07:00:00 +0000
Date discovered: 1984-09-01 06:00:00 +0000
Date discovered: 1984-08-01 06:00:00 +0000
Date discovered: 1984-07-01 06:00:00 +0000
Date discovered: 1984-06-01 06:00:00 +0000
Date discovered: 1984-05-01 06:00:00 +0000
Date discovered: 1984-04-01 06:00:00 +0000
Date discovered: 1984-03-01 07:00:00 +0000
Date discovered: 1984-02-01 07:00:00 +0000
Date discovered: 1984-01-01 07:00:00 +0000
Date discovered: 1983-12-01 07:00:00 +0000
Date discovered: 1983-11-01 07:00:00 +0000
Date discovered: 1983-10-01 07:00:00 +0000
Date discovered: 1983-09-01 06:00:00 +0000
Date discovered: 1983-08-01 06:00:00 +0000
Date discovered: 1983-07-01 06:00:00 +0000
Date discovered: 1983-06-01 06:00:00 +0000
Date discovered: 1983-05-01 06:00:00 +0000
Date discovered: 1983-04-01 06:00:00 +0000
Date discovered: 1983-03-01 07:00:00 +0000
Date discovered: 1983-02-01 07:00:00 +0000
Date discovered: 1983-01-01 07:00:00 +0000
Date discovered: 1982-12-01 07:00:00 +0000
Date discovered: 1982-11-01 07:00:00 +0000
Date discovered: 1982-10-01 07:00:00 +0000
Date discovered: 1982-09-01 06:00:00 +0000
Date discovered: 1982-08-01 06:00:00 +0000
Date discovered: 1982-07-01 06:00:00 +0000
Date discovered: 1982-06-01 06:00:00 +0000
Date discovered: 1982-05-01 06:00:00 +0000
Date discovered: 1982-04-01 06:00:00 +0000
Date discovered: 1982-03-01 07:00:00 +0000
Date discovered: 1982-02-01 07:00:00 +0000
Date discovered: 1982-01-01 07:00:00 +0000
Date discovered: 1981-12-01 07:00:00 +0000
Date discovered: 1981-11-01 07:00:00 +0000
Date discovered: 1981-10-01 07:00:00 +0000
Date discovered: 1981-09-01 06:00:00 +0000
Date discovered: 1981-08-01 06:00:00 +0000
Date discovered: 1981-07-01 06:00:00 +0000
Date discovered: 1981-06-01 06:00:00 +0000
Date discovered: 1981-05-01 06:00:00 +0000
Date discovered: 1981-04-01 06:00:00 +0000
Date discovered: 1981-03-01 07:00:00 +0000
Date discovered: 1981-02-01 07:00:00 +0000
Date discovered: 1981-01-01 07:00:00 +0000
Date discovered: 1980-12-01 07:00:00 +0000
Date discovered: 1980-11-01 07:00:00 +0000
Date discovered: 1980-10-01 07:00:00 +0000
Date discovered: 1980-09-01 06:00:00 +0000
Date discovered: 1980-08-01 06:00:00 +0000
Date discovered: 1980-07-01 06:00:00 +0000
Date discovered: 1980-06-01 06:00:00 +0000
Date discovered: 1980-05-01 06:00:00 +0000
Date discovered: 1976-04-01 07:00:00 +0000
When counting backward there’s a unusual bounce from 2019 to 1995 and once more from 1980 to 1976. Am I doing one thing incorrect or is that this a bug in enumerateDares? I’m utilizing Xcode 11.3.
Replace:
The conduct of enumerateDates when enumerating backwards is even stranger. After we change startDate, the counting is typically appropriate. A fast verify of all of the potential begin days in January 2020 offers us the next matrix (the primary column being startDate, the second column the variety of days with dateComponents.day = 1, the third column dateComponents.day = 2, and many others …)
2020-01-01 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 177 174 111
2020-01-02 482 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 177 174 111
2020-01-03 482 482 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 177 174 111
2020-01-04 482 482 482 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 177 174 111
2020-01-05 482 482 482 482 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 177 174 111
...
2020-01-27 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 189 189 177 174 111
2020-01-28 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 189 177 174 111
2020-01-29 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 201 177 174 111
2020-01-30 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 201 189 174 111
2020-01-31 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 482 201 189 174 111
So possibly enumerateDates solely counts appropriately if the primary hit is within the present month?