Instances and Calendar Error? Objective-C - objective-c

This application is supposed to Log how many seconds it's been since a birthdate(1996) but when I try and log NSLog(#"It has been %f seconds since the start of 1996.", seconds); it logs:
2014-01-29 11:36:15.756 TimeAfterTime[750:303] It has been (null) seconds since the start of 1996.
my question is why does it print (null)? This is a Objective-C Command Line Tool application in Xcode. Any suggestions would be greatly appreciated! Thanks
Code I'm using:
// main.m
// TimeAfterTime
//
// Created by JP on 1/28/14.
// Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1996];
[comps setMonth:11];
[comps setDay:7];
[comps setHour:13];
[comps setMinute:23];
[comps setSecond:0];
NSCalendar *g = [[[NSCalendar alloc] init] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *dateOfBirth = [g dateFromComponents:comps];
NSDate *now = [[NSDate alloc] init];
NSLog(#"This NSDate object lives at %p", now);
NSLog(#"The date is %#", now);
double seconds = [now timeIntervalSinceDate:dateOfBirth];
NSLog(#"It has been %f seconds since the start of 1996.", seconds);
}
return 0;
}
The result it prints in the console:
2014-01-29 11:36:15.752 TimeAfterTime[750:303] This NSDate object lives at 0x100103680
2014-01-29 11:36:15.756 TimeAfterTime[750:303] The date is 2014-01-29 16:36:15 +0000
2014-01-29 11:36:15.756 TimeAfterTime[750:303] It has been (null) seconds since the start of 1996.
Program ended with exit code: 0

NSCalendar *g = [[[NSCalendar alloc] init] initWithCalendarIdentifier:NSGregorianCalendar];
should be
NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
Due to the wrong initialization, g = nil and thus dateOfBirth = nil.

Related

Objective-c Why is the following date incorrect

I was doing an assignment for my intro to mobile development class.
We had to get the seconds since a date and give how old the person is in seconds, I wanted to extend this and show the actual age of the person. Can achieve this by(check out my code)
When the date is set to the NEXT day so my birthday is tomorrow it is showing an incorrect AGE.
Heres my code;
#import <Foundation/Foundation.h>
int main (int argc, char* argv[]) {
#autoreleasepool {
NSDateComponents* comps = [[NSDateComponents alloc] init];
[comps setYear: 2000];
[comps setMonth: 11];
[comps setDay: 25];
[comps setHour: 17];
[comps setMinute: 50];
[comps setSecond: 01];
NSCalendar* g = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate* dateOfBirth = [g dateFromComponents:comps];
NSDate* now = [NSDate date];
double secondsSince = [now timeIntervalSinceDate:dateOfBirth];
double actualAge = secondsSince / 60 / 60 / 24 / 365;
NSLog(#"\nI am %.9f Seconds Old \nI am Exactly %.9f Years Old %#", secondsSince, actualAge, now);
}
}
Here's my output:
2014-11-24 13:35:23.547 secondsAlive[4978:44040]
I am 441661522.544027984 Seconds Old
I am Exactly 14.004995007 Years Old 2014-11-24 18:35:23 +0000
There is 365.242 days in a year not 365.

Create an Array of NSDates like in Ruby

I want to create an array of NSDates starting from today to next month. This can easily be done in Ruby using Time.now..(Time.now + 30.days)
How can I create an array of dates just like in Ruby in Objective C?
Any ObjC solution is unfortunately going to be far more verbose than that Ruby code.
The correct way to make the calculation is with NSDateComponents:
NSMutableArray * dateArray = [NSMutableArray array];
NSCalendar * cal = [NSCalendar currentCalendar];
NSDateComponents * plusDays = [NSDateComponents new];
NSDate * now = [NSDate date];
for( NSUInteger day = 0; day < NUMDAYS; day++ ){
[plusDays setDay:day];
[dateArray addObject:[cal dateByAddingComponents:plusDays toDate:now options:0]];
}
To make the procedure more convenient (if you need to do it more than a few times), you could put this loop into a category method on NSCalendar, with NUMDAYS replaced with the argument and substituting self for cal.
There's nothing built in to do this quite as concisely as the Ruby you've posted. Breaking the problem down, you need a way to get the day after a particular date. Here's a function that will do that:
NSDate *CalendarDayAfterDate(NSDate *date)
{
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSCalendar *calendar = [NSCalendar currentCalendar];
return [calendar dateByAddingComponents:components toDate:date options:0];
}
Next, you need to get an array of days one after the other:
NSDate *today = [NSDate date];
NSMutableArray *dates = [NSMutableArray arrayWithObject:today];
for (NSUInteger i=0; i<30; i++) {
NSDate *tomorrow = CalendarDayAfterDate(today);
[dates addObject:tomorrow];
today = tomorrow;
}
After much downvoting and commenting, here's my REVISED answer...
-(NSDate *)nextDayFromDate:(NSDate *)originalDate {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponent = [NSDateComponents new];
dateComponent.day = 1;
NSDate *tomorrow = [calendar dateByAddingComponents:dateComponent toDate:originalDate options:0];
return tomorrow;
}
NSMutableArray *dateArray = [NSMutableArray array];
NSDate *now = [NSDate date];
[dateArray addObject:now];
for (int i=0;i<31;i++) {
NSDate *firstDate = [dateArray objectAtIndex:i];
NSDate *newDate = [self nextDayFromDate:firstDate];
[dateArray addObject:newDate];
}
What this does is use the NSCalendar API to add a "day interval" to any given NSDate. Add "Now" to the array, then do a loop 30 times, each time using the previous NSDate object as input to the logic.

Get only weekends between two dates

I'm trying get only the Saturdays and Sundays between two dates, but I don't know why get me free days on a week.
Here is my code:
- (BOOL)checkForWeekend:(NSDate *)aDate {
BOOL isWeekendDate = NO;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSRange weekdayRange = [calendar maximumRangeOfUnit:NSWeekdayCalendarUnit];
NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:aDate];
NSUInteger weekdayOfDate = [components weekday];
if (weekdayOfDate == weekdayRange.location || weekdayOfDate == weekdayRange.length) {
// The date falls somewhere on the first or last days of the week.
isWeekendDate = YES;
}
return isWeekendDate;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSString *strDateIni = [NSString stringWithString:#"28-01-2012"];
NSString *strDateEnd = [NSString stringWithString:#"31-01-2012"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MM-yyyy"];
NSDate *startDate = [df dateFromString:strDateIni];
NSDate *endDate = [df dateFromString:strDateEnd];
unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];
// int months = [comps month];
int days = [comps day];
for (int i=0; i<days; i++)
{
NSTimeInterval interval = i;
NSDate * futureDate = [startDate dateByAddingTimeInterval:interval];
BOOL isWeekend = [self checkForWeekend:futureDate]; // Any date can be passed here.
if (isWeekend) {
NSLog(#"Weekend date! Yay!");
}
else
{
NSLog(#"Not is Weekend");
}
}
}
The problem:
The issue was caused by NSTimeInterval interval = i; The logic of the for loop was to iterate by days. Setting the time interval to i was iterating by seconds.
From documentation on NSTimeInterval
NSTimeInterval is always specified in seconds;
The answer:
Changing the NSTimeInterval line to
NSTimeInterval interval = i*24*60*60;
Here is a link to another answer I posted on SO (shameless, I know). It has some code that may help you with dates in the future. The methods are implemented as categories of NSDate, meaning they become methods of NSDate.
There are several functions there that help with weekends. But these two might be most helpful:
- (NSDate*) theFollowingWeekend;
- (NSDate *) thePreviousWeekend;
They return the date of the weekend following and prior to the receiver (self).
Generally, you should not use the notion that a day is 86400 seconds, and should use NSDateComponents and NSCalendar. This works even when daylight savings time transitions occur between dates. Like this:
- (NSDate *) dateByAddingDays:(NSInteger) numberOfDays {
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = numberOfDays;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
return [theCalendar dateByAddingComponents:dayComponent toDate:self options:0];
}
One very important thing to remember is that one day is not (necessarily) equal to 24*60*60 seconds. And you should not do date arithmetic yourself
What you really need to do might seem a little tedious but this is the correct thing to do: use NSCalendar and – dateByAddingComponents:toDate:options:
See Calendrical Calculations guide.

NSNumber from NSDate

I'm attempting to get around a date validation that refuses to take anything earlier than tomorrow.
So far I have this:
NSDate *dateY = [NSDate dateWithTimeIntervalSinceNow:-86400];
// Negative one day, in seconds (-60*60*24)
NSLog(#"%#", [NSDate date]);
// "yyyy-MM-dd HH:mm:ss Z", accurate assuming Z = +0000
NSLog(#"%#", dateY);
// "yyyy-MM-dd HH:mm:ss Z", same accuracy (minus one day)
That's great, but dateY is not an NSNumber. I need an NSNumber for the comparison, but I can't find anything that works. (I don't even know how an NSNumber can be 2011-04-14 13:22:29 +0000, anyway...)
I can use NSDateFormatter to convert an NSDate into an NSString, so if it would be possible to take that string and convert it to the required NSNumber (as opposed to directly converting the NSDate to an NSNumber, which I can't seem to find help with either), that would be fine.
- (BOOL)validateDueDate:(id *)ioValue error:(NSError **)outError {
NSDate *dateY = [NSDate dateWithTimeIntervalSinceNow:-86400];
NSNumber *tis1970 = [NSNumber numberWithDouble:[dateY timeIntervalSince1970]];
NSLog(#"NSNumber From Date : %#", tis1970);
NSLog(#"Date From NSNumber : %#", [NSDate dateWithTimeIntervalSince1970:[tis1970 doubleValue]]);
// Due dates in the past are not valid
// Enforced that a due date has to be >= today's date
if ([*ioValue compare:[NSDate date]] == NSOrderedAscending) {
if (outError != NULL) {
NSString *errorStr = [[[NSString alloc] initWithString:#"Due date must be today or later."] autorelease];
NSDictionary *userInfoDictionary = [NSDictionary dictionaryWithObject:errorStr forKey:#"ErrorString"];
NSError *error = [[[NSError alloc]
initWithDomain:TASKS_ERROR_DOMAIN
code:DUEDATE_VALIDATION_ERROR_CODE
userInfo:userInfoDictionary] autorelease];
*outError = error;
}
return NO;
} else {
return YES;
}
}
Right now, the user is not allowed to choose a date before tomorrow. errorStr lies. Before today makes more sense than before tomorrow as a rule for refusing to save the date, so I've been fighting with this thing to let me use yesterday in place of today, rather than looking any deeper.
Edit: Using NSOrderedSame allows any date to be selected without an error. That won't do.
You can convert an NSDate to an NSNumber like this:
NSDate *aDate = [NSDate date];
NSNumber *secondsSinceRefDate = [NSNumber numberWithDouble:[aDate timeIntervalSinceReferenceDate]];
and convert back like:
aDate = [NSDate dateWithTimeIntervalSinceReferenceDate:[NSNumber doubleValue]];
All that is needed to get a NSNumber is
NSDate *dateY = [NSDate dateWithTimeIntervalSinceNow:-86400];
NSNumber *tis1970 = [NSNumber numberWithDouble:[dateY timeIntervalSince1970]];
NSLog(#"NSNumber From Date : %#", tis1970);
NSLog(#"Date From NSNumber : %#", [NSDate dateWithTimeIntervalSince1970:[tis1970 doubleValue]]);
You should never use 86400 to calculate date differences, because not all days have 86,400 seconds in them. Use NSDateComponents instead:
- (BOOL)validateDueDate:(NSDate *)dueDate error:(NSError *)error {
NSDate *today = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:today];
//adjust the components to tomorrow at the first instant of the day
[components setDay:[components day] + 1];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
NSDate *tomorrow = [calendar dateFromComponents:components];
NSDate *earlierDate = [dueDate earlierDate:tomorrow];
if ([earlierDate isEqualToDate:dueDate]) {
//the dueDate is before tomorrow
if (error != nil) {
NSString *errorStr = [[[NSString alloc] initWithString:#"Due date must be today or later."] autorelease];
NSDictionary *userInfoDictionary = [NSDictionary dictionaryWithObject:errorStr forKey:NSLocalizedDescriptionKey];
*error = [[[NSError alloc] initWithDomain:TASKS_ERROR_DOMAIN code:DUEDATE_VALIDATION_ERROR_CODE userInfo:userInfoDictionary] autorelease];
}
return NO;
}
return YES;
}
WARNING: code typed in a browser. Caveat Implementor

How can I compare two dates, return a number of days

how can I compare two dates return number of days.
Ex: Missing X days of the Cup.
look my code.
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"d MMMM,yyyy"];
NSDate *date1 = [df dateFromString:#"11-05-2010"];
NSDate *date2 = [df dateFromString:#"11-06-2010"];
NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
//int days = (int)interval / 30;
//int months = (interval - (months/30)) / 30;
NSString *timeDiff = [NSString stringWithFormat:#"%dMissing%d days of the Cup",date1,date2, fabs(interval)];
label.text = timeDiff; // output (Missing X days of the Cup)
From Apple's example, basically use an NSCalendar:
NSDate * date1 = <however you initialize this>;
NSDate * date2 = <...>;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
fromDate:date1
toDate:date2 options:0];
NSInteger months = [components month];
NSInteger days = [components day];
You can use next category:
#interface NSDate (Additions)
-(NSInteger)numberOfDaysUntilDay:(NSDate *)aDate;
-(NSInteger)numberOfHoursUntilDay:(NSDate *)aDate;
#end
#implementation NSDate (Additions)
const NSInteger secondPerMunite = 60;
const NSInteger munitePerHour = 60;
const NSInteger hourPerDay = 24;
-(NSInteger)numberOfDaysUntilDay:(NSDate *)aDate
{
NSInteger selfTimeInterval = [aDate timeIntervalSinceDate:self];
return abs(selfTimeInterval / (secondPerMunite * munitePerHour * hourPerDay));
}
-(NSInteger)numberOfHoursUntilDay:(NSDate *)aDate
{
NSInteger selfTimeInterval = [aDate timeIntervalSinceDate:self];
return abs(selfTimeInterval / (secondPerMunite * munitePerHour));
}
#end
Need to call this method and set 2 dates only which we want to calculate differently.
-(void) calculateSleepHours:(NSDate *)sleepDate :(NSDate *)wakeStr
{
if (sleepDate !=nil && wakeStr!=nil) {`enter code here`
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd HH:mm:ssZ"];
NSDate *date1 = [ApplicationManager getInstance].sleepTime;;
NSDate *date2 = [ApplicationManager getInstance].wakeupTime;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
fromDate:date1
toDate:date2 options:0];
NSInteger months = [components month];
NSInteger days = [components day];
NSInteger hours = [components hour];
NSInteger minute=[components minute];
NSInteger second=[components second];
DLog(#"Month %ld day %ld hour is %ld min %ld sec %ld ",(long)months,(long)days,(long)hours,(long)minute,(long)second);
sleepHours.text=[NSString stringWithFormat:#"Hour %ld Min %ld Sec %ld",(long)hours,(long)minute,(long)second];
}
}
Try this category:
#interface NSDate (DateUtils)
-(NSInteger)numberOfDaysUntilDay:(NSDate *)aDate;
#end
#implementation NSDate (DateUtils)
-(NSInteger)numberOfDaysUntilDay:(NSDate *)aDate
{
NSCalendar *calendar = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitDay
fromDate:self
toDate:aDate options:kNilOptions];
return [components day];
}
#end
You can use this category by adding an import:
#import "NSDate+DateUtils.h"
And call it from your code:
NSInteger days = [myDate numberOfDaysUntilDay:someOtherDate];