|
| 1 | +var vows = require('vows'); |
| 2 | +var assert = require('assert'); |
| 3 | + |
| 4 | +require('../lib/date-utils.js'); |
| 5 | +//getTimezoneOffset returns number of minutes |
| 6 | +//converting to milliseconds |
| 7 | +var offset = (new Date().getTimezoneOffset()) * 60 * 1000; |
| 8 | + |
| 9 | +vows.describe('Date Parse').addBatch({ |
| 10 | + 'can instantiate milliseconds': { |
| 11 | + topic: function () { return new Date(123456789); }, |
| 12 | + 'returns a valid date object': function (date) { |
| 13 | + assert.ok(date); |
| 14 | + }, |
| 15 | + 'returns a correct value': function (date) { |
| 16 | + assert.equal(date.valueOf(), 123456789); |
| 17 | + } |
| 18 | + }, |
| 19 | + |
| 20 | + 'can instantiate string': { |
| 21 | + topic: function () { return new Date('Jan 1, 2011 01:01:01 GMT'); }, |
| 22 | + 'returns a valid date object': function (date) { |
| 23 | + assert.ok(date); |
| 24 | + }, |
| 25 | + 'returns a correct value': function (date) { |
| 26 | + assert.equal(date.valueOf(), 1293843661000); |
| 27 | + } |
| 28 | + }, |
| 29 | + |
| 30 | + 'can instantiate arguments': { |
| 31 | + topic: function () { return new Date(2011, 1, 1, 1, 1, 1, 0); }, |
| 32 | + 'returns a valid date object': function (date) { |
| 33 | + assert.ok(date); |
| 34 | + } |
| 35 | + }, |
| 36 | + |
| 37 | + 'can parse normal date': { |
| 38 | + topic: function () { return Date.parse('Jan 1, 2011 01:01:01 GMT'); }, |
| 39 | + 'returns a correct value': function (milli) { |
| 40 | + assert.equal(milli, 1293843661000); |
| 41 | + } |
| 42 | + }, |
| 43 | + 'can parse ISO-8601': { |
| 44 | + topic: function () { return Date.parse('2011-01-01T01:01:01Z'); }, |
| 45 | + 'returns a correct value': function (milli) { |
| 46 | + assert.equal(milli, 1293843661000); |
| 47 | + } |
| 48 | + }, |
| 49 | + 'can parse custom format': { |
| 50 | + topic: function () { |
| 51 | + return Date.parse('20/6/2011 8:30', 'd/M/y H:m'); |
| 52 | + }, |
| 53 | + 'returns a correct value': function (milli) { |
| 54 | + assert.equal(milli, 1308558600000 + offset); |
| 55 | + } |
| 56 | + }, |
| 57 | + 'parse custom format with full month name': { |
| 58 | + topic: function () { |
| 59 | + return Date.parse('June 20, 2011 08:30:00', 'MMM d, y H:m:s'); |
| 60 | + }, |
| 61 | + 'returns a correct value': function (milli) { |
| 62 | + assert.equal(milli, 1308558600000 + offset); |
| 63 | + } |
| 64 | + }, |
| 65 | + 'parse custom format with abbr month name': { |
| 66 | + topic: function () { |
| 67 | + return Date.parse('Jun 20, 2011 08:30:00', 'MMM d, y H:m:s'); |
| 68 | + }, |
| 69 | + 'returns a correct value': function (milli) { |
| 70 | + assert.equal(milli, 1308558600000 + offset); |
| 71 | + } |
| 72 | + }, |
| 73 | + 'parse custom format with 12 hr clock': { |
| 74 | + topic: function () { |
| 75 | + return Date.parse('June 20, 2011 08:30:00AM', 'MMM d, y h:m:sa'); |
| 76 | + }, |
| 77 | + 'returns a correct value': function (milli) { |
| 78 | + assert.equal(milli, 1308558600000 + offset); |
| 79 | + } |
| 80 | + }, |
| 81 | + 'parse mysql date format': { |
| 82 | + topic: function () { |
| 83 | + return Date.parse('2011-06-20 08:30:00', 'y-M-d H:m:s'); |
| 84 | + }, |
| 85 | + 'returns a correct value': function (milli) { |
| 86 | + assert.equal(milli, 1308558600000 + offset); |
| 87 | + } |
| 88 | + }, |
| 89 | + 'parse us date format w/o time': { |
| 90 | + topic: function () { |
| 91 | + return Date.parse('6/20/2011', 'M/d/y'); |
| 92 | + }, |
| 93 | + 'returns a correct value': function (milli) { |
| 94 | + assert.equal(milli, 1308528000000 + offset); |
| 95 | + } |
| 96 | + }, |
| 97 | + 'parse us date format with time': { |
| 98 | + topic: function () { |
| 99 | + return Date.parse('6/20/2011 00:00:01', 'M/d/y H:m:s'); |
| 100 | + }, |
| 101 | + 'returns a correct value': function (milli) { |
| 102 | + assert.equal(milli, 1308528001000 + offset); |
| 103 | + } |
| 104 | + }, |
| 105 | + 'parse uk date format w/o time': { |
| 106 | + topic: function () { |
| 107 | + return Date.parse('20/6/2011', 'd/M/y'); |
| 108 | + }, |
| 109 | + 'returns a correct value': function (milli) { |
| 110 | + assert.equal(milli, 1308528000000 + offset); |
| 111 | + } |
| 112 | + }, |
| 113 | + 'parse uk date format with time': { |
| 114 | + topic: function () { |
| 115 | + |
| 116 | + return Date.parse('20/6/2011 00:00:01', 'd/M/y H:m:s'); |
| 117 | + }, |
| 118 | + 'returns a correct value': function (milli) { |
| 119 | + assert.equal(milli, 1308528001000 + offset); |
| 120 | + } |
| 121 | + } |
| 122 | +}).export(module); |
0 commit comments