NSDateComponents - Combining a date and time loses 1 hour? - objective-c

I am trying to combine a NSDate (date) with a NSDate (time) into a new NSDate object, for whatever reason I seem to be losing 1 hour.
I have an array of formatted time objects that are 15 minutes between each one.
I have a selectable date item, which referes to a NSDate object.
A user selects a formatted time object (I have a reference to the NSDate in a dictionary) and then when it comes to save; I do this;
if (self.selectedDate && self.selectedTimeObject)
{
// Combine selected date and selected time into a new NSDate object
NSCalendar *cal = [NSCalendar currentCalendar];
unsigned unitFlagsDate = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
unsigned unitFlagsTime = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponents = [cal components:unitFlagsDate fromDate:self.selectedDate];
NSDateComponents *timeComponents = [cal components:unitFlagsTime fromDate:self.selectedTimeObject];
[dateComponents setSecond:[timeComponents second]];
[dateComponents setHour:[timeComponents hour]];
[dateComponents setMinute:[timeComponents minute]];
NSDate *combinedDate = [cal dateFromComponents:dateComponents];
NSLog(#"targetDate = %#", combinedDate);
}
In my example I choose 01:00; but for some reason I lose 1 hour depending on the date.
Logs;
targetDate = 2015-03-31 00:00:00 +0000
targetDate = 2015-03-30 00:00:00 +0000
targetDate = 2015-03-29 01:00:00 +0000
targetDate = 2015-03-28 01:00:00 +0000
targetDate = 2015-03-27 01:00:00 +0000
targetDate = 2015-03-31 00:00:00 +0000
targetDate = 2015-04-01 00:00:00 +0000
targetDate = 2015-04-02 00:00:00 +0000
Now in my logs, I noticed that anything before 28 is fine, but anything after that date loses 1 hour without explanation.
I have checked my array, I have checked my indexes, I have checked that the objects are valid time objects; however I am stumped -- why does it lose 1 hour?

The issue is most likely that you use NSLog(#"%#", date); to log your date. This will log times in UTC, i.e. +0000. If you are not living in a country that uses UTC (e.g. UK, Iceland, Ghana, Portugal, ...) these dates appear different from what you would expect. (See also: https://stackoverflow.com/a/4976950/457406)
If you create a date with NSDateComponents it uses your local timezone. Depending on your local timezone the same local time (e.g. 01:00), has different representations in UTC throughout the year. Also know as Daylight Savings Time.
In your experiments you saw that change at the end of March. Which is the time of the year when Daylight Savings Time starts in many countries. For example in Germany it starts on Sunday, March 29 at 2:00 in the morning. Which would explain the change in your logs on March 30 1:00.
To make a long story short, use something else to log your dates. For example:
NSLog(#"%#", [NSDateFormatter localizedStringFromDate: combinedDate dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]);

Related

Objective-C: Get the number of calendar days between two dates

I just don't get the correct result...so depending on the timezone I want the calendar day difference between two dates. So if one starts at 23:00 on day 1 and ends on 14:00 on day 2 it should return 1. Now my method returns 0, why? Because it's less than 24 hours? Example:
MY Nslog:
CheckForPictures departure date: Tue Jan 28 23:10:00 2020 destinationDate: Wed Jan 29 09:30:00 2020 in timeZone:Europe/Zurich and get a day difference: 0
(Computer has also timezone Zurich, so it is local time)
My Method:
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:timeZone];
NSDateComponents *components = [calendar components:NSCalendarUnitDay
fromDate:self.departureTime
toDate:self.destinationTime
options:0];
NSLog(#"CheckForPictures departure date: %# destinationDate: %# in timeZone:%# and get a day difference: %ld", self.departureTime, self.destinationTime, timeZone.name, components.day);
return components.day;
And this code returns 0 and logs above log...
I believe you're asking NSCalendar the wrong question. You want to know if the arrival date is different than the departure date, but you're asking for the number of days between arrival and departure. A date change could happen even if the difference in time is only a few minutes. "A few minutes" rounds to "0 days" if you ask how many days have passed.
You actually want to know if the date has changed. I think I would be doing something like getting the day of the month for each date and comparing that. Since no months have 1 day, I would think that would work.
With Craigs input I came up with this solution:
- (NSInteger)calendarDaysBetweenDepartureAndArrivalTimeForTimeZone:(NSTimeZone *)timeZone
{
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:timeZone];
NSDateComponents *departureComponents = [calendar components:(NSCalendarUnitDay) fromDate:self.departureTime];
NSDateComponents *destinationComponents = [calendar components:(NSCalendarUnitDay) fromDate:self.destinationTime];
NSInteger difference = destinationComponents.day - departureComponents.day;
if(difference < 0){
//Month overlapping
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self.departureTime];
difference = range.length - departureComponents.day + departureComponents.day;
}
NSLog(#"CheckForPictures departure date: %# destinationDate: %# in timeZone:%# and get a day difference: %ld", self.departureTime, self.destinationTime, timeZone.name, difference);
return difference;
}

Storing an absolute NSDate, not relative to a timezone

I am creating an iOS app to track attendance. Each attendance entry is stored in an object which has a status attribute (e.g. present, absent) and an NSDate attribute called date which denotes the day which that attendance record was taken. When I select a particular date (using a UIDatePickerView or alike) I want all the attendance records (objects) for that date to appear in a table view.
While this sounds simple in principle, I am running into an issue relating to timezones. I am aware that NSDates are stored independent of timezones (i.e. they are stored relative to UTC/GMT +0000). This means that if I am in Sydney and take attendance on, for example, Sunday 4 November 2012 because the date is stored as timezone independent, if I take my iPhone/iPad to a different time zone (such as San Francisco) all the attendance records would shift one day back, in this case to Saturday 3 November 2012, because that was the moment in time when the attendance was taken in San Francisco local time (which was actually the next day, in Sydney local time).
I don't want this to happen - I want the date to be absolute. In other words, if the attendance is taken on Sunday 4 November 2012 then it needs to stay on that date, no matter where in the world (and whichever timezone) I may go. As you can see, this is quite in contrast to, say, a calendar application where it is desirable for the timing of appointments to change depending on the timezone.
Any suggestions on a better way to approach this problem would be appreciated. Please keep in mind that I am selecting the date to display using a UIDatePickerView which returns the current NSDate in the timezone independent format, so I also need a way to do an easy comparison (preferably in an NSPredicate since the attendance objects are stored in Core Data) to get all the attendance objects for that particular day.
Have you tried converting the time to it's NSDateComponents? you can then recreate an NSDate from it regardless of the time zone.
Edited to add
// This is just so I can create a date from a string.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss z"];
// Create a date as recorded in a timezone that isn't mine.
NSDate *localDate = [formatter dateFromString:#"2012-10-30 10:30:00 +0200"];
NSLog(#"Initial Date: %#", localDate);
// this logs 2012-10-30 08:30:00 +0000
// Which is what you would expect, as the original time was 2 hours ahead
NSDateComponents *components = [[NSDateComponents alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:localDate];
NSLog(#"Components: %#", components);
// Create a date from these time components in some other time zone
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"EST"]];
NSDate *newDate = [gregorian dateFromComponents:components];
NSLog(#"New Date: %#", newDate);
// This logs 2012-10-30 12:30:00 +0000
// Which is the local EST of 8:30 am expressed in UTC
Which demonstrates how I can turn make 8:30 am in +2 time zone look the same as for a -4 timezone.
I believe the better way for you is to use timestamp since it independ of any time zone. You can use vary methods to convert timestamps to date and back. And implement any logic you wish.
Also you can easily compare them.

Converting NSDate

I have an NSDate (lets call it x), 12 September, 2012 10:18PM (GMT). I want to convert x to a minute before my current time zone's (EST) midnight. So, x represented in EST with NSDateFormatter after conversion would be 12 September, 2012 11:59PM (EST). What's the best way to do this?
Thanks
Take a look at NSDateComponents: https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/Reference/Reference.html
I believe you'll need to convert the NSDate to NSDateComponents, set the time to 11:59PM, then convert back to NSDate.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:date];
[components setHour:23];
[components setMinute:59];
NSDate *convertedDate = [calendar dateFromComponents:components];
NSLog(#"date=%#, convertedDate=%#", date, convertedDate);
Such manipulations can be easily done with the numeric representation of a date. In this representation, you deal with the number of seconds since a reference date.
The reference date for the purpose of the timeIntervalSinceReferenceDate is January 1st, 2001, at 00:00:00 GMT.
NSDate* date = [NSDate date];
NSInteger secondsSinceReferenceDate = [date timeIntervalSinceReferenceDate];
secondsSinceReferenceDate += 86400 - (secondsSinceReferenceDate % 86400);
secondsSinceReferenceDate -= 60;
secondsSinceReferenceDate -= [NSTimeZone.localTimeZone secondsFromGMTForDate:date];
NSDate* justBeforeToday =
[NSDate dateWithTimeIntervalSinceReferenceDate:secondsSinceReferenceDate];
NSLog(#"Date used was %#", date);
NSLog(#"Just before tomorrow is %#", justBeforeToday);
Since there are 86400 seconds in a day (24 hours times 60 minutes 60 times 60 seconds = 86400 seconds), you know that 86400 - (secondsSinceReferenceDate % 86400) is the number of seconds there are still to midnight. So if you take today's date (or any other valid date), add this number of seconds, and then subtract another 60 seconds, you'll have today's evening at 11:59 PM in the GMT timezone.
With [NSTimeZone.localTimeZone secondsFromGMTForDate:], you know how many seconds your timezone is offset from the GMT timezone. By subtracting this offset to your integer representation, you effectively get when it will be 11:59 PM in your local timezone.
Here's a sample output:
Date used was 2012-09-12 22:37:49 +0000
Just before tomorrow is 2012-09-13 03:59:00 +0000
I'm in the EDT timezone too, and this looks like the correct answer (remember Standard Time is -5 from GMT, but right now we're in daylight savings, so it's -4 from GMT, which is invariant).

NSDateComponents issue - incorrect day

I have a NSDateComponents problem. I have two NSDates that I am trying to compare by checking if their year, month and day match. This I am doing by converting the NSDate values to these integer components as follows:
//NSDate *cgiDate is previously set to 2011-08-04 00:00:00 +0000
//NSDate *orderDate is previously set to 2011-08-04 14:49:02 +0000
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cgiDateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:cgiDate];
NSCalendar *orderCalendar = [NSCalendar currentCalendar];
NSDateComponents *orderDateComponents = [orderCalendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:orderDate];
if (([cgiDateComponents day] == [orderDateComponents day]) &&
([cgiDateComponents month] == [orderDateComponents month]) &&
([cgiDateComponents year] == [orderDateComponents year])) {
GHTestLog(#"MATCHED");
} else {
GHTestLog(#"Not matched");
GHTestLog(#"Day: %d vs. %d", [cgiDateComponents day], [orderDateComponents day]);
}
My result is Not Matched, Day: 3 vs. 4. Why would this be?
I have read with great interest the following questions:
NSDateComponents - day method returning wrong day and
https://stackoverflow.com/questions/3920445/nsdatecomponents-incorrectly-reporting-day however neither answer my question of why this is not working.
Any advice?
I think the issue is the following:
Your dates are set in GMT time zone (+0000)
If you are in the US, for example at GTM-6, then by the -currentCalendar the first date will be 6pm on Aug 3rd while second date will be 8:49am on Aug 4th.
You should force your calendar to have the UTC (GMT) timezone, or put the dates in your time zone, depending what is correct for your application.
It looks like you have a time zone issue. Though your dates are set at time zone +0000, your [NSCalendar calendar] call likely returns you a calendar for your local time zone. Given the adjustment to local time, the orderDate is likely in the next day. Manually set your calendars to time zone +0000.
NSDate has an isEqualToDate: function. so what you are trying to do can be done by:
[datea isEqualToDate:dateb];
there is also a Compare: function available, that returns the ordering of the dates.
edit: I'm now aware this doesn't answer your question about why the days return different values when they are set the same. Sorry! this still may help make your code a bit cleaner.

NSDateComponents returns week 53 on 1/1/1970

So when extracting the dates components from NSDate object using NSCalendar and NSDateComponents I encountered a weird behavior.
If the date is 0 sec from 1970 the week component will return 53.
Is there an explination for this or a way to fix other than the obvious way of modulus 52?
here is the code you can run on your machine to test:
-
(void)testDate {
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSDateComponents *comp = [cal components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSWeekCalendarUnit) fromDate:date];
DLog(#"%d/%d/%d week: %d", [comp day],[comp month], [comp year], [comp week]);
}
and here is the output:
31/12/1969 week: 53
(gdb) po date
1970-01-01 00:00:00 +0000
(gdb)
Well I got this,
2011-06-22 22:38:50.516 SmallTasks[23164:903] 1/1/1970 week: 1
So I am bit surprised by the result you got but I am not that surprised that a week: 53 turned up as 52 * 7 = 364 and we've 365 days in a year. For that to happen I would expect the week to start on Sunday on 1969 but it didn't.
You my friend have discovered the leap week. I have run across this doing some SQL reporting a couple years back.