@lbd-sh/date-tz 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,213 @@
1
+ import { DateTz } from './date-tz';
2
+
3
+ describe('DateTz', () => {
4
+ it('should create an instance with the correct date and timezone', () => {
5
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
6
+ expect(dateTz.datetime).toBe(1609459200000);
7
+ expect(dateTz.timezone).toBe(0);
8
+ });
9
+
10
+ it('should compare two DateTz instances with the same timezone', () => {
11
+ const dateTz1 = new DateTz(1609459200000, 0);
12
+ const dateTz2 = new DateTz(1609545600000, 0); // 2021-01-02 00:00:00 UTC
13
+ expect(dateTz1.compare(dateTz2)).toBeLessThan(0);
14
+ });
15
+
16
+ it('should throw an error when comparing two DateTz instances with different timezones', () => {
17
+ const dateTz1 = new DateTz(1609459200000, 0);
18
+ const dateTz2 = new DateTz(1609459200000, 1);
19
+ expect(() => dateTz1.compare(dateTz2)).toThrow('Cannot compare dates with different timezones');
20
+ });
21
+
22
+ it('should format the date correctly with the default pattern', () => {
23
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
24
+ expect(dateTz.toString()).toBe('2021-01-01 00:00:00');
25
+ });
26
+
27
+ it('should format the date correctly with a custom pattern', () => {
28
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
29
+ expect(dateTz.toString('DD/MM/YYYY')).toBe('01/01/2021');
30
+ });
31
+
32
+ it('should add minutes correctly', () => {
33
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
34
+ dateTz.add(30, 'minute');
35
+ expect(dateTz.toString()).toBe('2021-01-01 00:30:00');
36
+ });
37
+
38
+ it('should add hours correctly', () => {
39
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
40
+ dateTz.add(2, 'hour');
41
+ expect(dateTz.toString()).toBe('2021-01-01 02:00:00');
42
+ });
43
+
44
+ it('should add days correctly', () => {
45
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
46
+ dateTz.add(1, 'day');
47
+ expect(dateTz.toString()).toBe('2021-01-02 00:00:00');
48
+ });
49
+
50
+ it('should add months correctly', () => {
51
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
52
+ dateTz.add(1, 'month');
53
+ expect(dateTz.toString()).toBe('2021-02-01 00:00:00');
54
+ });
55
+
56
+ it('should add years correctly', () => {
57
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
58
+ dateTz.add(1, 'year');
59
+ expect(dateTz.toString()).toBe('2022-01-01 00:00:00');
60
+ });
61
+
62
+ it('should return the correct year', () => {
63
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
64
+ expect(dateTz.year).toBe(2021);
65
+ });
66
+
67
+ it('should return the correct month', () => {
68
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
69
+ expect(dateTz.month).toBe(0); // January
70
+ });
71
+
72
+ it('should return the correct day', () => {
73
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
74
+ expect(dateTz.day).toBe(1);
75
+ });
76
+
77
+ it('should return the correct hour', () => {
78
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
79
+ expect(dateTz.hour).toBe(0);
80
+ });
81
+
82
+ it('should return the correct minute', () => {
83
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
84
+ expect(dateTz.minute).toBe(0);
85
+ });
86
+
87
+ it('should create an instance with the correct date and timezone', () => {
88
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
89
+ expect(dateTz.datetime).toBe(1609459200000);
90
+ expect(dateTz.timezone).toBe(0);
91
+ });
92
+
93
+ it('should compare two DateTz instances with the same timezone', () => {
94
+ const dateTz1 = new DateTz(1609459200000, 0);
95
+ const dateTz2 = new DateTz(1609545600000, 0); // 2021-01-02 00:00:00 UTC
96
+ expect(dateTz1.compare(dateTz2)).toBeLessThan(0);
97
+ });
98
+
99
+ it('should throw an error when comparing two DateTz instances with different timezones', () => {
100
+ const dateTz1 = new DateTz(1609459200000, 0);
101
+ const dateTz2 = new DateTz(1609459200000, 1);
102
+ expect(() => dateTz1.compare(dateTz2)).toThrow('Cannot compare dates with different timezones');
103
+ });
104
+
105
+ it('should format the date correctly with the default pattern', () => {
106
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
107
+ expect(dateTz.toString()).toBe('2021-01-01 00:00:00');
108
+ });
109
+
110
+ it('should format the date correctly with a custom pattern', () => {
111
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
112
+ expect(dateTz.toString('DD/MM/YYYY')).toBe('01/01/2021');
113
+ });
114
+
115
+ it('should add minutes correctly', () => {
116
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
117
+ dateTz.add(30, 'minute');
118
+ expect(dateTz.toString()).toBe('2021-01-01 00:30:00');
119
+ });
120
+
121
+ it('should add hours correctly', () => {
122
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
123
+ dateTz.add(2, 'hour');
124
+ expect(dateTz.toString()).toBe('2021-01-01 02:00:00');
125
+ });
126
+
127
+ it('should add days correctly', () => {
128
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
129
+ dateTz.add(1, 'day');
130
+ expect(dateTz.toString()).toBe('2021-01-02 00:00:00');
131
+ });
132
+
133
+ it('should add months correctly', () => {
134
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
135
+ dateTz.add(1, 'month');
136
+ expect(dateTz.toString()).toBe('2021-02-01 00:00:00');
137
+ });
138
+
139
+ it('should add years correctly', () => {
140
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
141
+ dateTz.add(1, 'year');
142
+ expect(dateTz.toString()).toBe('2022-01-01 00:00:00');
143
+ });
144
+
145
+ it('should return the correct year', () => {
146
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
147
+ expect(dateTz.year).toBe(2021);
148
+ });
149
+
150
+ it('should return the correct month', () => {
151
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
152
+ expect(dateTz.month).toBe(0); // January
153
+ });
154
+
155
+ it('should return the correct day', () => {
156
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
157
+ expect(dateTz.day).toBe(1);
158
+ });
159
+
160
+ it('should return the correct hour', () => {
161
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
162
+ expect(dateTz.hour).toBe(0);
163
+ });
164
+
165
+ it('should return the correct minute', () => {
166
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
167
+ expect(dateTz.minute).toBe(0);
168
+ });
169
+
170
+ it('should parse a date string correctly', () => {
171
+ const dateTz = DateTz.parse('2021--01-01 00:00:00', 'YYYY--MM-DD HH:mm', 0);
172
+ expect(dateTz.datetime).toBe(1609459200000);
173
+ expect(dateTz.timezone).toBe(0);
174
+ });
175
+
176
+ it('should set the year correctly', () => {
177
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
178
+ dateTz.set(2022, 'year');
179
+ expect(dateTz.toString()).toBe('2022-01-01 00:00:00');
180
+ });
181
+
182
+ it('should set the month correctly', () => {
183
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
184
+ dateTz.set(2, 'month');
185
+ expect(dateTz.toString()).toBe('2021-02-01 00:00:00');
186
+ });
187
+
188
+ it('should set the day correctly', () => {
189
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
190
+ dateTz.set(2, 'day');
191
+ expect(dateTz.toString()).toBe('2021-01-02 00:00:00');
192
+ });
193
+
194
+ it('should set the hour correctly', () => {
195
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
196
+ dateTz.set(2, 'hour');
197
+ expect(dateTz.toString()).toBe('2021-01-01 02:00:00');
198
+ });
199
+
200
+ it('should set the minute correctly', () => {
201
+ const dateTz = new DateTz(1609459200000, 0); // 2021-01-01 00:00:00 UTC
202
+ dateTz.set(30, 'minute');
203
+ expect(dateTz.toString()).toBe('2021-01-01 00:30:00');
204
+ });
205
+
206
+ it('should convert timezone name to offset correctly', () => {
207
+ expect(DateTz.tzNameToOffset('PST')).toBe(-8);
208
+ });
209
+
210
+ it('should convert timezone offset to name correctly', () => {
211
+ expect(DateTz.tzOffsetToName(-8)).toBe('PST');
212
+ });
213
+ });