Pages

Saturday, March 6, 2010

Cocoa. Date and Time

This small program detects the current date:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSDate* today = [[NSDate alloc] init];
NSLog(@"today is: %@", today);

[today release];
[pool drain];
return 0;
}
In the console you'll see:
run
[Switching to process 12401 local thread 0x3f03]
Running…
2010-03-06 17:05:45.217 DayOfToday[12401:a0f] today is: 2010-03-06 17:05:45 +0200

NSDate object (today) was created and initialized with the current date and time:
NSDate* today = [[NSDate alloc] init];
And the next line prints out this information:
NSLog(@"today is: %@", today);
Same result will be here:
NSDate *today = [[NSDate alloc] init];
NSString *text = [today description];
NSLog(text);
Method description gives a string representing the date.
The following program shows to calculate other related dates:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSDate *today = [[NSDate alloc] init];
NSLog(@"Today is: %@", today);

//Number os seconds in a day:
NSTimeInterval secondsPerDay = 24 * 60 * 60;

//Calculates the date of tomorrow:
NSDate *tomorrow = [today addTimeInterval: secondsPerDay];
NSLog(@"Tomorrow is: %@", tomorrow);

//Calculates the date of yesterday:
NSDate *yesterday = [today addTimeInterval: -secondsPerDay];
NSLog(@"Yesterday is: %@", yesterday);

NSLog(@"Verify today: %@", today);

[today release];
[pool drain];
return 0;
}
There is another very simple way to create the date objects:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSString *dateString = @"2010-03-12 14:07:00 +0200";
NSDate *today = [[NSDate alloc] initWithString: dateString];
NSLog(@"Today is: %@", today);

[today release];
[pool drain];
return 0;
}
Of course, this line:
NSString *dateString = @"2010-03-12 14:07:00 +0200";
can be shorter. For example, like that:
NSDate *today = [[NSDate alloc] initWithString: @"2010-03-12 14:07:00 +0200"];
Or simply:
NSDate *today = [NSDate dateWithString: @"2010-03-12 14:07:00 +0200"];

A special class NSDateFormatter allows to format the output:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSDate *today = [[NSDate alloc] init];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle: NSDateFormatterMediumStyle];

NSLog(@"%@", [dateFormatter stringFromDate: today]);

[dateFormatter release];
[today release];
[pool drain];
return 0;
}
The output will be:

run
[Switching to process 13097 local thread 0x4003]
Running…
2010-03-06 18:19:40.752 DayOfToday[13097:a0f] Mar 6, 2010

Method compare allows to compare two NSDate objects:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSDate *d1 = [NSDate dateWithString: @"2010-03-06 01:01:01 +0200"];
NSDate *d2 = [NSDate dateWithString: @"2010-03-12 02:02:02 +0200"];

NSComparisonResult result = [d1 compare: d2];

switch (result) {
case NSOrderedAscending:
NSLog(@"Ascending (d2 is a later date than d1)");
break;

case NSOrderedDescending:
NSLog(@"Descending (d1 is a later date than d2)");
break;

case NSOrderedSame:
NSLog(@"Same (d1 and d2 is equal)");
break;
}
[pool drain];
return 0;
}
And here is a program sorting an array of dates:
#import <Foundation/Foundation.h>

NSComparisonResult dateSort(NSDate *d1, NSDate *d2, void *context) {
return [d1 compare:d2];
}

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *today = [[NSDate alloc] init];
NSDate *tomorrow = [today addTimeInterval:secondsPerDay];
NSDate *afterTomorrow = [today addTimeInterval: 2 * secondsPerDay];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

NSArray *array = [NSArray arrayWithObjects: tomorrow, today, afterTomorrow, nil];
[array sortedArrayUsingFunction: dateSort context:nil];
NSLog(@"Sorted:");
for(NSDate *date in array) {
NSLog(@"%@", [dateFormatter stringFromDate:date]);
}

[dateFormatter release];
[array release];
[pool drain];
return 0;
}
References:
1. Mac OS Reference Library. NSDate Class Reference.
2. Mac OS Reference Library. NSDateFormatter Class Reference.

No comments:

Post a Comment