Open Source Objective-C Calendar Class
I always try to get you guys something for Christmas, but you're so hard to buy for. So instead, you get code. Mac OS X has solid time/date functionality, but it's spread across a number of different places. THCalendarInfo consolidates them.Using NSDate, NSCalendarDate, CFCalendar, and so on, this class does its best to give you solutions for real-world use cases. It's particularly useful for walking hours, days, weeks, months, years, and so on. Examples speak louder than descriptions:
THCalendarInfo * calInfo = [THCalendarInfo calendarInfo];
NSLog(@"Month: %@", [calInfo monthName]);
NSLog(@"Date: %i", [calInfo dayOfMonth]);
NSLog(@"Day: %@", [calInfo dayName]);
NSLog(@"Days in Month: %i\n\n", [calInfo daysInMonth]);
[calInfo moveToPreviousDay];
NSLog(@"Date: %i", [calInfo dayOfMonth]);
NSLog(@"Day: %@\n\n", [calInfo dayName]);
[calInfo moveToNextMonth];
NSLog(@"Month: %@", [calInfo monthName]);
NSLog(@"Date: %i", [calInfo dayOfMonth]);
NSLog(@"Day: %@", [calInfo dayName]);
NSLog(@"Days in Month: %i\n\n", [calInfo daysInMonth]);
[calInfo moveToNextYear];
NSLog(@"Month: %@", [calInfo monthName]);
NSLog(@"Date: %i", [calInfo dayOfMonth]);
NSLog(@"Year: %i", [calInfo year]);
NSLog(@"Day: %@", [calInfo dayName]);
NSLog(@"Days in Month: %i\n\n", [calInfo daysInMonth]);
[calInfo moveToFirstDayOfMonth];
NSLog(@"Month: %@", [calInfo monthName]);
NSLog(@"Date: %i", [calInfo dayOfMonth]);
NSLog(@"Day: %@\n\n", [calInfo dayName]);
[calInfo resetDateAndTimeToCurrent];
NSLog(@"NSDate rep: %@\n\n", [calInfo date]);
[calInfo adjustDays:81];
NSLog(@"Month: %@", [calInfo monthName]);
NSLog(@"Date: %i\n\n", [calInfo dayOfMonth]);
[calInfo adjustDays:-81];
NSLog(@"Month: %@", [calInfo monthName]);
NSLog(@"Date: %i\n\n", [calInfo dayOfMonth]);
// class methods
NSLog(@"+currentHour: %i", [THCalendarInfo currentHour]);
NSLog(@"+currentMinute: %i", [THCalendarInfo currentMinute]);
NSLog(@"+currentSecond: %i", [THCalendarInfo currentSecond]);That results in this output:
Month: December
Date: 25
Day: Monday
Days in Month: 31
-moveToPreviousDay
Date: 24
Day: Sunday
-moveToNextMonth
Month: January
Date: 24
Day: Wednesday
Days in Month: 31
-moveToNextYear
Month: January
Date: 24
Year: 2008
Day: Thursday
Days in Month: 31
-moveToFirstDayOfMonth
Month: January
Date: 1
Day: Tuesday
-resetDateAndTimeToCurrent
NSDate rep: 2006-12-25 08:58:11 -0800
-adjustDays:81
Month: March
Date: 16
-adjustDays:-81
Month: December
Date: 25
+currentHour: 8
+currentMinute: 58
+currentSecond: 11You can also also get the current date/time as CFAbsoluteTime, NSDate or NSCalendarDate. Dealing with licenses is a hassle, but this one is BSD. I hope that's okay.
Enjoy your Christmas present.
Open Source Objective-C Calendar Class
Posted Dec 25, 2006 — 7 comments below
Posted Dec 25, 2006 — 7 comments below








Patrick Thomson — Dec 25, 06 2869
I'll see if I can work this into my application.
I hope you like your gift.
Martin — Dec 25, 06 2870
Clint — Dec 25, 06 2873
Thanks for all your hard work Scott!
Rob — Dec 25, 06 2874
... now I know how to make Fprint functionality in Cocoa, Thanks !
John Devor — Dec 25, 06 2876
Hunter — May 06, 08 5792
btw,
I am looking for a way that how to set system date...
For example, now is 05/07/2008 00:49:53, I want to set datetime to 06/07/2008 00:49:53. that change date without modifing time.
first , I use the popen to run the system command "date" in my program, but it can't change date without modifing time. And it will lost the second info of time...
such as:
NSString *output = [NSString string];
NSString *cmd = [NSString stringWithFormat:@"date 060700492008"];
NSLog(@"exec cmd:%@",cmd);
FILE *pipe = popen([cmd cStringUsingEncoding: NSASCIIStringEncoding], "r");
if (!pipe) return -1;
char buf[1024];
while(fgets(buf, 1024, pipe)) {
output = [output stringByAppendingFormat: @"%s", buf];
}
pclose(pipe);
Is there another way to do that?
help me T_T
Scott Stevenson — May 08, 08 5806
I don't know the answer to that offhand. Probably whatever works in unix in general would work for Mac OS X.