William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | #import "TestDateAdditions.h"
#import <AIUtilities/AIDateAdditions.h>
//2009-03-08T03:00:00.5 PDT - half a second after the start of Daylight Saving Time.
#define TEST_DATE [NSDate dateWithTimeIntervalSinceReferenceDate:258199200.5]
//We're testing converting intervals. If we use a time zone that supports DST, the DST changes screw us up. We need an invariant time zone, and UTC works well for this purpose.
#define TEST_TIME_ZONE [NSTimeZone timeZoneWithName:@"UTC"]
@implementation TestDateAdditions
- (void)testConvertIntervalToWeeks
{
NSDate *now = TEST_DATE;
NSDate *then;
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSTimeZone *tz = TEST_TIME_ZONE;
gregorianCalendar.timeZone = tz;
NSDateComponents *components;
NSInteger weeks, days, hours, minutes;
NSTimeInterval seconds;
//Test exactly one week ago.
components = [[[NSDateComponents alloc] init] autorelease];
components.day = -7;
then = [gregorianCalendar dateByAddingComponents:components toDate:now options:0UL];
[NSDate convertTimeInterval:[now timeIntervalSinceDate:then]
toWeeks:&weeks
days:&days
hours:&hours
minutes:&minutes
seconds:&seconds];
STAssertEquals(weeks, (NSInteger)1, @"Expected the difference between now and 7 days ago, which is %f seconds, to be 1 week; result was %iw, %id, %ih, %im, %fs", [now timeIntervalSinceDate:then], weeks, days, hours, minutes, seconds);
STAssertEquals( days, (NSInteger)0, @"Expected the difference between now and 7 days ago to be 1 week, 0 days; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(hours, (NSInteger)0, @"Expected the difference between now and 7 days ago to be 1 week, 0 hours; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(minutes, (NSInteger)0, @"Expected the difference between now and 7 days ago to be 1 week, 0 minutes; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(seconds, 0.0, @"Expected the difference between now and 7 days ago to be 1 week, 0 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
//Test eight days ago. [Insert obligatory Beatles reference]
components.day = -8;
then = [gregorianCalendar dateByAddingComponents:components toDate:now options:0UL];
[NSDate convertTimeInterval:[now timeIntervalSinceDate:then]
toWeeks:&weeks
days:&days
hours:&hours
minutes:&minutes
seconds:&seconds];
STAssertEquals(weeks, (NSInteger)1, @"Expected the difference between now and 8 days ago to be 1 week, 1 day; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals( days, (NSInteger)1, @"Expected the difference between now and 8 days ago to be 1 week, 1 day; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(hours, (NSInteger)0, @"Expected the difference between now and 8 days ago to be 1 week, 1 day, 0 hours; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(minutes, (NSInteger)0, @"Expected the difference between now and 8 days ago to be 1 week, 1 day, 0 minutes; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(seconds, 0.0, @"Expected the difference between now and 8 days ago to be 1 week, 1 day, 0 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
//Test six days (almost, but not quite, one week) ago. [Insert obligatory DJ Shadow reference]
components = [[[NSDateComponents alloc] init] autorelease];
components.day = -6;
then = [gregorianCalendar dateByAddingComponents:components toDate:now options:0UL];
[NSDate convertTimeInterval:[now timeIntervalSinceDate:then]
toWeeks:&weeks
days:&days
hours:&hours
minutes:&minutes
seconds:&seconds];
STAssertEquals(weeks, (NSInteger)0, @"Expected the difference between now and 6 days ago to be 0 weeks, 6 days; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals( days, (NSInteger)6, @"Expected the difference between now and 6 days ago to be 6 days; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(hours, (NSInteger)0, @"Expected the difference between now and 6 days ago to be 6 days, 0 hours; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(minutes, (NSInteger)0, @"Expected the difference between now and 6 days ago to be 6 days, 0 minutes; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(seconds, 0.0, @"Expected the difference between now and 6 days ago to be 6 days, 0 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
}
- (void)testConvertIntervalToDays
{
NSDate *now = TEST_DATE;
NSDate *then;
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSTimeZone *tz = TEST_TIME_ZONE;
gregorianCalendar.timeZone = tz;
NSDateComponents *components;
NSInteger weeks, days, hours, minutes;
NSTimeInterval seconds;
//Test one day ago.
components = [[[NSDateComponents alloc] init] autorelease];
components.day = -1;
then = [gregorianCalendar dateByAddingComponents:components toDate:now options:0UL];
[NSDate convertTimeInterval:[now timeIntervalSinceDate:then]
toWeeks:&weeks
days:&days
hours:&hours
minutes:&minutes
seconds:&seconds];
STAssertEquals(weeks, (NSInteger)0, @"Expected the difference between now and 1 day ago to be 0 weeks, 1 day; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals( days, (NSInteger)1, @"Expected the difference between now and 1 day ago to be 1 day; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(hours, (NSInteger)0, @"Expected the difference between now and 1 day ago to be 1 day, 0 hours; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(minutes, (NSInteger)0, @"Expected the difference between now and 1 day ago to be 1 day, 0 minutes; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(seconds, 0.0, @"Expected the difference between now and 1 day ago to be 1 day, 0 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
//Test one day ago, expressed as hours.
components = [[[NSDateComponents alloc] init] autorelease];
components.hour = -24;
then = [gregorianCalendar dateByAddingComponents:components toDate:now options:0UL];
[NSDate convertTimeInterval:[now timeIntervalSinceDate:then]
toWeeks:&weeks
days:&days
hours:&hours
minutes:&minutes
seconds:&seconds];
STAssertEquals(weeks, (NSInteger)0, @"Expected the difference between now and 1 day ago to be 0 weeks, 1 day; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals( days, (NSInteger)1, @"Expected the difference between now and 1 day ago to be 1 day; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(hours, (NSInteger)0, @"Expected the difference between now and 1 day ago to be 1 day, 0 hours; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(minutes, (NSInteger)0, @"Expected the difference between now and 1 day ago to be 1 day, 0 minutes; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(seconds, 0.0, @"Expected the difference between now and 1 day ago to be 1 day, 0 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
//Test 23 hours (almost, but not quite, one day) ago.
components = [[[NSDateComponents alloc] init] autorelease];
components.hour = -23;
then = [gregorianCalendar dateByAddingComponents:components toDate:now options:0UL];
[NSDate convertTimeInterval:[now timeIntervalSinceDate:then]
toWeeks:&weeks
days:&days
hours:&hours
minutes:&minutes
seconds:&seconds];
STAssertEquals(weeks, (NSInteger)0, @"Expected the difference between now and 23 hours ago to be 0 weeks, 23 hours; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals( days, (NSInteger)0, @"Expected the difference between now and 23 hours ago to be 0 days, 23 hours; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(hours, (NSInteger)23, @"Expected the difference between now and 23 hours ago to be 23 hours; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(minutes, (NSInteger)0, @"Expected the difference between now and 23 hours ago to be 23 hours, 0 minutes; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(seconds, 0.0, @"Expected the difference between now and 23 hours ago to be 23 hours, 0 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
}
- (void)testConvertIntervalToHours
{
NSDate *now = TEST_DATE;
NSDate *then;
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSTimeZone *tz = TEST_TIME_ZONE;
gregorianCalendar.timeZone = tz;
NSDateComponents *components;
NSInteger weeks, days, hours, minutes;
NSTimeInterval seconds;
//Test one hour ago.
components = [[[NSDateComponents alloc] init] autorelease];
components.hour = -1;
then = [gregorianCalendar dateByAddingComponents:components toDate:now options:0UL];
[NSDate convertTimeInterval:[now timeIntervalSinceDate:then]
toWeeks:&weeks
days:&days
hours:&hours
minutes:&minutes
seconds:&seconds];
STAssertEquals(weeks, (NSInteger)0, @"Expected the difference between now and 1 hour ago to be 0 weeks, 1 hour; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals( days, (NSInteger)0, @"Expected the difference between now and 1 hour ago to be 0 days, 1 hour; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(hours, (NSInteger)1, @"Expected the difference between now and 1 hour ago to be 1 hour; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(minutes, (NSInteger)0, @"Expected the difference between now and 1 hour ago to be 1 hour, 0 minutes; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(seconds, 0.0, @"Expected the difference between now and 1 hour ago to be 1 hour, 0 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
//Test one hour ago, expressed as minutes.
components = [[[NSDateComponents alloc] init] autorelease];
components.minute = -60;
then = [gregorianCalendar dateByAddingComponents:components toDate:now options:0UL];
[NSDate convertTimeInterval:[now timeIntervalSinceDate:then]
toWeeks:&weeks
days:&days
hours:&hours
minutes:&minutes
seconds:&seconds];
STAssertEquals(weeks, (NSInteger)0, @"Expected the difference between now and 1 hour ago to be 0 weeks, 1 hour; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals( days, (NSInteger)0, @"Expected the difference between now and 1 hour ago to be 0 days, 1 hour; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(hours, (NSInteger)1, @"Expected the difference between now and 1 hour ago to be 1 hour; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(minutes, (NSInteger)0, @"Expected the difference between now and 1 hour ago to be 1 hour, 0 minutes; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(seconds, 0.0, @"Expected the difference between now and 1 hour ago to be 1 hour, 0 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
//Test 59 minutes (almost, but not quite, one hour) ago.
components = [[[NSDateComponents alloc] init] autorelease];
components.minute = -59;
then = [gregorianCalendar dateByAddingComponents:components toDate:now options:0UL];
[NSDate convertTimeInterval:[now timeIntervalSinceDate:then]
toWeeks:&weeks
days:&days
hours:&hours
minutes:&minutes
seconds:&seconds];
STAssertEquals(weeks, (NSInteger)0, @"Expected the difference between now and 59 minutes ago to be 0 weeks, 59 minutes; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals( days, (NSInteger)0, @"Expected the difference between now and 59 minutes ago to be 0 days, 59 minutes; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(hours, (NSInteger)0, @"Expected the difference between now and 59 minutes ago to be 0 hours, 59 minutes; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(minutes, (NSInteger)59, @"Expected the difference between now and 59 minutes ago to be 59 minutes; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(seconds, 0.0, @"Expected the difference between now and 59 minutes ago to be 59 minutes, 0 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
}
- (void)testConvertIntervalToMinutes
{
NSDate *now = TEST_DATE;
NSDate *then;
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSTimeZone *tz = TEST_TIME_ZONE;
gregorianCalendar.timeZone = tz;
NSDateComponents *components;
NSInteger weeks, days, hours, minutes;
NSTimeInterval seconds;
//Test one minute ago.
components = [[[NSDateComponents alloc] init] autorelease];
components.minute = -1;
then = [gregorianCalendar dateByAddingComponents:components toDate:now options:0UL];
[NSDate convertTimeInterval:[now timeIntervalSinceDate:then]
toWeeks:&weeks
days:&days
hours:&hours
minutes:&minutes
seconds:&seconds];
STAssertEquals(weeks, (NSInteger)0, @"Expected the difference between now and 1 minute ago to be 0 weeks, 1 minute; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals( days, (NSInteger)0, @"Expected the difference between now and 1 minute ago to be 0 days, 1 minute; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(hours, (NSInteger)0, @"Expected the difference between now and 1 minute ago to be 0 hours, 1 minute; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(minutes, (NSInteger)1, @"Expected the difference between now and 1 minute ago to be 1 minute; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(seconds, 0.0, @"Expected the difference between now and 1 minute ago to be 1 minute, 0 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
//Test 59 seconds (almost, but not quite, one minute) ago.
components = [[[NSDateComponents alloc] init] autorelease];
components.second = -59;
then = [gregorianCalendar dateByAddingComponents:components toDate:now options:0UL];
[NSDate convertTimeInterval:[now timeIntervalSinceDate:then]
toWeeks:&weeks
days:&days
hours:&hours
minutes:&minutes
seconds:&seconds];
STAssertEquals(weeks, (NSInteger)0, @"Expected the difference between now and 59 seconds ago to be 0 weeks, 59 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals( days, (NSInteger)0, @"Expected the difference between now and 59 seconds ago to be 0 days, 59 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(hours, (NSInteger)0, @"Expected the difference between now and 59 seconds ago to be 0 hours, 59 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(minutes, (NSInteger)0, @"Expected the difference between now and 59 seconds ago to be 0 minutes, 59 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(seconds, 59.0, @"Expected the difference between now and 59 seconds ago to be 59 seconds; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
}
- (void)testConvertIntervalToSeconds
{
NSDate *now = TEST_DATE;
NSDate *then;
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSTimeZone *tz = TEST_TIME_ZONE;
gregorianCalendar.timeZone = tz;
NSDateComponents *components;
NSInteger weeks, days, hours, minutes;
NSTimeInterval seconds;
//Test one second ago.
components = [[[NSDateComponents alloc] init] autorelease];
components.second = -1;
then = [gregorianCalendar dateByAddingComponents:components toDate:now options:0UL];
[NSDate convertTimeInterval:[now timeIntervalSinceDate:then]
toWeeks:&weeks
days:&days
hours:&hours
minutes:&minutes
seconds:&seconds];
STAssertEquals(weeks, (NSInteger)0, @"Expected the difference between now and 1 second ago to be 0 weeks, 1 second; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals( days, (NSInteger)0, @"Expected the difference between now and 1 second ago to be 0 days, 1 second; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(hours, (NSInteger)0, @"Expected the difference between now and 1 second ago to be 0 hours, 1 second; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(minutes, (NSInteger)0, @"Expected the difference between now and 1 second ago to be 0 minutes, 1 second; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
STAssertEquals(seconds, 1.0, @"Expected the difference between now and 1 second ago to be 1 second; result was %iw, %id, %ih, %im, %fs", weeks, days, hours, minutes, seconds);
}
@end
|