Skip to content

Commit f588ee6

Browse files
committed
make date storage tests tz-agnostic
1 parent 12fbd3c commit f588ee6

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

test/Issues/RDBC_236.ts

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ import {
1010
} from "../../src";
1111
import { DateUtil } from "../../src/Utility/DateUtil";
1212
import { GetDocumentsCommand } from "../../src/Documents/Commands/GetDocumentsCommand";
13+
import { StringUtil } from "../../src/Utility/StringUtil";
1314

14-
describe.only("DateUtil", function () {
15+
const LOCAL_TIMEZONE_OFFSET = -(new Date().getTimezoneOffset()); // getTimezoneOffset() returns reversed offset
16+
const LOCAL_TIMEZONE_OFFSET_HOURS = LOCAL_TIMEZONE_OFFSET / 60;
17+
const LOCAL_TIMEZONE_STRING =
18+
// tslint:disable-next-line:max-line-length
19+
`${LOCAL_TIMEZONE_OFFSET >= 0 ? "+" : "-"}${StringUtil.leftPad(LOCAL_TIMEZONE_OFFSET_HOURS.toString(), 2, "0")}:00`;
20+
21+
describe("DateUtil", function () {
1522

1623
describe("without timezones", function () {
1724

@@ -33,9 +40,12 @@ describe.only("DateUtil", function () {
3340
withTimezone: false,
3441
useUtcDates: true
3542
});
36-
const date = moment("2018-10-15T09:46:28.306").toDate();
43+
const date = moment("2018-10-15T12:00:00.000").toDate();
3744
const stringified = dateUtil.stringify(date);
38-
assert.strictEqual(stringified, "2018-10-15T07:46:28.3060000Z");
45+
46+
const expected = new Date(2018, 9, 15, date.getHours() - LOCAL_TIMEZONE_OFFSET_HOURS, 0, 0, 0);
47+
const expectedStringified = moment(expected).format(DateUtil.DEFAULT_DATE_FORMAT) + "Z";
48+
assert.strictEqual(stringified, expectedStringified);
3949

4050
const parsed = dateUtil.parse(stringified);
4151
assert.strictEqual(parsed.getHours(), date.getHours());
@@ -50,11 +60,20 @@ describe.only("DateUtil", function () {
5060
const dateUtil = new DateUtil({
5161
withTimezone: true
5262
});
53-
const date = moment.parseZone("2018-10-15T09:46:28.3060000+06:00").toDate();
54-
assert.strictEqual(date.getHours(), 5);
5563

64+
const hour6 = 12;
65+
const timezoneOffsetHours = 6;
66+
const date = moment.parseZone(`2018-10-15T${hour6}:00:00.0000000+06:00`).toDate();
67+
// preconditions check
68+
assert.strictEqual(
69+
date.getHours(), hour6 - timezoneOffsetHours + LOCAL_TIMEZONE_OFFSET_HOURS);
70+
71+
const expectedHours = date.getHours();
72+
const expected = new Date(2018, 9, 15, expectedHours, 0, 0, 0);
73+
const expectedStringified =
74+
moment(expected).format(DateUtil.DEFAULT_DATE_FORMAT) + LOCAL_TIMEZONE_STRING;
5675
const stringified = dateUtil.stringify(date);
57-
assert.strictEqual(stringified, "2018-10-15T05:46:28.3060000+02:00");
76+
assert.strictEqual(stringified, expectedStringified);
5877

5978
const parsed = dateUtil.parse(stringified);
6079
assert.strictEqual(parsed.getHours(), date.getHours());
@@ -66,11 +85,21 @@ describe.only("DateUtil", function () {
6685
withTimezone: true,
6786
useUtcDates: true
6887
});
69-
const date = moment.parseZone("2018-10-15T09:46:28.3060000+06:00").toDate();
70-
assert.strictEqual(date.getHours(), 5);
7188

89+
const hour6 = 12;
90+
const timezoneOffsetHours = 6;
91+
const date = moment.parseZone(`2018-10-15T${hour6}:00:00.0000000+06:00`).toDate();
92+
// preconditions check
93+
assert.strictEqual(
94+
date.getHours(), hour6 - timezoneOffsetHours + LOCAL_TIMEZONE_OFFSET_HOURS);
95+
96+
const expectedHours = date.getHours() - LOCAL_TIMEZONE_OFFSET_HOURS;
97+
const utcTimezoneString = "+00:00";
98+
const expected = new Date(2018, 9, 15, expectedHours, 0, 0, 0);
99+
const expectedStringified =
100+
moment(expected).format(DateUtil.DEFAULT_DATE_FORMAT) + utcTimezoneString;
72101
const stringified = dateUtil.stringify(date);
73-
assert.strictEqual(stringified, "2018-10-15T03:46:28.3060000+00:00");
102+
assert.strictEqual(stringified, expectedStringified);
74103

75104
const parsed = dateUtil.parse(stringified);
76105
assert.strictEqual(parsed.getHours(), date.getHours());
@@ -85,7 +114,7 @@ describe("[RDBC-236] Dates storage", function () {
85114

86115
let store: IDocumentStore;
87116

88-
describe("store timezone info", function () {
117+
describe("storing timezone info", function () {
89118

90119
beforeEach(async function () {
91120
testContext.customizeStore = async store => {
@@ -102,8 +131,9 @@ describe("[RDBC-236] Dates storage", function () {
102131
afterEach(async () =>
103132
await disposeTestDocumentStore(store));
104133

105-
it("can store & load date with timezone info", async () => {
106-
const date = new Date(2018, 9, 12, 13, 10, 10, 0);
134+
it("can store & load date", async () => {
135+
const hoursLocal = 13;
136+
const date = new Date(2018, 9, 12, hoursLocal, 10, 10, 0);
107137

108138
{
109139
const session = store.openSession();
@@ -122,7 +152,7 @@ describe("[RDBC-236] Dates storage", function () {
122152
});
123153
await store.getRequestExecutor().execute(cmd);
124154
assert.strictEqual(
125-
cmd.result.results[0]["start"], "2018-10-12T13:10:10.0000000+02:00");
155+
cmd.result.results[0]["start"], "2018-10-12T13:10:10.0000000" + LOCAL_TIMEZONE_STRING);
126156
}
127157

128158
{
@@ -154,7 +184,8 @@ describe("[RDBC-236] Dates storage", function () {
154184
await disposeTestDocumentStore(store));
155185

156186
it("can properly store & load date", async () => {
157-
const date = new Date(2018, 9, 12, 13, 10, 10, 0);
187+
const hoursLocal = 13;
188+
const date = new Date(2018, 9, 12, hoursLocal, 10, 10, 0);
158189

159190
{
160191
const session = store.openSession();
@@ -172,8 +203,10 @@ describe("[RDBC-236] Dates storage", function () {
172203
conventions: store.conventions
173204
});
174205
await store.getRequestExecutor().execute(cmd);
206+
const hoursUtcString = StringUtil.leftPad(
207+
(hoursLocal - LOCAL_TIMEZONE_OFFSET_HOURS).toString(), 2, "0");
175208
assert.strictEqual(
176-
cmd.result.results[0]["start"], "2018-10-12T11:10:10.0000000Z");
209+
cmd.result.results[0]["start"], `2018-10-12T${hoursUtcString}:10:10.0000000Z`);
177210
}
178211

179212
{
@@ -205,8 +238,9 @@ describe("[RDBC-236] Dates storage", function () {
205238
afterEach(async () =>
206239
await disposeTestDocumentStore(store));
207240

208-
it("can store & load date with timezone info", async () => {
209-
const date = new Date(2018, 9, 12, 13, 10, 10, 0);
241+
it("can store & load date", async () => {
242+
const hoursLocal = 13;
243+
const date = new Date(2018, 9, 12, hoursLocal, 10, 10, 0);
210244

211245
{
212246
const session = store.openSession();
@@ -224,8 +258,11 @@ describe("[RDBC-236] Dates storage", function () {
224258
conventions: store.conventions
225259
});
226260
await store.getRequestExecutor().execute(cmd);
261+
const hoursUtcString = StringUtil.leftPad(
262+
(hoursLocal - LOCAL_TIMEZONE_OFFSET_HOURS).toString(), 2, "0");
227263
assert.strictEqual(
228-
cmd.result.results[0]["start"], "2018-10-12T11:10:10.0000000+00:00");
264+
cmd.result.results[0]["start"],
265+
`2018-10-12T${hoursUtcString}:10:10.0000000+00:00`);
229266
}
230267

231268
{

0 commit comments

Comments
 (0)