Skip to content
Browse files

some stuff from goal tender. need to write tests for all this and cle…

…an it up a lot.
  • Loading branch information...
1 parent 934326b commit be3c8540d4f6e3c6d8126eb1967e7b590d70b040 Brian Cooke committed Dec 26, 2008
Showing with 179 additions and 13 deletions.
  1. +23 −0 NSDate+roobasoft.h
  2. +143 −11 NSDate+roobasoft.m
  3. +1 −1 NSMutableURLRequest+roobasoft.m
  4. +2 −0 NSString+roobasoft.h
  5. +10 −1 NSString+roobasoft.m
View
23 NSDate+roobasoft.h
@@ -33,6 +33,11 @@
+ (NSDate *) midnight;
- (NSDate *) midnight;
++ (NSDate *) justBeforeMidnight;
+- (NSDate *) justBeforeMidnight;
+
++ (NSDate *) addDays:(NSInteger)days;
+- (NSDate *) addDays:(NSInteger)days;
+ (NSDate *) tomorrow;
- (NSDate *) tomorrow;
@@ -41,6 +46,24 @@
- (NSDate *) yesterday;
- (NSString *) pretty;
+- (NSString *) prettyWithYear:(BOOL)withYear;
+- (NSString *) prettyWithYear:(BOOL)withYear yesterdaySupport:(BOOL)yesterdaySupport;
- (NSString *) description;
+
+- (NSInteger) daysTill:(NSDate *)otherDate;
+
++ (NSDate *) nextWeekday;
+- (NSDate *) nextWeekday;
+
++ (NSDate *) noon;
+- (NSDate *) noon;
+
++ (NSDate *) friday;
+- (NSDate *) friday;
+
++ (NSDate *) saturday;
+- (NSDate *) saturday;
+
+
@end
View
154 NSDate+roobasoft.m
@@ -29,7 +29,22 @@
static int midnight_offset = 0;
+@interface NSDate (private_roobasoft)
+- (NSDate *) nextDay:(NSInteger)weekdayUnit;
+@end
+
+
@implementation NSDate (roobasoft)
+
+#ifdef DEBUG
+// tomfoolery
+//+ (id) date
+//{
+// return [NSDate dateWithTimeIntervalSinceNow:(60*60*24*(4))];
+//}
+#endif
+
+
+ (void) setMidnightOffset:(NSInteger)offset
{
midnight_offset = offset;
@@ -54,25 +69,46 @@ - (NSDate *) midnight
}
}
-+ (NSDate *) tomorrow
++ (NSDate *) justBeforeMidnight
{
- return [[NSDate date] tomorrow];
+ return [[NSDate date] justBeforeMidnight];
}
-- (NSDate *) tomorrow
+- (NSDate *) justBeforeMidnight
{
- NSDate *t = [self addTimeInterval:(24*60*60)];
+ return [NSDate dateWithTimeIntervalSince1970:[[self midnight] timeIntervalSince1970]-60];
+}
+
++ (NSDate *) addDays:(NSInteger)days
+{
+ return [[NSDate date] addDays:days];
+}
+
+- (NSDate *) addDays:(NSInteger)days
+{
+ NSDate *t = [self addTimeInterval:days * (24*60*60)];
// handle day light saving changes!
// if the hour is different we were adjusted. adjust back.
NSDateComponents *self_comps = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:self];
- NSDateComponents *tomorrow_comps = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:t];
+ NSDateComponents *future_comps = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:t];
- if ([tomorrow_comps hour] != [self_comps hour])
+ if ([future_comps hour] != [self_comps hour])
{
- t = [t addTimeInterval:(60*60)*[tomorrow_comps hour] - [self_comps hour]];
+ t = [t addTimeInterval:(60*60)*[future_comps hour] - [self_comps hour]];
}
return t;
+
+}
+
++ (NSDate *) tomorrow
+{
+ return [[NSDate date] tomorrow];
+}
+
+- (NSDate *) tomorrow
+{
+ return [self addDays:1];
}
+ (NSDate *) yesterday
@@ -85,23 +121,101 @@ - (NSDate *) yesterday
return [self addTimeInterval:(-24*60*60)];
}
++ (NSDate *) friday
+{
+ return [[NSDate date] friday];
+}
+
+- (NSDate *) friday
+{
+ return [self nextDay:6];
+}
+
++ (NSDate *) saturday
+{
+ return [[NSDate date] saturday];
+}
+
+- (NSDate *) saturday
+{
+ return [self nextDay:7];
+}
+
+- (NSDate *) nextDay:(NSInteger)weekdayUnit
+{
+ NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit) fromDate:self];
+
+ if (weekdayUnit-[comps weekday] <= 0)
+ [comps setDay:7 + [comps day] + weekdayUnit - [comps weekday]];
+ else
+ [comps setDay:[comps day] + weekdayUnit - [comps weekday]];
+
+ return [[NSCalendar currentCalendar] dateFromComponents:comps];
+}
+
+
++ (NSDate *) nextWeekday
+{
+ return [[NSDate date] nextWeekday];
+}
+
+- (NSDate *) nextWeekday
+{
+ // is it a friday or a saturday?
+ NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSWeekdayCalendarUnit) fromDate:self];
+
+ if ([comps weekday] == 6)
+ {
+ // add 3 days to get us to 12:00, monday
+ // sat, sun, mon.noon
+ return [[self addDays:3] noon];
+ }
+ else if ([comps weekday] == 7)
+ {
+ return [[self addDays:2] noon];
+ }
+ else
+ {
+ return [[self addDays:1] noon];
+ }
+}
+
- (NSString *) pretty
{
+ return [self prettyWithYear:NO];
+}
+
+- (NSString *)prettyWithYear:(BOOL)withYear
+{
+ return [self prettyWithYear:withYear yesterdaySupport:YES];
+}
+
+- (NSString *) prettyWithYear:(BOOL)withYear yesterdaySupport:(BOOL)yesterdaySupport
+{
// handle 'today' and 'yesterday'
if ([[self midnight] compare:[NSDate midnight]] == NSOrderedSame)
{
return @"Today";
}
- else if ([[self midnight] compare:[[NSDate yesterday] midnight]] == NSOrderedSame)
+ else if (yesterdaySupport && [[self midnight] compare:[[NSDate yesterday] midnight]] == NSOrderedSame)
{
return @"Yesterday";
}
- NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit|NSDayCalendarUnit fromDate:self];
+ NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit fromDate:self];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- NSString *ret = [NSString stringWithFormat:@"%@ %d", [[dateFormatter shortMonthSymbols] objectAtIndex:[comps month]-1], [comps day]];
+
+ NSString *ret = nil;
+ if (withYear)
+ {
+ ret = [NSString stringWithFormat:@"%@ %d, %d", [[dateFormatter shortMonthSymbols] objectAtIndex:[comps month]-1], [comps day], [comps year]];
+ }
+ else
+ {
+ ret = [NSString stringWithFormat:@"%@ %d", [[dateFormatter shortMonthSymbols] objectAtIndex:[comps month]-1], [comps day]];
+ }
[dateFormatter release];
- return ret;
+ return ret;
}
- (NSString *) description
@@ -114,4 +228,22 @@ - (NSString *) description
return ret;
}
+
+- (NSInteger) daysTill:(NSDate *)otherDate
+{
+ return (NSInteger)(([[otherDate midnight] timeIntervalSince1970] - [[self midnight] timeIntervalSince1970]) / (24*60*60));
+}
+
++ (NSDate *) noon
+{
+ return [[NSDate date] noon];
+}
+
+- (NSDate *) noon
+{
+ NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:self];
+
+ return [[[NSCalendar currentCalendar] dateFromComponents:comps] addTimeInterval:12*60*60];
+}
+
@end
View
2 NSMutableURLRequest+roobasoft.m
@@ -24,7 +24,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#import "NSMutableURLRequest+roobasoft.h"
-
+#import "NSData-Base64Extensions.h"
@implementation NSMutableURLRequest (roobasoft)
View
2 NSString+roobasoft.h
@@ -32,4 +32,6 @@
- (NSString *) urlEncode;
- (NSDate *) dateFromJSON; // 2008-10-29T06:22:08-0600 -> NSDate
+
+- (NSString *) truncate:(NSInteger)size;
@end
View
11 NSString+roobasoft.m
@@ -45,15 +45,24 @@ - (NSString *)urlEncode
// assumes self is in form
// 2008-10-29T06:22:08-0600
+// 2009-01-02T15:40:19Z
- (NSDate *) dateFromJSON
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"y-M-d H:m:sZ"];
- NSDate *ret = [formatter dateFromString:[self stringByReplacingOccurrencesOfString:@"T" withString:@" "]];
+ NSDate *ret = [formatter dateFromString:[[self stringByReplacingOccurrencesOfString:@"T" withString:@" "] stringByReplacingOccurrencesOfString:@"Z" withString:@"+0000"]];
[formatter release];
return ret;
}
+- (NSString *) truncate:(NSInteger)size
+{
+ if ([self length] <= size)
+ return self;
+
+ return [NSString stringWithFormat:@"%@...", [[self substringToIndex:size-3] stringByReplacingOccurrencesOfString:@"\n" withString:@" "]];
+}
+
@end

0 comments on commit be3c854

Please sign in to comment.
Something went wrong with that request. Please try again.