Skip to content

Instantly share code, notes, and snippets.

@patoroco
Last active March 16, 2016 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patoroco/acfa59d184f9e6d12f0b to your computer and use it in GitHub Desktop.
Save patoroco/acfa59d184f9e6d12f0b to your computer and use it in GitHub Desktop.
Bug with NSDateComponents (execute this method in simulator and in physical device to see different results)
😑 2000 --> 1999-01
😏 2001 --> 2001-01
😏 2002 --> 2002-01
😏 2003 --> 2003-01
😏 2004 --> 2004-01
😑 2005 --> 2004-01
😑 2006 --> 2005-01
😏 2007 --> 2007-01
😏 2008 --> 2008-01
😏 2009 --> 2009-01
😑 2010 --> 2009-01
😑 2011 --> 2010-01
😑 2012 --> 2011-01
😏 2013 --> 2013-01
😏 2014 --> 2014-01
😏 2015 --> 2015-01
😑 2016 --> 2015-01
😑 2017 --> 2016-01
😏 2018 --> 2018-01
😏 2019 --> 2019-01
😏 2020 --> 2020-01
😑 2021 --> 2020-01
😑 2022 --> 2021-01
😑 2023 --> 2022-01
😏 2024 --> 2024-01
😏 2025 --> 2025-01
😏 2026 --> 2026-01
😑 2027 --> 2026-01
😑 2028 --> 2027-01
😏 2029 --> 2029-01
😏 2030 --> 2030-01
😏 2031 --> 2031-01
😏 2032 --> 2032-01
😑 2033 --> 2032-01
😑 2034 --> 2033-01
😏 2035 --> 2035-01
😏 2036 --> 2036-01
😏 2037 --> 2037-01
😑 2038 --> 2037-01
😑 2039 --> 2038-01
😑 2040 --> 2039-01
😏 2041 --> 2041-01
😏 2042 --> 2042-01
😏 2043 --> 2043-01
😑 2044 --> 2043-01
😑 2045 --> 2044-01
😏 2046 --> 2046-01
😏 2047 --> 2047-01
😏 2048 --> 2048-01
😑 2049 --> 2048-01
- (void)printDate {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateFormatter *f2 = [NSDateFormatter new];
f2.dateFormat = @"YYYY-MM";
for (int i = 2000; i < 2050; i++) {
NSDateComponents *monthComp = [NSDateComponents new];
monthComp.year = i;
monthComp.month = 1;
NSDate *dateMonth = [calendar dateFromComponents:monthComp];
NSString *generated = [f2 stringFromDate:dateMonth];
NSString *expected = [NSString stringWithFormat:@"%d-01", i];
NSString *icon = [generated isEqualToString:expected] ? @"😏" : @"😑";
NSLog(@"%@ %d --> %@", icon, i, generated);
}
}
func printDate() {
let calendar = NSCalendar.currentCalendar()
let f2 = NSDateFormatter()
f2.dateFormat = "YYYY-MM"
for i in 2000...2050 {
let comp = NSDateComponents()
comp.year = i
comp.month = 1
let date = calendar.dateFromComponents(comp)!
let gen = f2.stringFromDate(date)
let exp = "\(i)-01"
let icon = (gen == exp) ? "😏" : "😑"
print("\(icon) \(i) -> \(gen)")
}
}
😏 2000 --> 2000-01
😏 2001 --> 2001-01
😏 2002 --> 2002-01
😏 2003 --> 2003-01
😏 2004 --> 2004-01
😏 2005 --> 2005-01
😏 2006 --> 2006-01
😏 2007 --> 2007-01
😏 2008 --> 2008-01
😏 2009 --> 2009-01
😏 2010 --> 2010-01
😏 2011 --> 2011-01
😏 2012 --> 2012-01
😏 2013 --> 2013-01
😏 2014 --> 2014-01
😏 2015 --> 2015-01
😏 2016 --> 2016-01
😏 2017 --> 2017-01
😏 2018 --> 2018-01
😏 2019 --> 2019-01
😏 2020 --> 2020-01
😏 2021 --> 2021-01
😏 2022 --> 2022-01
😏 2023 --> 2023-01
😏 2024 --> 2024-01
😏 2025 --> 2025-01
😏 2026 --> 2026-01
😏 2027 --> 2027-01
😏 2028 --> 2028-01
😏 2029 --> 2029-01
😏 2030 --> 2030-01
😏 2031 --> 2031-01
😏 2032 --> 2032-01
😏 2033 --> 2033-01
😏 2034 --> 2034-01
😏 2035 --> 2035-01
😏 2036 --> 2036-01
😏 2037 --> 2037-01
😏 2038 --> 2038-01
😏 2039 --> 2039-01
😏 2040 --> 2040-01
😏 2041 --> 2041-01
😏 2042 --> 2042-01
😏 2043 --> 2043-01
😏 2044 --> 2044-01
😏 2045 --> 2045-01
😏 2046 --> 2046-01
😏 2047 --> 2047-01
😏 2048 --> 2048-01
😏 2049 --> 2049-01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment