In my app, I try to change the normal start & end of the day. So, I will set the start of the day to be at 04:00:00 and the end of the day to be next day at 03:59:59 and consider different timezones
My code is as following
NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *comps = [gregorian
components:NSYearCalendarUnit|NSMonthCalendarUnit|
NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|
NSTimeZoneCalendarUnit fromDate:date];
[comps setTimeZone:[NSTimeZone localTimeZone]];
// Start of Day
comps.hour = 4;
//comps.hour = 4 + ([[NSTimeZone localTimeZone] secondsFromGMT] / 60 / 60);
comps.minute = 0;
comps.second = 0;
NSDate *startOfDay = [gregorian dateFromComponents:comps];
NSLog(@"Start of day %@", startOfDay);
//Prints Out: Start of day 2014-04-16 02:00:00 +0000
// End of Day
comps.day = comps.day +1;
comps.second = -1;
NSDate *endOfDay = [gregorian dateFromComponents:comps];
NSLog(@"End of day %@", endOfDay);
//Prints Out: End of day 2014-04-17 01:59:59 +0000
Please, I have 3 questions:
1. Why the NSGregorianCalendar ignores setting its property timezone and I must add
the timezone to the number of hours ?
2. Also, Why the timezone is not printed?
3. Is the shown code for computing end of day, good?