NSDateComponents issue - incorrect day - objective-c

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.

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;
}

Determining Day Components of an NSDate Object (Time Zone Issues)

How would I be able to determine if a certain NSDate object falls within a certain day? Since all dates calculate to the GMT time zone, a date maybe actually be on the 8th instead of the 9th, for example. What's the best way to account for the time zone difference when calculating things like days, months, day of week, etc.
An NSDate is an absolute point in time. When you want to know which day it falls on, you have to consider your specific time zone. Since you get the NSDateComponents with an NSCalendar, you can use setTimeZone: on the calendar to get the components (month, day, hour etc.) for the time zone you're interested in.
If you deal with "calendar dates" (i.e. dates that should always represent a specific day/month and not an absolute point in time), you could always use a fixed time zone (e.g. GMT) to present the dates. NSDateFormatter (which you use for displaying dates to the user) also has a timeZone property.
You need to set the desired timezone in NSCalendar before using it to break the date into parts:
NSCalendar* gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[gregorian setTimeZone:[NSTimeZone localTimeZone]]; // or timeZoneWithName:#"GMT" etc.
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents* compsDate = [gregorian components:unitFlags fromDate:date];
// [compsDate year] [compsDate month] [compsDate day]

NSDate and the next day

What I used to do was using the timestamp and then adding 86400 seconds ( 24 * 60 * 60 ). As I read a recent question on stackoverflow noting that that is not correct for every day, I want to change this.
So the next thing I came up with was by using NSDateComponents. I simply get the current day and add 1. Now I was wondering how "smart" that is. Like if the day is equal to 31, does it set the day to 1 and the month to whatever it is +1 ( or when it is 12 to 1 again ) ? I can do this manually but that would only work properly for the gregorian calendar so I don't really know whether that would be a good solution either..
It is smart, unless you want it to be dumb.
From the documentation for -[NSCalendar dateByAddingComponents:toDate:options]:
If you specify no options (you pass 0), overflow in a unit carries into the higher units (as in typical addition).
If you did not want units overflowing into higher units, then you would pass "NSWrapCalendarComponents" as the value of the options: parameter.
Be very careful of DST. It is different for various time zones. AND It happens at Midnight
I believe that if you call [NSCalendar date by addingComponents:toDate:options] and your date is between 11-12 pm, you risk missing or re-running a day because of DST.
To get around this, is suggest changing your date to noon on the day and then adding the day.
// increment by 1 calendar day
// and convert back to NSDate objects
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setDay:1];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[gregorian setTimeZone:self.myTimeZone]; // time zone of location;
// Use local noontime for the date to avoid problems around
// midnight, particularly near daylightsavings time discontinuities.
NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents* dateComponents = [gregorian components:unitFlags fromDate:date];
dateComponents.hour = 12;
NSDate* noonDate = [gregorian dateFromComponents:dateComponents];
NSDate *newDate = [gregorian dateByAddingComponents:comps toDate:noonDate options:0];
I'm not sure about the options:0 setting, but I did test this pretty thoroughly. I use it to get an array of with 10 days of dates. It gives correct wrapping around the various day/month/year discontinuities. Check out SunPose Rise Set on the app store if you want to test it. (yes, it's free)

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.

How to determine if an NSTimeInterval occurred during an arbitrary NSDate?

I have an NSTimeInterval, and I'd like to know whether that timestamp falls between the start and end of a date. Basically I need to be able to do something like:
NSDate *today = [NSDate date];
NSTimeInterval myInterval = someInterval;
[date returnYesIfFallsThisDate:myInterval];
Is there a straight forward method to figure out if my NSTimeInterval falls on the same day as an NSDate object?
Thanks all.
An NSTimeInterval is just a number of seconds. To compare it to an NSDate, you need to interpret the time interval relative to some reference point, then create an NSDate with the result, then compare the two NSDates.
There are two standard reference dates: 2001-01-01 and 1970-01-01 (the latter being “UNIX time”). If neither of those is the correct reference date, use NSCalendar to create an NSDate for the correct reference date, then create your NSDate of interest relative to that.
Now for the comparison.
If all you care about is the date on the calendar, then you'll create the NSDate, then use an NSCalendar to get NSDateComponents for the date. Specifically, you'll get the year, month, and day-of-the-month, then compare those.
There's no need to worry about “start” and “end” times; in fact, if you only care about the date on the calendar, you can ignore the time-of-day entirely.
Keep in mind that NSDate doesn't represent a "day" in any form, just a point in time. You'll have to do a little work with NSDateComponents, converted using an NSCalendar (typically the user's default calendar) to figure out the start and end NSDate values, and compare use those to compare your time interval to.
I would start by taking the NSDate that falls within your day, and converting it to an NSDateComponents, but without hours, minutes or seconds. Here's a quick (untested) example to help you get started:
NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
NSDateComponents *day = [[[NSDateComponents alloc] init] autorelease];
[day setDay:1];
NSDate *start = [[NSCalendar currentCalendar] dateFromComponents:comps];
NSDate *end = [[NSCalendar currentCalendar] dateByAddingComponents:day toDate:start options:0];
Then you can compare the time interval as suggested by Mr. Jalkut (or another method, NSDate can work with NSTimeIntervals in a few different ways). I would definitely spend some time looking over NSCalendar and NSDateComponents in the docs though, you might find a better method than what I'm suggesting for what you need to do.
Is it just me or does the OP seem to think that a NSTimeInterval is a representation of an interval in time from A to B? I.e. an absolute distance (in seconds) from Date A to Date B.
The original question needs to be repose to state that you need to find it based on the reference date. Something like this:
NSDate *refDate = someReferenceDate;
NSTimeInterval interval = someInterval;
NSDate *today = [NSDate date];
BOOL isInRange = false;
isInRange = [today isInterval:interval inRangeFromReference:refDate];
Then that method would use some (or parts of all) of the startegies that Damiel, Peter and Marc mention above.
Conceptually you want two dates that constitute the "start" and "end" of the target date in question. Then you can test the time interval directly against the respective time intervals of those dates.
For instance, if I live in San Francisco, then I want the "start" to be 12AM pacific time, and the end to be 11:59:59.99999 PM pacific time.
Once you've figured out what time zones/etc you want to consider as the start and end points, you can just use the time intervals to do the test:
if ((myInterval >= [earlyMorningDate timeIntervalSinceReferenceDate]) &&
(myInterval <= [lateEveningDate timeIntervalSinceReferenceDate]))
{
NSLog(#"Whoo hoo - it's the right date!");
}