|
| 1 | +import { hydrateDate } from "./hydrateDate"; |
| 2 | + |
| 3 | +describe("hydrate Date", () => { |
| 4 | + it("hydrate date type", () => { |
| 5 | + const dates = ["2006-01-02", "2006-1-02", "2006-01-2", "2006-1-2"]; |
| 6 | + for (const input of dates) { |
| 7 | + const date = hydrateDate(input); |
| 8 | + |
| 9 | + const year = date?.getUTCFullYear(); |
| 10 | + const month = date?.getUTCMonth(); |
| 11 | + const day = date?.getUTCDate(); |
| 12 | + |
| 13 | + expect(year).toEqual(2006); |
| 14 | + expect(month).toEqual(0); |
| 15 | + expect(day).toEqual(2); |
| 16 | + } |
| 17 | + }); |
| 18 | + it("hydrate pg_date type", () => { |
| 19 | + const dates = ["2023-2-13", "2023-02-13"]; |
| 20 | + for (const input of dates) { |
| 21 | + const date = hydrateDate(input); |
| 22 | + |
| 23 | + const year = date?.getUTCFullYear(); |
| 24 | + const month = date?.getUTCMonth(); |
| 25 | + const day = date?.getUTCDate(); |
| 26 | + |
| 27 | + expect(year).toEqual(2023); |
| 28 | + expect(month).toEqual(1); |
| 29 | + expect(day).toEqual(13); |
| 30 | + } |
| 31 | + }); |
| 32 | + it("hydrate timestamp type", () => { |
| 33 | + const input = "2033-02-13 12:00:10"; |
| 34 | + const date = hydrateDate(input); |
| 35 | + |
| 36 | + const year = date?.getUTCFullYear(); |
| 37 | + const month = date?.getUTCMonth(); |
| 38 | + const day = date?.getUTCDate(); |
| 39 | + const hour = date?.getUTCHours(); |
| 40 | + const minute = date?.getUTCMinutes(); |
| 41 | + const second = date?.getUTCSeconds(); |
| 42 | + |
| 43 | + expect(year).toEqual(2033); |
| 44 | + expect(month).toEqual(1); |
| 45 | + expect(day).toEqual(13); |
| 46 | + expect(hour).toEqual(12); |
| 47 | + expect(minute).toEqual(0); |
| 48 | + expect(second).toEqual(10); |
| 49 | + }); |
| 50 | + it("hydrate timestamptz type", () => { |
| 51 | + const input = "2033-02-13 12:00:10+01:00"; |
| 52 | + const date = hydrateDate(input); |
| 53 | + |
| 54 | + const year = date?.getUTCFullYear(); |
| 55 | + const month = date?.getUTCMonth(); |
| 56 | + const day = date?.getUTCDate(); |
| 57 | + const hour = date?.getUTCHours(); |
| 58 | + const minute = date?.getUTCMinutes(); |
| 59 | + const second = date?.getUTCSeconds(); |
| 60 | + |
| 61 | + expect(year).toEqual(2033); |
| 62 | + expect(month).toEqual(1); |
| 63 | + expect(day).toEqual(13); |
| 64 | + expect(hour).toEqual(11); |
| 65 | + expect(minute).toEqual(0); |
| 66 | + expect(second).toEqual(10); |
| 67 | + }); |
| 68 | + it("hydrate timestamptz type 2", () => { |
| 69 | + const input = "2033-02-13 12:00:10-01:00"; |
| 70 | + const date = hydrateDate(input); |
| 71 | + |
| 72 | + const year = date?.getUTCFullYear(); |
| 73 | + const month = date?.getUTCMonth(); |
| 74 | + const day = date?.getUTCDate(); |
| 75 | + const hour = date?.getUTCHours(); |
| 76 | + const minute = date?.getUTCMinutes(); |
| 77 | + const second = date?.getUTCSeconds(); |
| 78 | + |
| 79 | + expect(year).toEqual(2033); |
| 80 | + expect(month).toEqual(1); |
| 81 | + expect(day).toEqual(13); |
| 82 | + expect(hour).toEqual(13); |
| 83 | + expect(minute).toEqual(0); |
| 84 | + expect(second).toEqual(10); |
| 85 | + }); |
| 86 | + it("hydrate timestamptz type 3", () => { |
| 87 | + const input = "2033-02-13 12:00:10-02:30"; |
| 88 | + const date = hydrateDate(input); |
| 89 | + |
| 90 | + const year = date?.getUTCFullYear(); |
| 91 | + const month = date?.getUTCMonth(); |
| 92 | + const day = date?.getUTCDate(); |
| 93 | + const hour = date?.getUTCHours(); |
| 94 | + const minute = date?.getUTCMinutes(); |
| 95 | + const second = date?.getUTCSeconds(); |
| 96 | + |
| 97 | + expect(year).toEqual(2033); |
| 98 | + expect(month).toEqual(1); |
| 99 | + expect(day).toEqual(13); |
| 100 | + expect(hour).toEqual(14); |
| 101 | + expect(minute).toEqual(30); |
| 102 | + expect(second).toEqual(10); |
| 103 | + }); |
| 104 | + it("hydrate timestamptz type 4", () => { |
| 105 | + const input = "2033-02-13T12:00:10-02:30"; |
| 106 | + const date = hydrateDate(input); |
| 107 | + |
| 108 | + const year = date?.getUTCFullYear(); |
| 109 | + const month = date?.getUTCMonth(); |
| 110 | + const day = date?.getUTCDate(); |
| 111 | + const hour = date?.getUTCHours(); |
| 112 | + const minute = date?.getUTCMinutes(); |
| 113 | + const second = date?.getUTCSeconds(); |
| 114 | + |
| 115 | + expect(year).toEqual(2033); |
| 116 | + expect(month).toEqual(1); |
| 117 | + expect(day).toEqual(13); |
| 118 | + expect(hour).toEqual(14); |
| 119 | + expect(minute).toEqual(30); |
| 120 | + expect(second).toEqual(10); |
| 121 | + }); |
| 122 | + it("hydrate timestamptz padded time", () => { |
| 123 | + const input = "2033-02-13 2:15:10-1:00"; |
| 124 | + const date = hydrateDate(input); |
| 125 | + |
| 126 | + const year = date?.getUTCFullYear(); |
| 127 | + const month = date?.getUTCMonth(); |
| 128 | + const day = date?.getUTCDate(); |
| 129 | + const hour = date?.getUTCHours(); |
| 130 | + const minute = date?.getUTCMinutes(); |
| 131 | + const second = date?.getUTCSeconds(); |
| 132 | + |
| 133 | + expect(year).toEqual(2033); |
| 134 | + expect(month).toEqual(1); |
| 135 | + expect(day).toEqual(13); |
| 136 | + expect(hour).toEqual(3); |
| 137 | + expect(minute).toEqual(15); |
| 138 | + expect(second).toEqual(10); |
| 139 | + }); |
| 140 | + it("hydrate timestampntz type", () => { |
| 141 | + const input = "2033-02-13T12:00:10"; |
| 142 | + const date = hydrateDate(input); |
| 143 | + |
| 144 | + const year = date?.getUTCFullYear(); |
| 145 | + const month = date?.getUTCMonth(); |
| 146 | + const day = date?.getUTCDate(); |
| 147 | + const hour = date?.getUTCHours(); |
| 148 | + const minute = date?.getUTCMinutes(); |
| 149 | + const second = date?.getUTCSeconds(); |
| 150 | + |
| 151 | + expect(year).toEqual(2033); |
| 152 | + expect(month).toEqual(1); |
| 153 | + expect(day).toEqual(13); |
| 154 | + expect(hour).toEqual(12); |
| 155 | + expect(minute).toEqual(0); |
| 156 | + expect(second).toEqual(10); |
| 157 | + }); |
| 158 | + it("handle old dates", () => { |
| 159 | + const input = "0033-01-01 12:00:00"; |
| 160 | + const date = hydrateDate(input); |
| 161 | + |
| 162 | + const year = date?.getUTCFullYear(); |
| 163 | + const month = date?.getUTCMonth(); |
| 164 | + const day = date?.getUTCDate(); |
| 165 | + const hour = date?.getUTCHours(); |
| 166 | + const minute = date?.getUTCMinutes(); |
| 167 | + const second = date?.getUTCSeconds(); |
| 168 | + |
| 169 | + expect(year).toEqual(33); |
| 170 | + expect(month).toEqual(0); |
| 171 | + expect(day).toEqual(1); |
| 172 | + expect(hour).toEqual(12); |
| 173 | + expect(minute).toEqual(0); |
| 174 | + expect(second).toEqual(0); |
| 175 | + }); |
| 176 | +}); |
0 commit comments