-1

I have tried changing my functions that get the start of day/end of day NSDates several times and I know the loop is working bc its printing out the right days of the week, but the start_timestamp and end_timestamp for each day is showing up the same for some reason. It has to be something involving the int not changing or something not directly related to the logic im not seeing:

Here is what its printing out:

WEEK_ARRAY:{
    Friday =     {
        "DAY_END" = 1454389199;
        "DAY_START" = 1454302800;
    };
    Monday =     {
        "DAY_END" = 1454389199;
        "DAY_START" = 1454302800;
    };
    Saturday =     {
        "DAY_END" = 1454389199;
        "DAY_START" = 1454302800;
    };
    Sunday =     {
        "DAY_END" = 1454389199;
        "DAY_START" = 1454302800;
    };
    Thursday =     {
        "DAY_END" = 1454389199;
        "DAY_START" = 1454302800;
    };
    Tuesday =     {
        "DAY_END" = 1454389199;
        "DAY_START" = 1454302800;
    };
    Wednesday =     {
        "DAY_END" = 1454389199;
        "DAY_START" = 1454302800;
    };
}

Here is the logic:

- (NSMutableDictionary *)lastSevenDays {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"EEEE"];

    NSDate *date = [NSDate date];
    NSMutableDictionary *weekDays = [[NSMutableDictionary alloc] init];
    for (int i = 0; i <7; i++) {
        NSString *weekDay = [formatter stringFromDate:date];
        date = [self dateByAddingOneDayFromDate:date];
        NSMutableDictionary *specificDayDict=[[NSMutableDictionary alloc]init];

        NSDate *StartNSDate=[self beginningOfDay:date];

        NSNumber *StartTstamp=[NSNumber numberWithInt:[self convertNSDateToTimestamp:StartNSDate]];


        int endOfDay=[self convertNSDateToTimestamp:[self endOfDay:date]];

        NSLog(@"DAY:%@ | DAY_START:%@ | DAY_END:%d",weekDay,StartTstamp,endOfDay);

        [specificDayDict setValue:StartTstamp forKey:@"DAY_START"];
        [specificDayDict setValue:[NSNumber numberWithInt:endOfDay] forKey:@"DAY_END"];
        [weekDays setObject:specificDayDict forKey:weekDay];
    }
    return weekDays;
}
- (NSDate *)dateByAddingOneDayFromDate:(NSDate *)date {
    NSCalendar *cal = [NSCalendar currentCalendar];

    NSDateComponents *plusOneDay = [[NSDateComponents alloc] init];
    [plusOneDay setDay:+1];
    NSDate *newDate = [cal dateByAddingComponents:plusOneDay
                                           toDate:date
                                          options:NSWrapCalendarComponents];
    return newDate;
}

-(NSDate *)beginningOfDay:(NSDate *)date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];

    [components setHour:0];
    [components setMinute:0];
    [components setSecond:0];

    return [cal dateFromComponents:components];

}

-(NSDate *)endOfDay:(NSDate *)date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];

    [components setHour:23];
    [components setMinute:59];
    [components setSecond:59];

    return [cal dateFromComponents:components];

}

-(NSDate *)convertTimestampToNSDate:(int)timestamp{

    NSDate* date = [NSDate dateWithTimeIntervalSince1970:timestamp];

    return date;
}
-(int )convertNSDateToTimestamp:(NSDate *)date{

    int timestamp=[date timeIntervalSince1970];
    return timestamp;
}
ChuckKelly
  • 1,742
  • 5
  • 25
  • 54
  • 1
    Wow that's a lot code. I'm sure most of it could be thrown away. For example why have a `beginningOfDay:` method that you call for every day when you could just get the beginning of today and subtract one whole day each iteration? – trojanfoe Feb 05 '16 at 08:00
  • Please dont judge, its 3am. Im just looking for a answer.WHY IT REPEAT SO MUCH?!?! – ChuckKelly Feb 05 '16 at 08:03
  • 2
    "Don't judge" Heheh. You've come to the wrong place. – trojanfoe Feb 05 '16 at 08:08
  • Please don't [delete and repost](http://stackoverflow.com/questions/35218318/nsdictonary-of-nsdictionarys-containing-start-end-timestamp-of-each-of-next-7-da?noredirect=1). – jscs Feb 05 '16 at 08:17
  • You might also want to look at [`-[NSCalendar enumerateDatesStartingAfterDate:matchingComponents:options:usingBlock:]`](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/occ/instm/NSCalendar/enumerateDatesStartingAfterDate:matchingComponents:options:usingBlock:) and [`-[NSCalendar dateBySettingHour:minute:second:ofDate:options:]`](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/occ/instm/NSCalendar/dateBySettingHour:minute:second:ofDate:options:) – jscs Feb 05 '16 at 08:25
  • 1
    Don't write code at 3am. It will take you more time tomorrow to clean it up than it would have taken to write it in the first place if you weren't so tired. See vadian's post. – gnasher729 Feb 05 '16 at 08:44

2 Answers2

2

The reason is the missing NSDayCalendarUnit component in both beginningOfDay and endOfDay methods.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • That worked for the timestamp, but now im realizing the days are not in order for some reason? See the log its giving me Friday, Monday, Saturday, Sunday? – ChuckKelly Feb 05 '16 at 10:41
  • That reason is because dictionaries are unordered by definition. If you want to preserve the order use an array and add the weekday to the dictionary. – vadian Feb 05 '16 at 10:42
0

In beginningOfDay and endOfDay functions you have missed NSCalendarUnitDay. Also NSMonthCalendarUnit, NSYearCalendarUnit are deprecated in ios8.

-(NSDate *)beginningOfDay:(NSDate *)date{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay| NSCalendarUnitHour  | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];

[components setHour:0];
[components setMinute:0];
[components setSecond:0];

return [cal dateFromComponents:components];

}

-(NSDate *)endOfDay:(NSDate *)date{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay| NSCalendarUnitHour  | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];

[components setHour:23];
[components setMinute:59];
[components setSecond:59];

return [cal dateFromComponents:components];

}

Sahana Kini
  • 562
  • 2
  • 8
  • 2
    This isn't very different to the other answer. You should have added a comment about the deprecation to that other answer. – trojanfoe Feb 05 '16 at 08:48