Repository navigation
Expand file tree
/
Copy pathDateTime.js
More file actions
347 lines (347 loc) · 13.9 KB
/
Copy pathDateTime.js
File metadata and controls
347 lines (347 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import { PHPLiteral } from "./PHPVariable.js";
import { SYMBOL_PHP_NAME, SYMBOL_PHP_CONSTANTS, SYMBOL_PHP_NEW } from "./Reflection.js";
export class PHPDateTimeZone {
static [SYMBOL_PHP_NAME] = "DateTimeZone";
static [SYMBOL_PHP_CONSTANTS] = {
africa: 1,
america: 2,
antarctica: 4,
arctic: 8,
asia: 16,
atlantic: 32,
australia: 64,
europe: 128,
indian: 256,
pacific: 512,
utc: 1024,
all: 2047,
all_with_bc: 4095,
per_country: 4096,
};
timezone = "UTC";
constructor(timezone = "UTC") {
this.timezone = timezone;
}
static async [SYMBOL_PHP_NEW](ctx, timezoneArg) {
const obj = Object.create(this.prototype);
obj.timezone = "UTC";
await obj.__construct(ctx, timezoneArg);
return obj;
}
async __construct(ctx, timezoneArg) {
const tz = timezoneArg?.get ? await timezoneArg.get() : timezoneArg;
this.timezone = String(tz ?? "UTC");
}
getName() {
return this.timezone;
}
getname() { return this.getName(); }
getOffset(datetimeArg) {
return 0;
}
getoffset(datetimeArg) { return this.getOffset(datetimeArg); }
}
export class PHPDateTime {
static [SYMBOL_PHP_NAME] = "DateTime";
date;
timezone;
constructor(timeStr = "now", timezone) {
this.date = timeStr === "now" ? new Date() : new Date(Date.parse(timeStr) || Date.now());
this.timezone = timezone;
}
static async [SYMBOL_PHP_NEW](ctx, timeStrArg, timezoneArg) {
const obj = Object.create(this.prototype);
obj.date = new Date();
await obj.__construct(ctx, timeStrArg, timezoneArg);
return obj;
}
async __construct(ctx, timeStrArg, timezoneArg) {
const timeStr = await ctx.toVal(timeStrArg);
const tz = await ctx.toVal(timezoneArg);
this.date = !timeStr || timeStr === "now" ? new Date() : new Date(Date.parse(String(timeStr)) || Date.now());
if (tz instanceof PHPDateTimeZone) {
this.timezone = tz;
}
}
async format(ctxOrFormat, formatArg) {
const fmt = typeof ctxOrFormat === "string" ? ctxOrFormat : String(formatArg?.get ? formatArg.get() : (formatArg || "Y-m-d H:i:s"));
return await DateTimeRuntime.date(null, new PHPLiteral(fmt), new PHPLiteral(Math.floor(this.date.getTime() / 1000)));
}
modify(ctxOrMod, modArg) {
const modStr = String(modArg?.get ? modArg.get() : (modArg || ctxOrMod || ""));
const match = modStr.match(/^([+-]?\d+)\s*(day|days|month|months|year|years|hour|hours|minute|minutes|second|seconds)$/i);
if (match) {
const amount = parseInt(match[1], 10);
const unit = match[2].toLowerCase();
if (unit.startsWith("day"))
this.date.setDate(this.date.getDate() + amount);
else if (unit.startsWith("month"))
this.date.setMonth(this.date.getMonth() + amount);
else if (unit.startsWith("year"))
this.date.setFullYear(this.date.getFullYear() + amount);
else if (unit.startsWith("hour"))
this.date.setHours(this.date.getHours() + amount);
else if (unit.startsWith("minute"))
this.date.setMinutes(this.date.getMinutes() + amount);
else if (unit.startsWith("second"))
this.date.setSeconds(this.date.getSeconds() + amount);
}
return this;
}
setTimezone(ctxOrTz, tzArg) {
const tz = tzArg?.get ? tzArg.get() : (tzArg || ctxOrTz);
if (tz instanceof PHPDateTimeZone) {
this.timezone = tz;
}
return this;
}
settimezone(ctxOrTz, tzArg) { return this.setTimezone(ctxOrTz, tzArg); }
getTimezone() {
return this.timezone || new PHPDateTimeZone("UTC");
}
gettimezone() { return this.getTimezone(); }
getTimestamp() {
return Math.floor(this.date.getTime() / 1000);
}
gettimestamp() { return this.getTimestamp(); }
getOffset() {
if (this.timezone) {
return this.timezone.getOffset(this);
}
return 0;
}
getoffset() { return this.getOffset(); }
static createFromFormat(ctxOrFmt, fmtArg, dtArg, tzArg) {
const fmt = String(fmtArg?.get ? fmtArg.get() : (fmtArg || ctxOrFmt || ""));
const dt = String(dtArg?.get ? dtArg.get() : (dtArg || ""));
const tz = tzArg?.get ? tzArg.get() : tzArg;
const d = new PHPDateTime();
if (tz instanceof PHPDateTimeZone)
d.timezone = tz;
const timestamp = Date.parse(dt);
if (!isNaN(timestamp)) {
d.date = new Date(timestamp);
return d;
}
return d;
}
static createfromformat(ctxOrFmt, fmtArg, dtArg, tzArg) {
return PHPDateTime.createFromFormat(ctxOrFmt, fmtArg, dtArg, tzArg);
}
setTimestamp(ctxOrTs, tsArg) {
const ts = Number(tsArg?.get ? tsArg.get() : (tsArg || ctxOrTs)) || 0;
this.date = new Date(ts * 1000);
return this;
}
settimestamp(ctxOrTs, tsArg) { return this.setTimestamp(ctxOrTs, tsArg); }
}
export class PHPDateTimeImmutable extends PHPDateTime {
static [SYMBOL_PHP_NAME] = "DateTimeImmutable";
modify(ctxOrMod, modArg) {
const clone = Object.create(PHPDateTimeImmutable.prototype);
clone.date = new Date(this.date.getTime());
clone.timezone = this.timezone;
return PHPDateTime.prototype.modify.call(clone, ctxOrMod, modArg);
}
static async createFromMutable(ctxOrDt, dtArg) {
const dt = dtArg?.get ? dtArg.get() : (dtArg || ctxOrDt);
const imm = Object.create(this.prototype);
imm.date = dt instanceof PHPDateTime ? new Date(dt.date.getTime()) : new Date();
imm.timezone = dt instanceof PHPDateTime ? dt.timezone : undefined;
return imm;
}
static async createfrommutable(ctxOrDt, dtArg) {
return PHPDateTimeImmutable.createFromMutable(ctxOrDt, dtArg);
}
static createFromFormat(ctxOrFmt, fmtArg, dtArg, tzArg) {
const dt = PHPDateTime.createFromFormat(ctxOrFmt, fmtArg, dtArg, tzArg);
if (!dt)
return false;
const imm = Object.create(PHPDateTimeImmutable.prototype);
imm.date = new Date(dt.date.getTime());
imm.timezone = dt.timezone;
return imm;
}
static createfromformat(ctxOrFmt, fmtArg, dtArg, tzArg) {
return PHPDateTimeImmutable.createFromFormat(ctxOrFmt, fmtArg, dtArg, tzArg);
}
}
export class DateTimeRuntime {
static defaultTimezone = "UTC";
static async checkdate(ctx, monthArg, dayArg, yearArg) {
const month = Number(await (monthArg?.get !== undefined ? monthArg.get() : monthArg));
const day = Number(await (dayArg?.get !== undefined ? dayArg.get() : dayArg));
const year = Number(await (yearArg?.get !== undefined ? yearArg.get() : yearArg));
if (!Number.isInteger(month) || !Number.isInteger(day) || !Number.isInteger(year))
return false;
if (year < 1 || year > 32767 || month < 1 || month > 12 || day < 1)
return false;
const daysInMonth = [31, (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
return day <= daysInMonth[month - 1];
}
static async time(ctx) {
return Math.floor(Date.now() / 1000);
}
static async microtime(ctx, getAsFloatArg) {
const getAsFloat = Boolean(await getAsFloatArg?.get());
const now = Date.now();
const sec = Math.floor(now / 1000);
const msec = (now % 1000) / 1000;
if (getAsFloat)
return sec + msec;
return `${msec.toFixed(8)} ${sec}`;
}
static async date(ctx, formatArg, timestampArg) {
const format = String(await formatArg?.get() ?? "");
const timestampValue = await timestampArg?.get();
const timestamp = timestampValue !== undefined ? Number(timestampValue) : undefined;
const d = timestamp !== undefined ? new Date(timestamp * 1000) : new Date();
let res = "";
for (let i = 0; i < format.length; i++) {
const ch = format[i];
switch (ch) {
case "Y":
res += d.getFullYear();
break;
case "m":
res += String(d.getMonth() + 1).padStart(2, "0");
break;
case "d":
res += String(d.getDate()).padStart(2, "0");
break;
case "H":
res += String(d.getHours()).padStart(2, "0");
break;
case "i":
res += String(d.getMinutes()).padStart(2, "0");
break;
case "s":
res += String(d.getSeconds()).padStart(2, "0");
break;
case "U":
res += Math.floor(d.getTime() / 1000);
break;
default:
res += ch;
break;
}
}
return res;
}
static async strtotime(ctx, timeStrArg, nowArg) {
try {
const timeStr = String(await timeStrArg?.get() ?? "");
const nowValue = await nowArg?.get();
const now = nowValue !== undefined ? Number(nowValue) : undefined;
const base = now !== undefined ? new Date(now * 1000) : new Date();
const parsed = Date.parse(timeStr);
if (!Number.isNaN(parsed))
return Math.floor(parsed / 1000);
return Math.floor(base.getTime() / 1000);
}
catch {
return false;
}
}
static async date_default_timezone_get(ctx) {
return DateTimeRuntime.defaultTimezone;
}
static async date_default_timezone_set(ctx, timezoneIdArg) {
const timezoneId = String(await timezoneIdArg?.get() ?? "");
DateTimeRuntime.defaultTimezone = timezoneId;
return true;
}
static async timezone_identifiers_list(ctx, timezoneGroupArg, countryCodeArg) {
return Object.keys(PHPDateTimeZone[SYMBOL_PHP_CONSTANTS]).map(() => "UTC");
}
static async gmdate(ctx, formatArg, timestampArg) {
const format = String(await formatArg?.get() ?? "");
const timestampValue = await timestampArg?.get();
const timestamp = timestampValue !== undefined ? Number(timestampValue) : Math.floor(Date.now() / 1000);
const d = new Date(timestamp * 1000);
let res = "";
for (let i = 0; i < format.length; i++) {
const ch = format[i];
switch (ch) {
case "Y":
res += d.getUTCFullYear();
break;
case "m":
res += String(d.getUTCMonth() + 1).padStart(2, "0");
break;
case "d":
res += String(d.getUTCDate()).padStart(2, "0");
break;
case "H":
res += String(d.getUTCHours()).padStart(2, "0");
break;
case "i":
res += String(d.getUTCMinutes()).padStart(2, "0");
break;
case "s":
res += String(d.getUTCSeconds()).padStart(2, "0");
break;
case "U":
res += Math.floor(d.getTime() / 1000);
break;
default:
res += ch;
break;
}
}
return res;
}
static async date_create(ctx, datetimeArg, timezoneArg) {
const dtStr = await ctx.toVal(datetimeArg);
const tz = await ctx.toVal(timezoneArg);
const d = new PHPDateTime();
if (tz instanceof PHPDateTimeZone)
d.timezone = tz;
const str = await ctx.str(dtStr || "now");
const ts = Date.parse(str === "now" ? new Date().toISOString() : str);
if (!isNaN(ts)) {
d.date = new Date(ts);
}
return d;
}
static async date_create_immutable(ctx, datetimeArg, timezoneArg) {
const dt = await DateTimeRuntime.date_create(ctx, datetimeArg, timezoneArg);
if (!dt)
return false;
const imm = Object.create(PHPDateTimeImmutable.prototype);
imm.date = new Date(dt.date.getTime());
imm.timezone = dt.timezone;
return imm;
}
static async date_create_from_format(ctx, formatArg, datetimeArg, timezoneArg) {
return PHPDateTime.createFromFormat(ctx, formatArg, datetimeArg, timezoneArg);
}
static async date_create_immutable_from_format(ctx, formatArg, datetimeArg, timezoneArg) {
return PHPDateTimeImmutable.createFromFormat(ctx, formatArg, datetimeArg, timezoneArg);
}
static functions = {
"checkdate": DateTimeRuntime.checkdate,
"time": DateTimeRuntime.time,
"microtime": DateTimeRuntime.microtime,
"date": DateTimeRuntime.date,
"gmdate": DateTimeRuntime.gmdate,
"strtotime": DateTimeRuntime.strtotime,
"date_create": DateTimeRuntime.date_create,
"date_create_immutable": DateTimeRuntime.date_create_immutable,
"date_create_from_format": DateTimeRuntime.date_create_from_format,
"date_create_immutable_from_format": DateTimeRuntime.date_create_immutable_from_format,
"date_default_timezone_get": DateTimeRuntime.date_default_timezone_get,
"date_default_timezone_set": DateTimeRuntime.date_default_timezone_set,
"timezone_identifiers_list": DateTimeRuntime.timezone_identifiers_list,
};
static classes = {
"datetime": PHPDateTime,
"datetimezone": PHPDateTimeZone,
"datetimeimmutable": PHPDateTimeImmutable,
};
static register(engine) {
engine.registerFunctions(DateTimeRuntime.functions);
engine.registerClasses(DateTimeRuntime.classes);
}
}
//# sourceMappingURL=DateTime.js.map