[483] | 1 | define(["doh", "../date"], function(doh, date){ |
---|
| 2 | doh.register("tests.date.util", [ |
---|
| 3 | |
---|
| 4 | /* Informational Functions |
---|
| 5 | **************************/ |
---|
| 6 | |
---|
| 7 | function test_date_getDaysInMonth(t){ |
---|
| 8 | // months other than February |
---|
| 9 | t.is(31, date.getDaysInMonth(new Date(2006,0,1))); |
---|
| 10 | t.is(31, date.getDaysInMonth(new Date(2006,2,1))); |
---|
| 11 | t.is(30, date.getDaysInMonth(new Date(2006,3,1))); |
---|
| 12 | t.is(31, date.getDaysInMonth(new Date(2006,4,1))); |
---|
| 13 | t.is(30, date.getDaysInMonth(new Date(2006,5,1))); |
---|
| 14 | t.is(31, date.getDaysInMonth(new Date(2006,6,1))); |
---|
| 15 | t.is(31, date.getDaysInMonth(new Date(2006,7,1))); |
---|
| 16 | t.is(30, date.getDaysInMonth(new Date(2006,8,1))); |
---|
| 17 | t.is(31, date.getDaysInMonth(new Date(2006,9,1))); |
---|
| 18 | t.is(30, date.getDaysInMonth(new Date(2006,10,1))); |
---|
| 19 | t.is(31, date.getDaysInMonth(new Date(2006,11,1))); |
---|
| 20 | |
---|
| 21 | // Februarys |
---|
| 22 | t.is(28, date.getDaysInMonth(new Date(2006,1,1))); |
---|
| 23 | t.is(29, date.getDaysInMonth(new Date(2004,1,1))); |
---|
| 24 | t.is(29, date.getDaysInMonth(new Date(2000,1,1))); |
---|
| 25 | t.is(28, date.getDaysInMonth(new Date(1900,1,1))); |
---|
| 26 | t.is(28, date.getDaysInMonth(new Date(1800,1,1))); |
---|
| 27 | t.is(28, date.getDaysInMonth(new Date(1700,1,1))); |
---|
| 28 | t.is(29, date.getDaysInMonth(new Date(1600,1,1))); |
---|
| 29 | }, |
---|
| 30 | |
---|
| 31 | function test_date_isLeapYear(t){ |
---|
| 32 | t.f(date.isLeapYear(new Date(2006,0,1))); |
---|
| 33 | t.t(date.isLeapYear(new Date(2004,0,1))); |
---|
| 34 | t.t(date.isLeapYear(new Date(2000,0,1))); |
---|
| 35 | t.f(date.isLeapYear(new Date(1900,0,1))); |
---|
| 36 | t.f(date.isLeapYear(new Date(1800,0,1))); |
---|
| 37 | t.f(date.isLeapYear(new Date(1700,0,1))); |
---|
| 38 | t.t(date.isLeapYear(new Date(1600,0,1))); |
---|
| 39 | }, |
---|
| 40 | |
---|
| 41 | // The getTimezone function pulls from either the date's toString or |
---|
| 42 | // toLocaleString method -- it's really just a string-processing |
---|
| 43 | // function (assuming the Date obj passed in supporting both toString |
---|
| 44 | // and toLocaleString) and as such can be tested for multiple browsers |
---|
| 45 | // by manually settting up fake Date objects with the actual strings |
---|
| 46 | // produced by various browser/OS combinations. |
---|
| 47 | // FIXME: the function and tests are not localized. |
---|
| 48 | function test_date_getTimezoneName(t){ |
---|
| 49 | |
---|
| 50 | // Create a fake Date object with toString and toLocaleString |
---|
| 51 | // results manually set to simulate tests for multiple browsers |
---|
| 52 | function FakeDate(str, strLocale){ |
---|
| 53 | this.str = str || ''; |
---|
| 54 | this.strLocale = strLocale || ''; |
---|
| 55 | this.toString = function(){ |
---|
| 56 | return this.str; |
---|
| 57 | }; |
---|
| 58 | this.toLocaleString = function(){ |
---|
| 59 | return this.strLocale; |
---|
| 60 | }; |
---|
| 61 | } |
---|
| 62 | var dt = new FakeDate(); |
---|
| 63 | |
---|
| 64 | // FF 1.5 Ubuntu Linux (Breezy) |
---|
| 65 | dt.str = 'Sun Sep 17 2006 22:25:51 GMT-0500 (CDT)'; |
---|
| 66 | dt.strLocale = 'Sun 17 Sep 2006 10:25:51 PM CDT'; |
---|
| 67 | t.is('CDT', date.getTimezoneName(dt)); |
---|
| 68 | |
---|
| 69 | // Safari 2.0 Mac OS X 10.4 |
---|
| 70 | dt.str = 'Sun Sep 17 2006 22:55:01 GMT-0500'; |
---|
| 71 | dt.strLocale = 'September 17, 2006 10:55:01 PM CDT'; |
---|
| 72 | t.is('CDT', date.getTimezoneName(dt)); |
---|
| 73 | |
---|
| 74 | // FF 1.5 Mac OS X 10.4 |
---|
| 75 | dt.str = 'Sun Sep 17 2006 22:57:18 GMT-0500 (CDT)'; |
---|
| 76 | dt.strLocale = 'Sun Sep 17 22:57:18 2006'; |
---|
| 77 | t.is('CDT', date.getTimezoneName(dt)); |
---|
| 78 | |
---|
| 79 | // Opera 9 Mac OS X 10.4 -- no TZ data expect empty string return |
---|
| 80 | dt.str = 'Sun, 17 Sep 2006 22:58:06 GMT-0500'; |
---|
| 81 | dt.strLocale = 'Sunday September 17, 22:58:06 GMT-0500 2006'; |
---|
| 82 | t.is('', date.getTimezoneName(dt)); |
---|
| 83 | |
---|
| 84 | // IE 6 Windows XP |
---|
| 85 | dt.str = 'Mon Sep 18 11:21:07 CDT 2006'; |
---|
| 86 | dt.strLocale = 'Monday, September 18, 2006 11:21:07 AM'; |
---|
| 87 | t.is('CDT', date.getTimezoneName(dt)); |
---|
| 88 | |
---|
| 89 | // Opera 9 Ubuntu Linux (Breezy) -- no TZ data expect empty string return |
---|
| 90 | dt.str = 'Mon, 18 Sep 2006 13:30:32 GMT-0500'; |
---|
| 91 | dt.strLocale = 'Monday September 18, 13:30:32 GMT-0500 2006'; |
---|
| 92 | t.is('', date.getTimezoneName(dt)); |
---|
| 93 | |
---|
| 94 | // IE 5.5 Windows 2000 |
---|
| 95 | dt.str = 'Mon Sep 18 13:49:22 CDT 2006'; |
---|
| 96 | dt.strLocale = 'Monday, September 18, 2006 1:49:22 PM'; |
---|
| 97 | t.is('CDT', date.getTimezoneName(dt)); |
---|
| 98 | } |
---|
| 99 | ] |
---|
| 100 | ); |
---|
| 101 | |
---|
| 102 | doh.register("tests.date.math", |
---|
| 103 | [ |
---|
| 104 | function test_date_compare(t){ |
---|
| 105 | var d1=new Date(); |
---|
| 106 | d1.setHours(0); |
---|
| 107 | var d2=new Date(); |
---|
| 108 | d2.setFullYear(2005); |
---|
| 109 | d2.setHours(12); |
---|
| 110 | t.is(0, date.compare(d1, d1)); |
---|
| 111 | t.is(1, date.compare(d1, d2, "date")); |
---|
| 112 | t.is(-1, date.compare(d2, d1, "date")); |
---|
| 113 | t.is(-1, date.compare(d1, d2, "time")); |
---|
| 114 | t.is(1, date.compare(d1, d2, "datetime")); |
---|
| 115 | }, |
---|
| 116 | function test_date_add(t){ |
---|
| 117 | var interv = ''; // Interval (e.g., year, month) |
---|
| 118 | var dtA = null; // Date to increment |
---|
| 119 | var dtB = null; // Expected result date |
---|
| 120 | |
---|
| 121 | interv = "year"; |
---|
| 122 | dtA = new Date(2005, 11, 27); |
---|
| 123 | dtB = new Date(2006, 11, 27); |
---|
| 124 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 125 | |
---|
| 126 | dtA = new Date(2005, 11, 27); |
---|
| 127 | dtB = new Date(2004, 11, 27); |
---|
| 128 | t.is(dtB, date.add(dtA, interv, -1)); |
---|
| 129 | |
---|
| 130 | dtA = new Date(2000, 1, 29); |
---|
| 131 | dtB = new Date(2001, 1, 28); |
---|
| 132 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 133 | |
---|
| 134 | dtA = new Date(2000, 1, 29); |
---|
| 135 | dtB = new Date(2005, 1, 28); |
---|
| 136 | t.is(dtB, date.add(dtA, interv, 5)); |
---|
| 137 | |
---|
| 138 | dtA = new Date(1900, 11, 31); |
---|
| 139 | dtB = new Date(1930, 11, 31); |
---|
| 140 | t.is(dtB, date.add(dtA, interv, 30)); |
---|
| 141 | |
---|
| 142 | dtA = new Date(1995, 11, 31); |
---|
| 143 | dtB = new Date(2030, 11, 31); |
---|
| 144 | t.is(dtB, date.add(dtA, interv, 35)); |
---|
| 145 | |
---|
| 146 | interv = "quarter"; |
---|
| 147 | dtA = new Date(2000, 0, 1); |
---|
| 148 | dtB = new Date(2000, 3, 1); |
---|
| 149 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 150 | |
---|
| 151 | dtA = new Date(2000, 1, 29); |
---|
| 152 | dtB = new Date(2000, 7, 29); |
---|
| 153 | t.is(dtB, date.add(dtA, interv, 2)); |
---|
| 154 | |
---|
| 155 | dtA = new Date(2000, 1, 29); |
---|
| 156 | dtB = new Date(2001, 1, 28); |
---|
| 157 | t.is(dtB, date.add(dtA, interv, 4)); |
---|
| 158 | |
---|
| 159 | interv = "month"; |
---|
| 160 | dtA = new Date(2000, 0, 1); |
---|
| 161 | dtB = new Date(2000, 1, 1); |
---|
| 162 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 163 | |
---|
| 164 | dtA = new Date(2000, 0, 31); |
---|
| 165 | dtB = new Date(2000, 1, 29); |
---|
| 166 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 167 | |
---|
| 168 | dtA = new Date(2000, 1, 29); |
---|
| 169 | dtB = new Date(2001, 1, 28); |
---|
| 170 | t.is(dtB, date.add(dtA, interv, 12)); |
---|
| 171 | |
---|
| 172 | interv = "week"; |
---|
| 173 | dtA = new Date(2000, 0, 1); |
---|
| 174 | dtB = new Date(2000, 0, 8); |
---|
| 175 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 176 | |
---|
| 177 | interv = "day"; |
---|
| 178 | dtA = new Date(2000, 0, 1); |
---|
| 179 | dtB = new Date(2000, 0, 2); |
---|
| 180 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 181 | |
---|
| 182 | dtA = new Date(2001, 0, 1); |
---|
| 183 | dtB = new Date(2002, 0, 1); |
---|
| 184 | t.is(dtB, date.add(dtA, interv, 365)); |
---|
| 185 | |
---|
| 186 | dtA = new Date(2000, 0, 1); |
---|
| 187 | dtB = new Date(2001, 0, 1); |
---|
| 188 | t.is(dtB, date.add(dtA, interv, 366)); |
---|
| 189 | |
---|
| 190 | dtA = new Date(2000, 1, 28); |
---|
| 191 | dtB = new Date(2000, 1, 29); |
---|
| 192 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 193 | |
---|
| 194 | dtA = new Date(2001, 1, 28); |
---|
| 195 | dtB = new Date(2001, 2, 1); |
---|
| 196 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 197 | |
---|
| 198 | dtA = new Date(2000, 2, 1); |
---|
| 199 | dtB = new Date(2000, 1, 29); |
---|
| 200 | t.is(dtB, date.add(dtA, interv, -1)); |
---|
| 201 | |
---|
| 202 | dtA = new Date(2001, 2, 1); |
---|
| 203 | dtB = new Date(2001, 1, 28); |
---|
| 204 | t.is(dtB, date.add(dtA, interv, -1)); |
---|
| 205 | |
---|
| 206 | dtA = new Date(2000, 0, 1); |
---|
| 207 | dtB = new Date(1999, 11, 31); |
---|
| 208 | t.is(dtB, date.add(dtA, interv, -1)); |
---|
| 209 | |
---|
| 210 | interv = "weekday"; |
---|
| 211 | // Sat, Jan 1 |
---|
| 212 | dtA = new Date(2000, 0, 1); |
---|
| 213 | // Should be Mon, Jan 3 |
---|
| 214 | dtB = new Date(2000, 0, 3); |
---|
| 215 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 216 | |
---|
| 217 | // Sun, Jan 2 |
---|
| 218 | dtA = new Date(2000, 0, 2); |
---|
| 219 | // Should be Mon, Jan 3 |
---|
| 220 | dtB = new Date(2000, 0, 3); |
---|
| 221 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 222 | |
---|
| 223 | // Sun, Jan 2 |
---|
| 224 | dtA = new Date(2000, 0, 2); |
---|
| 225 | // Should be Fri, Jan 7 |
---|
| 226 | dtB = new Date(2000, 0, 7); |
---|
| 227 | t.is(dtB, date.add(dtA, interv, 5)); |
---|
| 228 | |
---|
| 229 | // Sun, Jan 2 |
---|
| 230 | dtA = new Date(2000, 0, 2); |
---|
| 231 | // Should be Mon, Jan 10 |
---|
| 232 | dtB = new Date(2000, 0, 10); |
---|
| 233 | t.is(dtB, date.add(dtA, interv, 6)); |
---|
| 234 | |
---|
| 235 | // Mon, Jan 3 |
---|
| 236 | dtA = new Date(2000, 0, 3); |
---|
| 237 | // Should be Mon, Jan 17 |
---|
| 238 | dtB = new Date(2000, 0, 17); |
---|
| 239 | t.is(dtB, date.add(dtA, interv, 10)); |
---|
| 240 | |
---|
| 241 | // Sat, Jan 8 |
---|
| 242 | dtA = new Date(2000, 0, 8); |
---|
| 243 | // Should be Mon, Jan 3 |
---|
| 244 | dtB = new Date(2000, 0, 3); |
---|
| 245 | t.is(dtB, date.add(dtA, interv, -5)); |
---|
| 246 | |
---|
| 247 | // Sun, Jan 9 |
---|
| 248 | dtA = new Date(2000, 0, 9); |
---|
| 249 | // Should be Wed, Jan 5 |
---|
| 250 | dtB = new Date(2000, 0, 5); |
---|
| 251 | t.is(dtB, date.add(dtA, interv, -3)); |
---|
| 252 | |
---|
| 253 | // Sun, Jan 23 |
---|
| 254 | dtA = new Date(2000, 0, 23); |
---|
| 255 | // Should be Fri, Jan 7 |
---|
| 256 | dtB = new Date(2000, 0, 7); |
---|
| 257 | t.is(dtB, date.add(dtA, interv, -11)); |
---|
| 258 | |
---|
| 259 | interv = "hour"; |
---|
| 260 | dtA = new Date(2000, 0, 1, 11); |
---|
| 261 | dtB = new Date(2000, 0, 1, 12); |
---|
| 262 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 263 | |
---|
| 264 | dtA = new Date(2001, 9, 28, 0); |
---|
| 265 | dtB = new Date(dtA.getTime() + (60 * 60 * 1000)); |
---|
| 266 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 267 | |
---|
| 268 | dtA = new Date(2001, 9, 28, 23); |
---|
| 269 | dtB = new Date(2001, 9, 29, 0); |
---|
| 270 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 271 | |
---|
| 272 | dtA = new Date(2001, 11, 31, 23); |
---|
| 273 | dtB = new Date(2002, 0, 1, 0); |
---|
| 274 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 275 | |
---|
| 276 | interv = "minute"; |
---|
| 277 | dtA = new Date(2000, 11, 31, 23, 59); |
---|
| 278 | dtB = new Date(2001, 0, 1, 0, 0); |
---|
| 279 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 280 | |
---|
| 281 | dtA = new Date(2000, 11, 27, 12, 2); |
---|
| 282 | dtB = new Date(2000, 11, 27, 13, 2); |
---|
| 283 | t.is(dtB, date.add(dtA, interv, 60)); |
---|
| 284 | |
---|
| 285 | interv = "second"; |
---|
| 286 | dtA = new Date(2000, 11, 31, 23, 59, 59); |
---|
| 287 | dtB = new Date(2001, 0, 1, 0, 0, 0); |
---|
| 288 | t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 289 | |
---|
| 290 | dtA = new Date(2000, 11, 27, 8, 10, 59); |
---|
| 291 | dtB = new Date(2000, 11, 27, 8, 11, 59); |
---|
| 292 | t.is(dtB, date.add(dtA, interv, 60)); |
---|
| 293 | |
---|
| 294 | // Test environment JS Date doesn't support millisec? |
---|
| 295 | //interv = "millisecond"; |
---|
| 296 | // |
---|
| 297 | //dtA = new Date(2000, 11, 31, 23, 59, 59, 999); |
---|
| 298 | //dtB = new Date(2001, 0, 1, 0, 0, 0, 0); |
---|
| 299 | //t.is(dtB, date.add(dtA, interv, 1)); |
---|
| 300 | // |
---|
| 301 | //dtA = new Date(2000, 11, 27, 8, 10, 53, 2); |
---|
| 302 | //dtB = new Date(2000, 11, 27, 8, 10, 54, 2); |
---|
| 303 | //t.is(dtB, date.add(dtA, interv, 1000)); |
---|
| 304 | }, |
---|
| 305 | function test_date_diff(t){ |
---|
| 306 | var dtA = null; // First date to compare |
---|
| 307 | var dtB = null; // Second date to compare |
---|
| 308 | var interv = ''; // Interval to compare on (e.g., year, month) |
---|
| 309 | |
---|
| 310 | interv = "year"; |
---|
| 311 | dtA = new Date(2005, 11, 27); |
---|
| 312 | dtB = new Date(2006, 11, 27); |
---|
| 313 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 314 | |
---|
| 315 | dtA = new Date(2000, 11, 31); |
---|
| 316 | dtB = new Date(2001, 0, 1); |
---|
| 317 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 318 | |
---|
| 319 | interv = "quarter"; |
---|
| 320 | dtA = new Date(2000, 1, 29); |
---|
| 321 | dtB = new Date(2001, 2, 1); |
---|
| 322 | t.is(4, date.difference(dtA, dtB, interv)); |
---|
| 323 | |
---|
| 324 | dtA = new Date(2000, 11, 1); |
---|
| 325 | dtB = new Date(2001, 0, 1); |
---|
| 326 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 327 | |
---|
| 328 | interv = "month"; |
---|
| 329 | dtA = new Date(2000, 1, 29); |
---|
| 330 | dtB = new Date(2001, 2, 1); |
---|
| 331 | t.is(13, date.difference(dtA, dtB, interv)); |
---|
| 332 | |
---|
| 333 | dtA = new Date(2000, 11, 1); |
---|
| 334 | dtB = new Date(2001, 0, 1); |
---|
| 335 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 336 | |
---|
| 337 | interv = "week"; |
---|
| 338 | dtA = new Date(2000, 1, 1); |
---|
| 339 | dtB = new Date(2000, 1, 8); |
---|
| 340 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 341 | |
---|
| 342 | dtA = new Date(2000, 1, 28); |
---|
| 343 | dtB = new Date(2000, 2, 6); |
---|
| 344 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 345 | |
---|
| 346 | dtA = new Date(2000, 2, 6); |
---|
| 347 | dtB = new Date(2000, 1, 28); |
---|
| 348 | t.is(-1, date.difference(dtA, dtB, interv)); |
---|
| 349 | |
---|
| 350 | interv = "day"; |
---|
| 351 | dtA = new Date(2000, 1, 29); |
---|
| 352 | dtB = new Date(2000, 2, 1); |
---|
| 353 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 354 | |
---|
| 355 | dtA = new Date(2000, 11, 31); |
---|
| 356 | dtB = new Date(2001, 0, 1); |
---|
| 357 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 358 | |
---|
| 359 | // DST leap -- check for rounding err |
---|
| 360 | // This is dependent on US calendar, but |
---|
| 361 | // shouldn't break in other locales |
---|
| 362 | dtA = new Date(2005, 3, 3); |
---|
| 363 | dtB = new Date(2005, 3, 4); |
---|
| 364 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 365 | |
---|
| 366 | interv = "weekday"; |
---|
| 367 | dtA = new Date(2006, 7, 3); |
---|
| 368 | dtB = new Date(2006, 7, 11); |
---|
| 369 | t.is(6, date.difference(dtA, dtB, interv)); |
---|
| 370 | |
---|
| 371 | // Positive diffs |
---|
| 372 | dtA = new Date(2006, 7, 4); |
---|
| 373 | dtB = new Date(2006, 7, 11); |
---|
| 374 | t.is(5, date.difference(dtA, dtB, interv)); |
---|
| 375 | |
---|
| 376 | dtA = new Date(2006, 7, 5); |
---|
| 377 | dtB = new Date(2006, 7, 11); |
---|
| 378 | t.is(5, date.difference(dtA, dtB, interv)); |
---|
| 379 | |
---|
| 380 | dtA = new Date(2006, 7, 6); |
---|
| 381 | dtB = new Date(2006, 7, 11); |
---|
| 382 | t.is(5, date.difference(dtA, dtB, interv)); |
---|
| 383 | |
---|
| 384 | dtA = new Date(2006, 7, 7); |
---|
| 385 | dtB = new Date(2006, 7, 11); |
---|
| 386 | t.is(4, date.difference(dtA, dtB, interv)); |
---|
| 387 | |
---|
| 388 | dtA = new Date(2006, 7, 7); |
---|
| 389 | dtB = new Date(2006, 7, 13); |
---|
| 390 | t.is(4, date.difference(dtA, dtB, interv)); |
---|
| 391 | |
---|
| 392 | dtA = new Date(2006, 7, 7); |
---|
| 393 | dtB = new Date(2006, 7, 14); |
---|
| 394 | t.is(5, date.difference(dtA, dtB, interv)); |
---|
| 395 | |
---|
| 396 | dtA = new Date(2006, 7, 7); |
---|
| 397 | dtB = new Date(2006, 7, 15); |
---|
| 398 | t.is(6, date.difference(dtA, dtB, interv)); |
---|
| 399 | |
---|
| 400 | dtA = new Date(2006, 7, 7); |
---|
| 401 | dtB = new Date(2006, 7, 28); |
---|
| 402 | t.is(15, date.difference(dtA, dtB, interv)); |
---|
| 403 | |
---|
| 404 | dtA = new Date(2006, 2, 2); |
---|
| 405 | dtB = new Date(2006, 2, 28); |
---|
| 406 | t.is(18, date.difference(dtA, dtB, interv)); |
---|
| 407 | |
---|
| 408 | // Negative diffs |
---|
| 409 | dtA = new Date(2006, 7, 11); |
---|
| 410 | dtB = new Date(2006, 7, 4); |
---|
| 411 | t.is(-5, date.difference(dtA, dtB, interv)); |
---|
| 412 | |
---|
| 413 | dtA = new Date(2006, 7, 11); |
---|
| 414 | dtB = new Date(2006, 7, 5); |
---|
| 415 | t.is(-4, date.difference(dtA, dtB, interv)); |
---|
| 416 | |
---|
| 417 | dtA = new Date(2006, 7, 11); |
---|
| 418 | dtB = new Date(2006, 7, 6); |
---|
| 419 | t.is(-4, date.difference(dtA, dtB, interv)); |
---|
| 420 | |
---|
| 421 | dtA = new Date(2006, 7, 11); |
---|
| 422 | dtB = new Date(2006, 7, 7); |
---|
| 423 | t.is(-4, date.difference(dtA, dtB, interv)); |
---|
| 424 | |
---|
| 425 | dtA = new Date(2006, 7, 13); |
---|
| 426 | dtB = new Date(2006, 7, 7); |
---|
| 427 | t.is(-5, date.difference(dtA, dtB, interv)); |
---|
| 428 | |
---|
| 429 | dtA = new Date(2006, 7, 14); |
---|
| 430 | dtB = new Date(2006, 7, 7); |
---|
| 431 | t.is(-5, date.difference(dtA, dtB, interv)); |
---|
| 432 | |
---|
| 433 | dtA = new Date(2006, 7, 15); |
---|
| 434 | dtB = new Date(2006, 7, 7); |
---|
| 435 | t.is(-6, date.difference(dtA, dtB, interv)); |
---|
| 436 | |
---|
| 437 | dtA = new Date(2006, 7, 28); |
---|
| 438 | dtB = new Date(2006, 7, 7); |
---|
| 439 | t.is(-15, date.difference(dtA, dtB, interv)); |
---|
| 440 | |
---|
| 441 | dtA = new Date(2006, 2, 28); |
---|
| 442 | dtB = new Date(2006, 2, 2); |
---|
| 443 | t.is(-18, date.difference(dtA, dtB, interv)); |
---|
| 444 | |
---|
| 445 | // Two days on the same weekend -- no weekday diff |
---|
| 446 | dtA = new Date(2006, 7, 5); |
---|
| 447 | dtB = new Date(2006, 7, 6); |
---|
| 448 | t.is(0, date.difference(dtA, dtB, interv)); |
---|
| 449 | |
---|
| 450 | interv = "hour"; |
---|
| 451 | dtA = new Date(2000, 11, 31, 23); |
---|
| 452 | dtB = new Date(2001, 0, 1, 0); |
---|
| 453 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 454 | |
---|
| 455 | dtA = new Date(2000, 11, 31, 12); |
---|
| 456 | dtB = new Date(2001, 0, 1, 0); |
---|
| 457 | t.is(12, date.difference(dtA, dtB, interv)); |
---|
| 458 | |
---|
| 459 | interv = "minute"; |
---|
| 460 | dtA = new Date(2000, 11, 31, 23, 59); |
---|
| 461 | dtB = new Date(2001, 0, 1, 0, 0); |
---|
| 462 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 463 | |
---|
| 464 | dtA = new Date(2000, 1, 28, 23, 59); |
---|
| 465 | dtB = new Date(2000, 1, 29, 0, 0); |
---|
| 466 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 467 | |
---|
| 468 | interv = "second"; |
---|
| 469 | dtA = new Date(2000, 11, 31, 23, 59, 59); |
---|
| 470 | dtB = new Date(2001, 0, 1, 0, 0, 0); |
---|
| 471 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 472 | |
---|
| 473 | interv = "millisecond"; |
---|
| 474 | dtA = new Date(2000, 11, 31, 23, 59, 59, 999); |
---|
| 475 | dtB = new Date(2001, 0, 1, 0, 0, 0, 0); |
---|
| 476 | t.is(1, date.difference(dtA, dtB, interv)); |
---|
| 477 | |
---|
| 478 | dtA = new Date(2000, 11, 31, 23, 59, 59, 0); |
---|
| 479 | dtB = new Date(2001, 0, 1, 0, 0, 0, 0); |
---|
| 480 | t.is(1000, date.difference(dtA, dtB, interv)); |
---|
| 481 | }, |
---|
| 482 | function test_date_add_diff_year(t){ |
---|
| 483 | var interv = ''; // Interval (e.g., year, month) |
---|
| 484 | var dtA = null; // Date to increment |
---|
| 485 | var dtB = null; // Expected result date |
---|
| 486 | |
---|
| 487 | interv = "year"; |
---|
| 488 | dtA = new Date(2005, 11, 27); |
---|
| 489 | dtB = date.add(dtA, interv, 1); |
---|
| 490 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 491 | |
---|
| 492 | dtA = new Date(2005, 11, 27); |
---|
| 493 | dtB = date.add(dtA, interv, -1); |
---|
| 494 | t.is(date.difference(dtA, dtB, interv), -1); |
---|
| 495 | |
---|
| 496 | dtA = new Date(2000, 1, 29); |
---|
| 497 | dtB = date.add(dtA, interv, 1); |
---|
| 498 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 499 | |
---|
| 500 | dtA = new Date(2000, 1, 29); |
---|
| 501 | dtB = date.add(dtA, interv, 5); |
---|
| 502 | t.is(date.difference(dtA, dtB, interv), 5); |
---|
| 503 | |
---|
| 504 | dtA = new Date(1900, 11, 31); |
---|
| 505 | dtB = date.add(dtA, interv, 30); |
---|
| 506 | t.is(date.difference(dtA, dtB, interv), 30); |
---|
| 507 | |
---|
| 508 | dtA = new Date(1995, 11, 31); |
---|
| 509 | dtB = date.add(dtA, interv, 35); |
---|
| 510 | t.is(date.difference(dtA, dtB, interv), 35); |
---|
| 511 | }, |
---|
| 512 | function test_date_add_diff_quarter(t){ |
---|
| 513 | var interv = ''; // Interval (e.g., year, month) |
---|
| 514 | var dtA = null; // Date to increment |
---|
| 515 | var dtB = null; // Expected result date |
---|
| 516 | interv = "quarter"; |
---|
| 517 | dtA = new Date(2000, 0, 1); |
---|
| 518 | dtB = date.add(dtA, interv, 1); |
---|
| 519 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 520 | |
---|
| 521 | dtA = new Date(2000, 1, 29); |
---|
| 522 | dtB = date.add(dtA, interv, 2); |
---|
| 523 | t.is(date.difference(dtA, dtB, interv), 2); |
---|
| 524 | |
---|
| 525 | dtA = new Date(2000, 1, 29); |
---|
| 526 | dtB = date.add(dtA, interv, 4); |
---|
| 527 | t.is(date.difference(dtA, dtB, interv), 4); |
---|
| 528 | }, |
---|
| 529 | function test_date_add_diff_month(t){ |
---|
| 530 | var interv = ''; // Interval (e.g., year, month) |
---|
| 531 | var dtA = null; // Date to increment |
---|
| 532 | var dtB = null; // Expected result date |
---|
| 533 | interv = "month"; |
---|
| 534 | dtA = new Date(2000, 0, 1); |
---|
| 535 | dtB = date.add(dtA, interv, 1); |
---|
| 536 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 537 | |
---|
| 538 | dtA = new Date(2000, 0, 31); |
---|
| 539 | dtB = date.add(dtA, interv, 1); |
---|
| 540 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 541 | |
---|
| 542 | dtA = new Date(2000, 1, 29); |
---|
| 543 | dtB = date.add(dtA, interv, 12); |
---|
| 544 | t.is(date.difference(dtA, dtB, interv), 12); |
---|
| 545 | }, |
---|
| 546 | function test_date_add_diff_week(t){ |
---|
| 547 | var interv = ''; // Interval (e.g., year, month) |
---|
| 548 | var dtA = null; // Date to increment |
---|
| 549 | var dtB = null; // Expected result date |
---|
| 550 | interv = "week"; |
---|
| 551 | dtA = new Date(2000, 0, 1); |
---|
| 552 | dtB = date.add(dtA, interv, 1); |
---|
| 553 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 554 | }, |
---|
| 555 | function test_date_add_diff_day(t){ |
---|
| 556 | var interv = ''; // Interval (e.g., year, month) |
---|
| 557 | var dtA = null; // Date to increment |
---|
| 558 | var dtB = null; // Expected result date |
---|
| 559 | interv = "day"; |
---|
| 560 | dtA = new Date(2000, 0, 1); |
---|
| 561 | dtB = date.add(dtA, interv, 1); |
---|
| 562 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 563 | |
---|
| 564 | dtA = new Date(2001, 0, 1); |
---|
| 565 | dtB = date.add(dtA, interv, 365); |
---|
| 566 | t.is(date.difference(dtA, dtB, interv), 365); |
---|
| 567 | |
---|
| 568 | dtA = new Date(2000, 0, 1); |
---|
| 569 | dtB = date.add(dtA, interv, 366); |
---|
| 570 | t.is(date.difference(dtA, dtB, interv), 366); |
---|
| 571 | |
---|
| 572 | dtA = new Date(2000, 1, 28); |
---|
| 573 | dtB = date.add(dtA, interv, 1); |
---|
| 574 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 575 | |
---|
| 576 | dtA = new Date(2001, 1, 28); |
---|
| 577 | dtB = date.add(dtA, interv, 1); |
---|
| 578 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 579 | |
---|
| 580 | dtA = new Date(2000, 2, 1); |
---|
| 581 | dtB = date.add(dtA, interv, -1); |
---|
| 582 | t.is(date.difference(dtA, dtB, interv), -1); |
---|
| 583 | |
---|
| 584 | dtA = new Date(2001, 2, 1); |
---|
| 585 | dtB = date.add(dtA, interv, -1); |
---|
| 586 | t.is(date.difference(dtA, dtB, interv), -1); |
---|
| 587 | |
---|
| 588 | dtA = new Date(2000, 0, 1); |
---|
| 589 | dtB = date.add(dtA, interv, -1); |
---|
| 590 | t.is(date.difference(dtA, dtB, interv), -1); |
---|
| 591 | }, |
---|
| 592 | function test_date_add_diff_weekday(t){ |
---|
| 593 | var interv = ''; // Interval (e.g., year, month) |
---|
| 594 | var dtA = null; // Date to increment |
---|
| 595 | var dtB = null; // Expected result date |
---|
| 596 | interv = "weekday"; |
---|
| 597 | // Sat, Jan 1 |
---|
| 598 | dtA = new Date(2000, 0, 1); |
---|
| 599 | // Should be Mon, Jan 3 |
---|
| 600 | dtB = date.add(dtA, interv, 1); |
---|
| 601 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 602 | |
---|
| 603 | // Sun, Jan 2 |
---|
| 604 | dtA = new Date(2000, 0, 2); |
---|
| 605 | // Should be Mon, Jan 3 |
---|
| 606 | dtB = date.add(dtA, interv, 1); |
---|
| 607 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 608 | |
---|
| 609 | // Sun, Jan 2 |
---|
| 610 | dtA = new Date(2000, 0, 2); |
---|
| 611 | // Should be Fri, Jan 7 |
---|
| 612 | dtB = date.add(dtA, interv, 5); |
---|
| 613 | t.is(date.difference(dtA, dtB, interv), 5); |
---|
| 614 | |
---|
| 615 | // Sun, Jan 2 |
---|
| 616 | dtA = new Date(2000, 0, 2); |
---|
| 617 | // Should be Mon, Jan 10 |
---|
| 618 | dtB = date.add(dtA, interv, 6); |
---|
| 619 | t.is(date.difference(dtA, dtB, interv), 6); |
---|
| 620 | |
---|
| 621 | // Mon, Jan 3 |
---|
| 622 | dtA = new Date(2000, 0, 3); |
---|
| 623 | // Should be Mon, Jan 17 |
---|
| 624 | dtB = date.add(dtA, interv, 10); |
---|
| 625 | t.is(date.difference(dtA, dtB, interv), 10); |
---|
| 626 | |
---|
| 627 | // Sat, Jan 8 |
---|
| 628 | dtA = new Date(2000, 0, 8); |
---|
| 629 | // Should be Mon, Jan 3 |
---|
| 630 | dtB = date.add(dtA, interv, -5); |
---|
| 631 | t.is(date.difference(dtA, dtB, interv), -5); |
---|
| 632 | |
---|
| 633 | // Sun, Jan 9 |
---|
| 634 | dtA = new Date(2000, 0, 9); |
---|
| 635 | // Should be Wed, Jan 5 |
---|
| 636 | dtB = date.add(dtA, interv, -3); |
---|
| 637 | t.is(date.difference(dtA, dtB, interv), -3); |
---|
| 638 | |
---|
| 639 | // Sun, Jan 23 |
---|
| 640 | dtA = new Date(2000, 0, 23); |
---|
| 641 | // Should be Fri, Jan 7 |
---|
| 642 | dtB = date.add(dtA, interv, -11); |
---|
| 643 | t.is(date.difference(dtA, dtB, interv), -11); |
---|
| 644 | }, |
---|
| 645 | function test_date_add_diff_hour(t){ |
---|
| 646 | var interv = ''; // Interval (e.g., year, month) |
---|
| 647 | var dtA = null; // Date to increment |
---|
| 648 | var dtB = null; // Expected result date |
---|
| 649 | interv = "hour"; |
---|
| 650 | dtA = new Date(2000, 0, 1, 11); |
---|
| 651 | dtB = date.add(dtA, interv, 1); |
---|
| 652 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 653 | |
---|
| 654 | dtA = new Date(2001, 9, 28, 0); |
---|
| 655 | dtB = date.add(dtA, interv, 1); |
---|
| 656 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 657 | |
---|
| 658 | dtA = new Date(2001, 9, 28, 23); |
---|
| 659 | dtB = date.add(dtA, interv, 1); |
---|
| 660 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 661 | |
---|
| 662 | dtA = new Date(2001, 11, 31, 23); |
---|
| 663 | dtB = date.add(dtA, interv, 1); |
---|
| 664 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 665 | }, |
---|
| 666 | function test_date_add_diff_minute(t){ |
---|
| 667 | var interv = ''; // Interval (e.g., year, month) |
---|
| 668 | var dtA = null; // Date to increment |
---|
| 669 | var dtB = null; // Expected result date |
---|
| 670 | interv = "minute"; |
---|
| 671 | dtA = new Date(2000, 11, 31, 23, 59); |
---|
| 672 | dtB = date.add(dtA, interv, 1); |
---|
| 673 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 674 | |
---|
| 675 | dtA = new Date(2000, 11, 27, 12, 2); |
---|
| 676 | dtB = date.add(dtA, interv, 60); |
---|
| 677 | t.is(date.difference(dtA, dtB, interv), 60); |
---|
| 678 | }, |
---|
| 679 | function test_date_add_diff_second(t){ |
---|
| 680 | var interv = ''; // Interval (e.g., year, month) |
---|
| 681 | var dtA = null; // Date to increment |
---|
| 682 | var dtB = null; // Expected result date |
---|
| 683 | console.debug("second"); |
---|
| 684 | interv = "second"; |
---|
| 685 | dtA = new Date(2000, 11, 31, 23, 59, 59); |
---|
| 686 | dtB = date.add(dtA, interv, 1); |
---|
| 687 | t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 688 | |
---|
| 689 | dtA = new Date(2000, 11, 27, 8, 10, 59); |
---|
| 690 | dtB = date.add(dtA, interv, 60); |
---|
| 691 | t.is(date.difference(dtA, dtB, interv), 60); |
---|
| 692 | |
---|
| 693 | // Test environment JS Date doesn't support millisec? |
---|
| 694 | //interv = "millisecond"; |
---|
| 695 | // |
---|
| 696 | //dtA = new Date(2000, 11, 31, 23, 59, 59, 999); |
---|
| 697 | //dtB = date.add(dtA, interv, 1); |
---|
| 698 | //t.is(date.difference(dtA, dtB, interv), 1); |
---|
| 699 | // |
---|
| 700 | //dtA = new Date(2000, 11, 27, 8, 10, 53, 2); |
---|
| 701 | //dtB = date.add(dtA, interv, 1000); |
---|
| 702 | //t.is(date.difference(dtA, dtB, interv), 1000); |
---|
| 703 | } |
---|
| 704 | ] |
---|
| 705 | ); |
---|
| 706 | }); |
---|