@elaraai/east 0.0.1-beta.0
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.
- package/LICENSE.md +682 -0
- package/README.md +276 -0
- package/dist/src/analyze.d.ts +95 -0
- package/dist/src/analyze.d.ts.map +1 -0
- package/dist/src/analyze.js +1110 -0
- package/dist/src/analyze.js.map +1 -0
- package/dist/src/ast.d.ts +263 -0
- package/dist/src/ast.d.ts.map +1 -0
- package/dist/src/ast.js +151 -0
- package/dist/src/ast.js.map +1 -0
- package/dist/src/ast_to_ir.d.ts +24 -0
- package/dist/src/ast_to_ir.d.ts.map +1 -0
- package/dist/src/ast_to_ir.js +834 -0
- package/dist/src/ast_to_ir.js.map +1 -0
- package/dist/src/builtins.d.ts +18 -0
- package/dist/src/builtins.d.ts.map +1 -0
- package/dist/src/builtins.js +1105 -0
- package/dist/src/builtins.js.map +1 -0
- package/dist/src/comparison.d.ts +28 -0
- package/dist/src/comparison.d.ts.map +1 -0
- package/dist/src/comparison.js +1017 -0
- package/dist/src/comparison.js.map +1 -0
- package/dist/src/compile.d.ts +22 -0
- package/dist/src/compile.d.ts.map +1 -0
- package/dist/src/compile.js +3260 -0
- package/dist/src/compile.js.map +1 -0
- package/dist/src/containers/ref.d.ts +106 -0
- package/dist/src/containers/ref.d.ts.map +1 -0
- package/dist/src/containers/ref.js +100 -0
- package/dist/src/containers/ref.js.map +1 -0
- package/dist/src/containers/sortedmap.d.ts +165 -0
- package/dist/src/containers/sortedmap.d.ts.map +1 -0
- package/dist/src/containers/sortedmap.js +237 -0
- package/dist/src/containers/sortedmap.js.map +1 -0
- package/dist/src/containers/sortedset.d.ts +185 -0
- package/dist/src/containers/sortedset.d.ts.map +1 -0
- package/dist/src/containers/sortedset.js +312 -0
- package/dist/src/containers/sortedset.js.map +1 -0
- package/dist/src/containers/variant.d.ts +131 -0
- package/dist/src/containers/variant.d.ts.map +1 -0
- package/dist/src/containers/variant.js +68 -0
- package/dist/src/containers/variant.js.map +1 -0
- package/dist/src/datetime_format/parse.d.ts +50 -0
- package/dist/src/datetime_format/parse.d.ts.map +1 -0
- package/dist/src/datetime_format/parse.js +908 -0
- package/dist/src/datetime_format/parse.js.map +1 -0
- package/dist/src/datetime_format/print.d.ts +35 -0
- package/dist/src/datetime_format/print.d.ts.map +1 -0
- package/dist/src/datetime_format/print.js +157 -0
- package/dist/src/datetime_format/print.js.map +1 -0
- package/dist/src/datetime_format/tokenize.d.ts +76 -0
- package/dist/src/datetime_format/tokenize.d.ts.map +1 -0
- package/dist/src/datetime_format/tokenize.js +271 -0
- package/dist/src/datetime_format/tokenize.js.map +1 -0
- package/dist/src/datetime_format/types.d.ts +99 -0
- package/dist/src/datetime_format/types.d.ts.map +1 -0
- package/dist/src/datetime_format/types.js +103 -0
- package/dist/src/datetime_format/types.js.map +1 -0
- package/dist/src/datetime_format/validate.d.ts +51 -0
- package/dist/src/datetime_format/validate.d.ts.map +1 -0
- package/dist/src/datetime_format/validate.js +208 -0
- package/dist/src/datetime_format/validate.js.map +1 -0
- package/dist/src/default.d.ts +21 -0
- package/dist/src/default.d.ts.map +1 -0
- package/dist/src/default.js +82 -0
- package/dist/src/default.js.map +1 -0
- package/dist/src/eastir.d.ts +33 -0
- package/dist/src/eastir.d.ts.map +1 -0
- package/dist/src/eastir.js +92 -0
- package/dist/src/eastir.js.map +1 -0
- package/dist/src/error.d.ts +13 -0
- package/dist/src/error.d.ts.map +1 -0
- package/dist/src/error.js +8 -0
- package/dist/src/error.js.map +1 -0
- package/dist/src/expr/array.d.ts +1711 -0
- package/dist/src/expr/array.d.ts.map +1 -0
- package/dist/src/expr/array.js +1805 -0
- package/dist/src/expr/array.js.map +1 -0
- package/dist/src/expr/ast.d.ts +17 -0
- package/dist/src/expr/ast.d.ts.map +1 -0
- package/dist/src/expr/ast.js +302 -0
- package/dist/src/expr/ast.js.map +1 -0
- package/dist/src/expr/blob.d.ts +141 -0
- package/dist/src/expr/blob.d.ts.map +1 -0
- package/dist/src/expr/blob.js +198 -0
- package/dist/src/expr/blob.js.map +1 -0
- package/dist/src/expr/block.d.ts +201 -0
- package/dist/src/expr/block.d.ts.map +1 -0
- package/dist/src/expr/block.js +1505 -0
- package/dist/src/expr/block.js.map +1 -0
- package/dist/src/expr/boolean.d.ts +207 -0
- package/dist/src/expr/boolean.d.ts.map +1 -0
- package/dist/src/expr/boolean.js +261 -0
- package/dist/src/expr/boolean.js.map +1 -0
- package/dist/src/expr/datetime.d.ts +544 -0
- package/dist/src/expr/datetime.d.ts.map +1 -0
- package/dist/src/expr/datetime.js +980 -0
- package/dist/src/expr/datetime.js.map +1 -0
- package/dist/src/expr/dict.d.ts +1242 -0
- package/dist/src/expr/dict.d.ts.map +1 -0
- package/dist/src/expr/dict.js +1492 -0
- package/dist/src/expr/dict.js.map +1 -0
- package/dist/src/expr/expr.d.ts +95 -0
- package/dist/src/expr/expr.d.ts.map +1 -0
- package/dist/src/expr/expr.js +171 -0
- package/dist/src/expr/expr.js.map +1 -0
- package/dist/src/expr/float.d.ts +357 -0
- package/dist/src/expr/float.d.ts.map +1 -0
- package/dist/src/expr/float.js +637 -0
- package/dist/src/expr/float.js.map +1 -0
- package/dist/src/expr/function.d.ts +46 -0
- package/dist/src/expr/function.d.ts.map +1 -0
- package/dist/src/expr/function.js +58 -0
- package/dist/src/expr/function.js.map +1 -0
- package/dist/src/expr/index.d.ts +450 -0
- package/dist/src/expr/index.d.ts.map +1 -0
- package/dist/src/expr/index.js +423 -0
- package/dist/src/expr/index.js.map +1 -0
- package/dist/src/expr/integer.d.ts +256 -0
- package/dist/src/expr/integer.d.ts.map +1 -0
- package/dist/src/expr/integer.js +311 -0
- package/dist/src/expr/integer.js.map +1 -0
- package/dist/src/expr/libs/array.d.ts +106 -0
- package/dist/src/expr/libs/array.d.ts.map +1 -0
- package/dist/src/expr/libs/array.js +140 -0
- package/dist/src/expr/libs/array.js.map +1 -0
- package/dist/src/expr/libs/blob.d.ts +42 -0
- package/dist/src/expr/libs/blob.d.ts.map +1 -0
- package/dist/src/expr/libs/blob.js +70 -0
- package/dist/src/expr/libs/blob.js.map +1 -0
- package/dist/src/expr/libs/datetime.d.ts +479 -0
- package/dist/src/expr/libs/datetime.d.ts.map +1 -0
- package/dist/src/expr/libs/datetime.js +624 -0
- package/dist/src/expr/libs/datetime.js.map +1 -0
- package/dist/src/expr/libs/dict.d.ts +66 -0
- package/dist/src/expr/libs/dict.d.ts.map +1 -0
- package/dist/src/expr/libs/dict.js +77 -0
- package/dist/src/expr/libs/dict.js.map +1 -0
- package/dist/src/expr/libs/float.d.ts +299 -0
- package/dist/src/expr/libs/float.d.ts.map +1 -0
- package/dist/src/expr/libs/float.js +564 -0
- package/dist/src/expr/libs/float.js.map +1 -0
- package/dist/src/expr/libs/integer.d.ts +228 -0
- package/dist/src/expr/libs/integer.d.ts.map +1 -0
- package/dist/src/expr/libs/integer.js +398 -0
- package/dist/src/expr/libs/integer.js.map +1 -0
- package/dist/src/expr/libs/set.d.ts +59 -0
- package/dist/src/expr/libs/set.d.ts.map +1 -0
- package/dist/src/expr/libs/set.js +69 -0
- package/dist/src/expr/libs/set.js.map +1 -0
- package/dist/src/expr/libs/string.d.ts +71 -0
- package/dist/src/expr/libs/string.d.ts.map +1 -0
- package/dist/src/expr/libs/string.js +75 -0
- package/dist/src/expr/libs/string.js.map +1 -0
- package/dist/src/expr/never.d.ts +15 -0
- package/dist/src/expr/never.d.ts.map +1 -0
- package/dist/src/expr/never.js +12 -0
- package/dist/src/expr/never.js.map +1 -0
- package/dist/src/expr/null.d.ts +15 -0
- package/dist/src/expr/null.d.ts.map +1 -0
- package/dist/src/expr/null.js +12 -0
- package/dist/src/expr/null.js.map +1 -0
- package/dist/src/expr/ref.d.ts +103 -0
- package/dist/src/expr/ref.d.ts.map +1 -0
- package/dist/src/expr/ref.js +131 -0
- package/dist/src/expr/ref.js.map +1 -0
- package/dist/src/expr/regex_validation.d.ts +25 -0
- package/dist/src/expr/regex_validation.d.ts.map +1 -0
- package/dist/src/expr/regex_validation.js +130 -0
- package/dist/src/expr/regex_validation.js.map +1 -0
- package/dist/src/expr/set.d.ts +1071 -0
- package/dist/src/expr/set.d.ts.map +1 -0
- package/dist/src/expr/set.js +1137 -0
- package/dist/src/expr/set.js.map +1 -0
- package/dist/src/expr/string.d.ts +414 -0
- package/dist/src/expr/string.d.ts.map +1 -0
- package/dist/src/expr/string.js +683 -0
- package/dist/src/expr/string.js.map +1 -0
- package/dist/src/expr/struct.d.ts +48 -0
- package/dist/src/expr/struct.d.ts.map +1 -0
- package/dist/src/expr/struct.js +65 -0
- package/dist/src/expr/struct.js.map +1 -0
- package/dist/src/expr/types.d.ts +68 -0
- package/dist/src/expr/types.d.ts.map +1 -0
- package/dist/src/expr/types.js +6 -0
- package/dist/src/expr/types.js.map +1 -0
- package/dist/src/expr/variant.d.ts +137 -0
- package/dist/src/expr/variant.d.ts.map +1 -0
- package/dist/src/expr/variant.js +105 -0
- package/dist/src/expr/variant.js.map +1 -0
- package/dist/src/fuzz.d.ts +80 -0
- package/dist/src/fuzz.d.ts.map +1 -0
- package/dist/src/fuzz.js +300 -0
- package/dist/src/fuzz.js.map +1 -0
- package/dist/src/index.d.ts +21 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +21 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/internal.d.ts +36 -0
- package/dist/src/internal.d.ts.map +1 -0
- package/dist/src/internal.js +11 -0
- package/dist/src/internal.js.map +1 -0
- package/dist/src/ir.d.ts +1571 -0
- package/dist/src/ir.d.ts.map +1 -0
- package/dist/src/ir.js +56 -0
- package/dist/src/ir.js.map +1 -0
- package/dist/src/location.d.ts +48 -0
- package/dist/src/location.d.ts.map +1 -0
- package/dist/src/location.js +62 -0
- package/dist/src/location.js.map +1 -0
- package/dist/src/platform.d.ts +21 -0
- package/dist/src/platform.d.ts.map +1 -0
- package/dist/src/platform.js +8 -0
- package/dist/src/platform.js.map +1 -0
- package/dist/src/serialization/beast.d.ts +39 -0
- package/dist/src/serialization/beast.d.ts.map +1 -0
- package/dist/src/serialization/beast.js +555 -0
- package/dist/src/serialization/beast.js.map +1 -0
- package/dist/src/serialization/beast2-stream.d.ts +38 -0
- package/dist/src/serialization/beast2-stream.d.ts.map +1 -0
- package/dist/src/serialization/beast2-stream.js +665 -0
- package/dist/src/serialization/beast2-stream.js.map +1 -0
- package/dist/src/serialization/beast2.d.ts +41 -0
- package/dist/src/serialization/beast2.d.ts.map +1 -0
- package/dist/src/serialization/beast2.js +489 -0
- package/dist/src/serialization/beast2.js.map +1 -0
- package/dist/src/serialization/binary-utils.d.ts +151 -0
- package/dist/src/serialization/binary-utils.d.ts.map +1 -0
- package/dist/src/serialization/binary-utils.js +929 -0
- package/dist/src/serialization/binary-utils.js.map +1 -0
- package/dist/src/serialization/east.d.ts +84 -0
- package/dist/src/serialization/east.d.ts.map +1 -0
- package/dist/src/serialization/east.js +1802 -0
- package/dist/src/serialization/east.js.map +1 -0
- package/dist/src/serialization/index.d.ts +11 -0
- package/dist/src/serialization/index.d.ts.map +1 -0
- package/dist/src/serialization/index.js +12 -0
- package/dist/src/serialization/index.js.map +1 -0
- package/dist/src/serialization/json.d.ts +36 -0
- package/dist/src/serialization/json.d.ts.map +1 -0
- package/dist/src/serialization/json.js +849 -0
- package/dist/src/serialization/json.js.map +1 -0
- package/dist/src/type_of_type.d.ts +115 -0
- package/dist/src/type_of_type.d.ts.map +1 -0
- package/dist/src/type_of_type.js +362 -0
- package/dist/src/type_of_type.js.map +1 -0
- package/dist/src/types.d.ts +648 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +1631 -0
- package/dist/src/types.js.map +1 -0
- package/package.json +87 -0
|
@@ -0,0 +1,980 @@
|
|
|
1
|
+
import { get_location } from "../location.js";
|
|
2
|
+
import { ArrayType, DateTimeType, FloatType, IntegerType, StringType } from "../types.js";
|
|
3
|
+
import { AstSymbol, Expr, FactorySymbol, TypeSymbol } from "./expr.js";
|
|
4
|
+
import { valueOrExprToAst } from "./ast.js";
|
|
5
|
+
import { tokenizeDateTimeFormat } from "../datetime_format/tokenize.js";
|
|
6
|
+
import { DateTimeFormatTokenType } from "../datetime_format/types.js";
|
|
7
|
+
import { validateDateTimeFormatTokens } from "../datetime_format/validate.js";
|
|
8
|
+
/**
|
|
9
|
+
* Expression representing date and time values and operations.
|
|
10
|
+
*
|
|
11
|
+
* DateTimeExpr provides methods for extracting components (year, month, day, hour, etc.),
|
|
12
|
+
* adding/subtracting durations, calculating differences, formatting, and parsing.
|
|
13
|
+
* DateTime values are immutable and represent UTC timestamps with millisecond precision.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* // Creating and manipulating dates
|
|
18
|
+
* const addTime = East.function([DateTimeType], DateTimeType, ($, date) => {
|
|
19
|
+
* const tomorrow = date.addDays(1n);
|
|
20
|
+
* const nextWeek = date.addWeeks(1n);
|
|
21
|
+
* $.return(nextWeek);
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Extracting components
|
|
25
|
+
* const getComponents = East.function([DateTimeType], StructType({year: IntegerType, month: IntegerType}), ($, date) => {
|
|
26
|
+
* $.return({year: date.getYear(), month: date.getMonth()});
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* // Formatting dates
|
|
30
|
+
* const formatDate = East.function([DateTimeType], StringType, ($, date) => {
|
|
31
|
+
* $.return(date.printFormatted("YYYY-MM-DD HH:mm:ss"));
|
|
32
|
+
* });
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export class DateTimeExpr extends Expr {
|
|
36
|
+
constructor(ast, createExpr) {
|
|
37
|
+
super(DateTimeType, ast, createExpr);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Extracts the year component from the DateTime.
|
|
41
|
+
*
|
|
42
|
+
* @returns An IntegerExpr representing the year (e.g., 2025n)
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* const extractYear = East.function([DateTimeType], IntegerType, ($, date) => {
|
|
47
|
+
* $.return(date.getYear());
|
|
48
|
+
* });
|
|
49
|
+
* const compiled = East.compile(extractYear.toIR(), []);
|
|
50
|
+
* compiled(new Date("2025-01-15T10:30:00.000Z")); // 2025n
|
|
51
|
+
* compiled(new Date("1999-12-31T23:59:59.999Z")); // 1999n
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
getYear() {
|
|
55
|
+
return this[FactorySymbol]({
|
|
56
|
+
ast_type: "Builtin",
|
|
57
|
+
type: IntegerType,
|
|
58
|
+
location: get_location(2),
|
|
59
|
+
builtin: "DateTimeGetYear",
|
|
60
|
+
type_parameters: [],
|
|
61
|
+
arguments: [this[AstSymbol]],
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Extracts the month component from the DateTime.
|
|
66
|
+
*
|
|
67
|
+
* @returns An IntegerExpr representing the month (1-12, where 1 = January, 12 = December)
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* const extractMonth = East.function([DateTimeType], IntegerType, ($, date) => {
|
|
72
|
+
* $.return(date.getMonth());
|
|
73
|
+
* });
|
|
74
|
+
* const compiled = East.compile(extractMonth.toIR(), []);
|
|
75
|
+
* compiled(new Date("2025-01-15T10:30:00.000Z")); // 1n
|
|
76
|
+
* compiled(new Date("2025-12-31T23:59:59.999Z")); // 12n
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
getMonth() {
|
|
80
|
+
return this[FactorySymbol]({
|
|
81
|
+
ast_type: "Builtin",
|
|
82
|
+
type: IntegerType,
|
|
83
|
+
location: get_location(2),
|
|
84
|
+
builtin: "DateTimeGetMonth",
|
|
85
|
+
type_parameters: [],
|
|
86
|
+
arguments: [this[AstSymbol]],
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Extracts the day of the month from the DateTime.
|
|
91
|
+
*
|
|
92
|
+
* @returns An IntegerExpr representing the day (1-31)
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* const extractDay = East.function([DateTimeType], IntegerType, ($, date) => {
|
|
97
|
+
* $.return(date.getDayOfMonth());
|
|
98
|
+
* });
|
|
99
|
+
* const compiled = East.compile(extractDay.toIR(), []);
|
|
100
|
+
* compiled(new Date("2025-01-15T10:30:00.000Z")); // 15n
|
|
101
|
+
* compiled(new Date("2025-02-28T12:00:00.000Z")); // 28n
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
getDayOfMonth() {
|
|
105
|
+
return this[FactorySymbol]({
|
|
106
|
+
ast_type: "Builtin",
|
|
107
|
+
type: IntegerType,
|
|
108
|
+
location: get_location(2),
|
|
109
|
+
builtin: "DateTimeGetDayOfMonth",
|
|
110
|
+
type_parameters: [],
|
|
111
|
+
arguments: [this[AstSymbol]],
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Extracts the hour component from the DateTime.
|
|
116
|
+
*
|
|
117
|
+
* @returns An IntegerExpr representing the hour (0-23 in 24-hour format)
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* const extractHour = East.function([DateTimeType], IntegerType, ($, date) => {
|
|
122
|
+
* $.return(date.getHour());
|
|
123
|
+
* });
|
|
124
|
+
* const compiled = East.compile(extractHour.toIR(), []);
|
|
125
|
+
* compiled(new Date("2025-01-15T14:30:00.000Z")); // 14n
|
|
126
|
+
* compiled(new Date("2025-01-15T00:00:00.000Z")); // 0n
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
getHour() {
|
|
130
|
+
return this[FactorySymbol]({
|
|
131
|
+
ast_type: "Builtin",
|
|
132
|
+
type: IntegerType,
|
|
133
|
+
location: get_location(2),
|
|
134
|
+
builtin: "DateTimeGetHour",
|
|
135
|
+
type_parameters: [],
|
|
136
|
+
arguments: [this[AstSymbol]],
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Extracts the minute component from the DateTime.
|
|
141
|
+
*
|
|
142
|
+
* @returns An IntegerExpr representing the minute (0-59)
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const extractMinute = East.function([DateTimeType], IntegerType, ($, date) => {
|
|
147
|
+
* $.return(date.getMinute());
|
|
148
|
+
* });
|
|
149
|
+
* const compiled = East.compile(extractMinute.toIR(), []);
|
|
150
|
+
* compiled(new Date("2025-01-15T14:30:45.000Z")); // 30n
|
|
151
|
+
* compiled(new Date("2025-01-15T14:00:00.000Z")); // 0n
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
getMinute() {
|
|
155
|
+
return this[FactorySymbol]({
|
|
156
|
+
ast_type: "Builtin",
|
|
157
|
+
type: IntegerType,
|
|
158
|
+
location: get_location(2),
|
|
159
|
+
builtin: "DateTimeGetMinute",
|
|
160
|
+
type_parameters: [],
|
|
161
|
+
arguments: [this[AstSymbol]],
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Extracts the second component from the DateTime.
|
|
166
|
+
*
|
|
167
|
+
* @returns An IntegerExpr representing the second (0-59)
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* const extractSecond = East.function([DateTimeType], IntegerType, ($, date) => {
|
|
172
|
+
* $.return(date.getSecond());
|
|
173
|
+
* });
|
|
174
|
+
* const compiled = East.compile(extractSecond.toIR(), []);
|
|
175
|
+
* compiled(new Date("2025-01-15T14:30:45.000Z")); // 45n
|
|
176
|
+
* compiled(new Date("2025-01-15T14:30:00.000Z")); // 0n
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
getSecond() {
|
|
180
|
+
return this[FactorySymbol]({
|
|
181
|
+
ast_type: "Builtin",
|
|
182
|
+
type: IntegerType,
|
|
183
|
+
location: get_location(2),
|
|
184
|
+
builtin: "DateTimeGetSecond",
|
|
185
|
+
type_parameters: [],
|
|
186
|
+
arguments: [this[AstSymbol]],
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Extracts the day of the week from the DateTime.
|
|
191
|
+
*
|
|
192
|
+
* @returns An IntegerExpr representing the day (0-6, where 0 = Sunday, 6 = Saturday)
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* const extractDayOfWeek = East.function([DateTimeType], IntegerType, ($, date) => {
|
|
197
|
+
* $.return(date.getDayOfWeek());
|
|
198
|
+
* });
|
|
199
|
+
* const compiled = East.compile(extractDayOfWeek.toIR(), []);
|
|
200
|
+
* compiled(new Date("2025-01-15T00:00:00.000Z")); // 3n (Wednesday)
|
|
201
|
+
* compiled(new Date("2025-01-12T00:00:00.000Z")); // 0n (Sunday)
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
getDayOfWeek() {
|
|
205
|
+
return this[FactorySymbol]({
|
|
206
|
+
ast_type: "Builtin",
|
|
207
|
+
type: IntegerType,
|
|
208
|
+
location: get_location(2),
|
|
209
|
+
builtin: "DateTimeGetDayOfWeek",
|
|
210
|
+
type_parameters: [],
|
|
211
|
+
arguments: [this[AstSymbol]],
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Extracts the millisecond component from the DateTime.
|
|
216
|
+
*
|
|
217
|
+
* @returns An IntegerExpr representing the millisecond (0-999)
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* const extractMillisecond = East.function([DateTimeType], IntegerType, ($, date) => {
|
|
222
|
+
* $.return(date.getMillisecond());
|
|
223
|
+
* });
|
|
224
|
+
* const compiled = East.compile(extractMillisecond.toIR(), []);
|
|
225
|
+
* compiled(new Date("2025-01-15T14:30:45.123Z")); // 123n
|
|
226
|
+
* compiled(new Date("2025-01-15T14:30:45.000Z")); // 0n
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
getMillisecond() {
|
|
230
|
+
return this[FactorySymbol]({
|
|
231
|
+
ast_type: "Builtin",
|
|
232
|
+
type: IntegerType,
|
|
233
|
+
location: get_location(2),
|
|
234
|
+
builtin: "DateTimeGetMillisecond",
|
|
235
|
+
type_parameters: [],
|
|
236
|
+
arguments: [this[AstSymbol]],
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Adds milliseconds to the DateTime, returning a new DateTime.
|
|
241
|
+
*
|
|
242
|
+
* @param milliseconds - The number of milliseconds to add (integer or float)
|
|
243
|
+
* @returns A DateTimeExpr representing the new DateTime
|
|
244
|
+
*
|
|
245
|
+
* @remarks Floats are rounded to the nearest millisecond. Accepts negative values.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```ts
|
|
249
|
+
* const addMs = East.function([DateTimeType, IntegerType], DateTimeType, ($, date, ms) => {
|
|
250
|
+
* $.return(date.addMilliseconds(ms));
|
|
251
|
+
* });
|
|
252
|
+
* const compiled = East.compile(addMs.toIR(), []);
|
|
253
|
+
* const date = new Date("2025-01-15T14:30:45.000Z");
|
|
254
|
+
* compiled(date, 500n); // Date("2025-01-15T14:30:45.500Z")
|
|
255
|
+
* compiled(date, 1000n); // Date("2025-01-15T14:30:46.000Z")
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
addMilliseconds(milliseconds) {
|
|
259
|
+
// Convert to bigint milliseconds
|
|
260
|
+
let ms;
|
|
261
|
+
if (typeof milliseconds === 'bigint') {
|
|
262
|
+
ms = Expr.from(milliseconds, IntegerType);
|
|
263
|
+
}
|
|
264
|
+
else if (typeof milliseconds === 'number') {
|
|
265
|
+
// Round float to nearest millisecond and convert to bigint
|
|
266
|
+
ms = Expr.from(BigInt(Math.round(milliseconds)), IntegerType);
|
|
267
|
+
}
|
|
268
|
+
else if (milliseconds[TypeSymbol] === IntegerType) {
|
|
269
|
+
ms = milliseconds;
|
|
270
|
+
}
|
|
271
|
+
else if (milliseconds[TypeSymbol] === FloatType) {
|
|
272
|
+
// Convert float expression to integer expression by rounding
|
|
273
|
+
ms = this[FactorySymbol]({
|
|
274
|
+
ast_type: "Builtin",
|
|
275
|
+
type: IntegerType,
|
|
276
|
+
location: get_location(2),
|
|
277
|
+
builtin: "FloatToInteger",
|
|
278
|
+
type_parameters: [],
|
|
279
|
+
arguments: [milliseconds[AstSymbol]],
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
throw new Error("Expected integer or float");
|
|
284
|
+
}
|
|
285
|
+
return this[FactorySymbol]({
|
|
286
|
+
ast_type: "Builtin",
|
|
287
|
+
type: DateTimeType,
|
|
288
|
+
location: get_location(2),
|
|
289
|
+
builtin: "DateTimeAddMilliseconds",
|
|
290
|
+
type_parameters: [],
|
|
291
|
+
arguments: [this[AstSymbol], ms[AstSymbol]],
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Subtracts milliseconds from the DateTime, returning a new DateTime.
|
|
296
|
+
*
|
|
297
|
+
* @param milliseconds - The number of milliseconds to subtract (integer or float)
|
|
298
|
+
* @returns A DateTimeExpr representing the new DateTime
|
|
299
|
+
*
|
|
300
|
+
* @remarks Floats are rounded to the nearest millisecond.
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```ts
|
|
304
|
+
* const subMs = East.function([DateTimeType, IntegerType], DateTimeType, ($, date, ms) => {
|
|
305
|
+
* $.return(date.subtractMilliseconds(ms));
|
|
306
|
+
* });
|
|
307
|
+
* const compiled = East.compile(subMs.toIR(), []);
|
|
308
|
+
* const date = new Date("2025-01-15T14:30:45.500Z");
|
|
309
|
+
* compiled(date, 500n); // Date("2025-01-15T14:30:45.000Z")
|
|
310
|
+
* compiled(date, 1000n); // Date("2025-01-15T14:30:44.500Z")
|
|
311
|
+
* ```
|
|
312
|
+
*/
|
|
313
|
+
subtractMilliseconds(milliseconds) {
|
|
314
|
+
// Negate the input and use addMilliseconds
|
|
315
|
+
if (typeof milliseconds === 'bigint') {
|
|
316
|
+
return this.addMilliseconds(-milliseconds);
|
|
317
|
+
}
|
|
318
|
+
else if (typeof milliseconds === 'number') {
|
|
319
|
+
return this.addMilliseconds(-milliseconds);
|
|
320
|
+
}
|
|
321
|
+
else if (milliseconds[TypeSymbol] === IntegerType) {
|
|
322
|
+
// Negate integer expression
|
|
323
|
+
const negated = this[FactorySymbol]({
|
|
324
|
+
ast_type: "Builtin",
|
|
325
|
+
type: IntegerType,
|
|
326
|
+
location: get_location(2),
|
|
327
|
+
builtin: "IntegerNegate",
|
|
328
|
+
type_parameters: [],
|
|
329
|
+
arguments: [milliseconds[AstSymbol]],
|
|
330
|
+
});
|
|
331
|
+
return this.addMilliseconds(negated);
|
|
332
|
+
}
|
|
333
|
+
else if (milliseconds[TypeSymbol] === FloatType) {
|
|
334
|
+
// Negate float expression
|
|
335
|
+
const negated = this[FactorySymbol]({
|
|
336
|
+
ast_type: "Builtin",
|
|
337
|
+
type: FloatType,
|
|
338
|
+
location: get_location(2),
|
|
339
|
+
builtin: "FloatNegate",
|
|
340
|
+
type_parameters: [],
|
|
341
|
+
arguments: [milliseconds[AstSymbol]],
|
|
342
|
+
});
|
|
343
|
+
return this.addMilliseconds(negated);
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
throw new Error("Expected integer or float");
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Adds seconds to the DateTime, returning a new DateTime.
|
|
351
|
+
*
|
|
352
|
+
* @param seconds - The number of seconds to add (integer or float)
|
|
353
|
+
* @returns A DateTimeExpr representing the new DateTime
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```ts
|
|
357
|
+
* const addSec = East.function([DateTimeType, IntegerType], DateTimeType, ($, date, secs) => {
|
|
358
|
+
* $.return(date.addSeconds(secs));
|
|
359
|
+
* });
|
|
360
|
+
* const compiled = East.compile(addSec.toIR(), []);
|
|
361
|
+
* compiled(new Date("2025-01-15T14:30:00.000Z"), 30n); // Date("2025-01-15T14:30:30.000Z")
|
|
362
|
+
* ```
|
|
363
|
+
*/
|
|
364
|
+
addSeconds(seconds) {
|
|
365
|
+
return this._addDuration(seconds, 1000n, 1000);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Adds minutes to the DateTime, returning a new DateTime.
|
|
369
|
+
*
|
|
370
|
+
* @param minutes - The number of minutes to add (integer or float)
|
|
371
|
+
* @returns A DateTimeExpr representing the new DateTime
|
|
372
|
+
*
|
|
373
|
+
* @example
|
|
374
|
+
* ```ts
|
|
375
|
+
* const addMin = East.function([DateTimeType, IntegerType], DateTimeType, ($, date, mins) => {
|
|
376
|
+
* $.return(date.addMinutes(mins));
|
|
377
|
+
* });
|
|
378
|
+
* const compiled = East.compile(addMin.toIR(), []);
|
|
379
|
+
* compiled(new Date("2025-01-15T14:00:00.000Z"), 30n); // Date("2025-01-15T14:30:00.000Z")
|
|
380
|
+
* ```
|
|
381
|
+
*/
|
|
382
|
+
addMinutes(minutes) {
|
|
383
|
+
return this._addDuration(minutes, 60000n, 60000);
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Adds hours to the DateTime, returning a new DateTime.
|
|
387
|
+
*
|
|
388
|
+
* @param hours - The number of hours to add (integer or float)
|
|
389
|
+
* @returns A DateTimeExpr representing the new DateTime
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* ```ts
|
|
393
|
+
* const addHr = East.function([DateTimeType, IntegerType], DateTimeType, ($, date, hrs) => {
|
|
394
|
+
* $.return(date.addHours(hrs));
|
|
395
|
+
* });
|
|
396
|
+
* const compiled = East.compile(addHr.toIR(), []);
|
|
397
|
+
* compiled(new Date("2025-01-15T14:00:00.000Z"), 2n); // Date("2025-01-15T16:00:00.000Z")
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
400
|
+
addHours(hours) {
|
|
401
|
+
return this._addDuration(hours, 3600000n, 3600000);
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Adds days to the DateTime, returning a new DateTime.
|
|
405
|
+
*
|
|
406
|
+
* @param days - The number of days to add (integer or float)
|
|
407
|
+
* @returns A DateTimeExpr representing the new DateTime
|
|
408
|
+
*
|
|
409
|
+
* @example
|
|
410
|
+
* ```ts
|
|
411
|
+
* const addDay = East.function([DateTimeType, IntegerType], DateTimeType, ($, date, days) => {
|
|
412
|
+
* $.return(date.addDays(days));
|
|
413
|
+
* });
|
|
414
|
+
* const compiled = East.compile(addDay.toIR(), []);
|
|
415
|
+
* compiled(new Date("2025-01-15T00:00:00.000Z"), 1n); // Date("2025-01-16T00:00:00.000Z")
|
|
416
|
+
* compiled(new Date("2025-01-31T00:00:00.000Z"), 1n); // Date("2025-02-01T00:00:00.000Z")
|
|
417
|
+
* ```
|
|
418
|
+
*/
|
|
419
|
+
addDays(days) {
|
|
420
|
+
return this._addDuration(days, 86400000n, 86400000);
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Adds weeks to the DateTime, returning a new DateTime.
|
|
424
|
+
*
|
|
425
|
+
* @param weeks - The number of weeks to add (integer or float)
|
|
426
|
+
* @returns A DateTimeExpr representing the new DateTime
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* ```ts
|
|
430
|
+
* const addWk = East.function([DateTimeType, IntegerType], DateTimeType, ($, date, weeks) => {
|
|
431
|
+
* $.return(date.addWeeks(weeks));
|
|
432
|
+
* });
|
|
433
|
+
* const compiled = East.compile(addWk.toIR(), []);
|
|
434
|
+
* compiled(new Date("2025-01-15T00:00:00.000Z"), 1n); // Date("2025-01-22T00:00:00.000Z")
|
|
435
|
+
* compiled(new Date("2025-01-15T00:00:00.000Z"), 2n); // Date("2025-01-29T00:00:00.000Z")
|
|
436
|
+
* ```
|
|
437
|
+
*/
|
|
438
|
+
addWeeks(weeks) {
|
|
439
|
+
return this._addDuration(weeks, 604800000n, 604800000); // 7 * 24 * 60 * 60 * 1000 = 604800000ms
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Subtracts seconds from the DateTime, returning a new DateTime.
|
|
443
|
+
*
|
|
444
|
+
* @param seconds - The number of seconds to subtract
|
|
445
|
+
* @returns A DateTimeExpr representing the new DateTime
|
|
446
|
+
*/
|
|
447
|
+
subtractSeconds(seconds) {
|
|
448
|
+
return this._subtractDuration(seconds, 1000n, 1000);
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Subtracts minutes from the DateTime, returning a new DateTime.
|
|
452
|
+
*
|
|
453
|
+
* @param minutes - The number of minutes to subtract
|
|
454
|
+
* @returns A DateTimeExpr representing the new DateTime
|
|
455
|
+
*/
|
|
456
|
+
subtractMinutes(minutes) {
|
|
457
|
+
return this._subtractDuration(minutes, 60000n, 60000);
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Subtracts hours from the DateTime, returning a new DateTime.
|
|
461
|
+
*
|
|
462
|
+
* @param hours - The number of hours to subtract
|
|
463
|
+
* @returns A DateTimeExpr representing the new DateTime
|
|
464
|
+
*/
|
|
465
|
+
subtractHours(hours) {
|
|
466
|
+
return this._subtractDuration(hours, 3600000n, 3600000);
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Subtracts days from the DateTime, returning a new DateTime.
|
|
470
|
+
*
|
|
471
|
+
* @param days - The number of days to subtract
|
|
472
|
+
* @returns A DateTimeExpr representing the new DateTime
|
|
473
|
+
*/
|
|
474
|
+
subtractDays(days) {
|
|
475
|
+
return this._subtractDuration(days, 86400000n, 86400000);
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Subtracts weeks from the DateTime, returning a new DateTime.
|
|
479
|
+
*
|
|
480
|
+
* @param weeks - The number of weeks to subtract
|
|
481
|
+
* @returns A DateTimeExpr representing the new DateTime
|
|
482
|
+
*/
|
|
483
|
+
subtractWeeks(weeks) {
|
|
484
|
+
return this._subtractDuration(weeks, 604800000n, 604800000); // 7 * 24 * 60 * 60 * 1000 = 604800000ms
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Helper method to add duration with conversion factor
|
|
488
|
+
*/
|
|
489
|
+
_addDuration(value, bigintFactor, numberFactor) {
|
|
490
|
+
let ms;
|
|
491
|
+
if (typeof value === 'bigint') {
|
|
492
|
+
ms = Expr.from(value * bigintFactor, IntegerType);
|
|
493
|
+
}
|
|
494
|
+
else if (typeof value === 'number') {
|
|
495
|
+
// Round to nearest millisecond after conversion
|
|
496
|
+
ms = Expr.from(BigInt(Math.round(value * numberFactor)), IntegerType);
|
|
497
|
+
}
|
|
498
|
+
else if (value[TypeSymbol] === IntegerType) {
|
|
499
|
+
// Multiply integer expression by factor
|
|
500
|
+
const factorExpr = Expr.from(bigintFactor, IntegerType);
|
|
501
|
+
ms = this[FactorySymbol]({
|
|
502
|
+
ast_type: "Builtin",
|
|
503
|
+
type: IntegerType,
|
|
504
|
+
location: get_location(2),
|
|
505
|
+
builtin: "IntegerMultiply",
|
|
506
|
+
type_parameters: [],
|
|
507
|
+
arguments: [value[AstSymbol], factorExpr[AstSymbol]],
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
else if (value[TypeSymbol] === FloatType) {
|
|
511
|
+
// Convert float to integer, then multiply by factor
|
|
512
|
+
const intValue = this[FactorySymbol]({
|
|
513
|
+
ast_type: "Builtin",
|
|
514
|
+
type: IntegerType,
|
|
515
|
+
location: get_location(2),
|
|
516
|
+
builtin: "FloatToInteger",
|
|
517
|
+
type_parameters: [],
|
|
518
|
+
arguments: [value[AstSymbol]],
|
|
519
|
+
});
|
|
520
|
+
const factorExpr = Expr.from(bigintFactor, IntegerType);
|
|
521
|
+
ms = this[FactorySymbol]({
|
|
522
|
+
ast_type: "Builtin",
|
|
523
|
+
type: IntegerType,
|
|
524
|
+
location: get_location(2),
|
|
525
|
+
builtin: "IntegerMultiply",
|
|
526
|
+
type_parameters: [],
|
|
527
|
+
arguments: [intValue[AstSymbol], factorExpr[AstSymbol]],
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
throw new Error("Expected integer or float");
|
|
532
|
+
}
|
|
533
|
+
return this.addMilliseconds(ms);
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Helper method to subtract duration with conversion factor
|
|
537
|
+
*/
|
|
538
|
+
_subtractDuration(value, bigintFactor, numberFactor) {
|
|
539
|
+
let negatedValue;
|
|
540
|
+
if (typeof value === 'bigint') {
|
|
541
|
+
negatedValue = -value;
|
|
542
|
+
}
|
|
543
|
+
else if (typeof value === 'number') {
|
|
544
|
+
negatedValue = -value;
|
|
545
|
+
}
|
|
546
|
+
else if (value[TypeSymbol] === IntegerType) {
|
|
547
|
+
// Negate integer expression
|
|
548
|
+
negatedValue = this[FactorySymbol]({
|
|
549
|
+
ast_type: "Builtin",
|
|
550
|
+
type: IntegerType,
|
|
551
|
+
location: get_location(2),
|
|
552
|
+
builtin: "IntegerNegate",
|
|
553
|
+
type_parameters: [],
|
|
554
|
+
arguments: [value[AstSymbol]],
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
else if (value[TypeSymbol] === FloatType) {
|
|
558
|
+
// Negate float expression
|
|
559
|
+
negatedValue = this[FactorySymbol]({
|
|
560
|
+
ast_type: "Builtin",
|
|
561
|
+
type: FloatType,
|
|
562
|
+
location: get_location(2),
|
|
563
|
+
builtin: "FloatNegate",
|
|
564
|
+
type_parameters: [],
|
|
565
|
+
arguments: [value[AstSymbol]],
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
else {
|
|
569
|
+
throw new Error("Expected integer or float");
|
|
570
|
+
}
|
|
571
|
+
return this._addDuration(negatedValue, bigintFactor, numberFactor);
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Calculates the duration in milliseconds between this DateTime and another.
|
|
575
|
+
*
|
|
576
|
+
* @param other - The DateTime to compare with
|
|
577
|
+
* @returns An IntegerExpr representing the duration (positive if other is after this, negative if before)
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* ```ts
|
|
581
|
+
* const getDuration = East.function([DateTimeType, DateTimeType], IntegerType, ($, date1, date2) => {
|
|
582
|
+
* $.return(date1.durationMilliseconds(date2));
|
|
583
|
+
* });
|
|
584
|
+
* const compiled = East.compile(getDuration.toIR(), []);
|
|
585
|
+
* const d1 = new Date("2025-01-15T14:30:00.000Z");
|
|
586
|
+
* const d2 = new Date("2025-01-15T14:30:01.500Z");
|
|
587
|
+
* compiled(d1, d2); // 1500n (1.5 seconds later)
|
|
588
|
+
* compiled(d2, d1); // -1500n (1.5 seconds earlier)
|
|
589
|
+
* ```
|
|
590
|
+
*/
|
|
591
|
+
durationMilliseconds(other) {
|
|
592
|
+
const otherAst = valueOrExprToAst(other);
|
|
593
|
+
return this[FactorySymbol]({
|
|
594
|
+
ast_type: "Builtin",
|
|
595
|
+
type: IntegerType,
|
|
596
|
+
location: get_location(2),
|
|
597
|
+
builtin: "DateTimeDurationMilliseconds",
|
|
598
|
+
type_parameters: [],
|
|
599
|
+
arguments: [otherAst, this[AstSymbol]],
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Calculates the duration in seconds between this DateTime and another.
|
|
604
|
+
*
|
|
605
|
+
* @param other - The DateTime to compare with
|
|
606
|
+
* @returns A FloatExpr representing the duration in seconds
|
|
607
|
+
*
|
|
608
|
+
* @example
|
|
609
|
+
* ```ts
|
|
610
|
+
* const getDurationSec = East.function([DateTimeType, DateTimeType], FloatType, ($, date1, date2) => {
|
|
611
|
+
* $.return(date1.durationSeconds(date2));
|
|
612
|
+
* });
|
|
613
|
+
* const compiled = East.compile(getDurationSec.toIR(), []);
|
|
614
|
+
* const d1 = new Date("2025-01-15T14:30:00.000Z");
|
|
615
|
+
* const d2 = new Date("2025-01-15T14:30:30.000Z");
|
|
616
|
+
* compiled(d1, d2); // 30.0 (30 seconds later)
|
|
617
|
+
* ```
|
|
618
|
+
*/
|
|
619
|
+
durationSeconds(other) {
|
|
620
|
+
const milliseconds = this.durationMilliseconds(other);
|
|
621
|
+
// Convert to float
|
|
622
|
+
const millisecondsFloat = this[FactorySymbol]({
|
|
623
|
+
ast_type: "Builtin",
|
|
624
|
+
type: FloatType,
|
|
625
|
+
location: get_location(2),
|
|
626
|
+
builtin: "IntegerToFloat",
|
|
627
|
+
type_parameters: [],
|
|
628
|
+
arguments: [milliseconds[AstSymbol]],
|
|
629
|
+
});
|
|
630
|
+
// Divide by 1000 to get seconds
|
|
631
|
+
const divisor = Expr.from(1000.0, FloatType);
|
|
632
|
+
return this[FactorySymbol]({
|
|
633
|
+
ast_type: "Builtin",
|
|
634
|
+
type: FloatType,
|
|
635
|
+
location: get_location(2),
|
|
636
|
+
builtin: "FloatDivide",
|
|
637
|
+
type_parameters: [],
|
|
638
|
+
arguments: [millisecondsFloat[AstSymbol], divisor[AstSymbol]],
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Calculates the duration in minutes between this DateTime and another.
|
|
643
|
+
*
|
|
644
|
+
* @param other - The DateTime to compare with
|
|
645
|
+
* @returns A FloatExpr representing the duration in minutes
|
|
646
|
+
*
|
|
647
|
+
* @example
|
|
648
|
+
* ```ts
|
|
649
|
+
* const getDurationMin = East.function([DateTimeType, DateTimeType], FloatType, ($, date1, date2) => {
|
|
650
|
+
* $.return(date1.durationMinutes(date2));
|
|
651
|
+
* });
|
|
652
|
+
* const compiled = East.compile(getDurationMin.toIR(), []);
|
|
653
|
+
* const d1 = new Date("2025-01-15T14:00:00.000Z");
|
|
654
|
+
* const d2 = new Date("2025-01-15T14:30:00.000Z");
|
|
655
|
+
* compiled(d1, d2); // 30.0 (30 minutes later)
|
|
656
|
+
* ```
|
|
657
|
+
*/
|
|
658
|
+
durationMinutes(other) {
|
|
659
|
+
const milliseconds = this.durationMilliseconds(other);
|
|
660
|
+
// Convert to float
|
|
661
|
+
const millisecondsFloat = this[FactorySymbol]({
|
|
662
|
+
ast_type: "Builtin",
|
|
663
|
+
type: FloatType,
|
|
664
|
+
location: get_location(2),
|
|
665
|
+
builtin: "IntegerToFloat",
|
|
666
|
+
type_parameters: [],
|
|
667
|
+
arguments: [milliseconds[AstSymbol]],
|
|
668
|
+
});
|
|
669
|
+
// Divide by 60000 to get minutes
|
|
670
|
+
const divisor = Expr.from(60000.0, FloatType);
|
|
671
|
+
return this[FactorySymbol]({
|
|
672
|
+
ast_type: "Builtin",
|
|
673
|
+
type: FloatType,
|
|
674
|
+
location: get_location(2),
|
|
675
|
+
builtin: "FloatDivide",
|
|
676
|
+
type_parameters: [],
|
|
677
|
+
arguments: [millisecondsFloat[AstSymbol], divisor[AstSymbol]],
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Calculates the duration in hours between this DateTime and another.
|
|
682
|
+
*
|
|
683
|
+
* @param other - The DateTime to compare with
|
|
684
|
+
* @returns A FloatExpr representing the duration in hours
|
|
685
|
+
*
|
|
686
|
+
* @example
|
|
687
|
+
* ```ts
|
|
688
|
+
* const getDurationHr = East.function([DateTimeType, DateTimeType], FloatType, ($, date1, date2) => {
|
|
689
|
+
* $.return(date1.durationHours(date2));
|
|
690
|
+
* });
|
|
691
|
+
* const compiled = East.compile(getDurationHr.toIR(), []);
|
|
692
|
+
* const d1 = new Date("2025-01-15T10:00:00.000Z");
|
|
693
|
+
* const d2 = new Date("2025-01-15T14:30:00.000Z");
|
|
694
|
+
* compiled(d1, d2); // 4.5 (4.5 hours later)
|
|
695
|
+
* ```
|
|
696
|
+
*/
|
|
697
|
+
durationHours(other) {
|
|
698
|
+
const milliseconds = this.durationMilliseconds(other);
|
|
699
|
+
// Convert to float
|
|
700
|
+
const millisecondsFloat = this[FactorySymbol]({
|
|
701
|
+
ast_type: "Builtin",
|
|
702
|
+
type: FloatType,
|
|
703
|
+
location: get_location(2),
|
|
704
|
+
builtin: "IntegerToFloat",
|
|
705
|
+
type_parameters: [],
|
|
706
|
+
arguments: [milliseconds[AstSymbol]],
|
|
707
|
+
});
|
|
708
|
+
// Divide by 3600000 to get hours
|
|
709
|
+
const divisor = Expr.from(3600000.0, FloatType);
|
|
710
|
+
return this[FactorySymbol]({
|
|
711
|
+
ast_type: "Builtin",
|
|
712
|
+
type: FloatType,
|
|
713
|
+
location: get_location(2),
|
|
714
|
+
builtin: "FloatDivide",
|
|
715
|
+
type_parameters: [],
|
|
716
|
+
arguments: [millisecondsFloat[AstSymbol], divisor[AstSymbol]],
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Calculates the duration in days between this DateTime and another.
|
|
721
|
+
*
|
|
722
|
+
* @param other - The DateTime to compare with
|
|
723
|
+
* @returns A FloatExpr representing the duration in days
|
|
724
|
+
*
|
|
725
|
+
* @example
|
|
726
|
+
* ```ts
|
|
727
|
+
* const getDurationDay = East.function([DateTimeType, DateTimeType], FloatType, ($, date1, date2) => {
|
|
728
|
+
* $.return(date1.durationDays(date2));
|
|
729
|
+
* });
|
|
730
|
+
* const compiled = East.compile(getDurationDay.toIR(), []);
|
|
731
|
+
* const d1 = new Date("2025-01-15T00:00:00.000Z");
|
|
732
|
+
* const d2 = new Date("2025-01-17T12:00:00.000Z");
|
|
733
|
+
* compiled(d1, d2); // 2.5 (2.5 days later)
|
|
734
|
+
* ```
|
|
735
|
+
*/
|
|
736
|
+
durationDays(other) {
|
|
737
|
+
const milliseconds = this.durationMilliseconds(other);
|
|
738
|
+
// Convert to float
|
|
739
|
+
const millisecondsFloat = this[FactorySymbol]({
|
|
740
|
+
ast_type: "Builtin",
|
|
741
|
+
type: FloatType,
|
|
742
|
+
location: get_location(2),
|
|
743
|
+
builtin: "IntegerToFloat",
|
|
744
|
+
type_parameters: [],
|
|
745
|
+
arguments: [milliseconds[AstSymbol]],
|
|
746
|
+
});
|
|
747
|
+
// Divide by 86400000 to get days
|
|
748
|
+
const divisor = Expr.from(86400000.0, FloatType);
|
|
749
|
+
return this[FactorySymbol]({
|
|
750
|
+
ast_type: "Builtin",
|
|
751
|
+
type: FloatType,
|
|
752
|
+
location: get_location(2),
|
|
753
|
+
builtin: "FloatDivide",
|
|
754
|
+
type_parameters: [],
|
|
755
|
+
arguments: [millisecondsFloat[AstSymbol], divisor[AstSymbol]],
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* Calculates the duration in weeks between this DateTime and another.
|
|
760
|
+
*
|
|
761
|
+
* @param other - The DateTime to compare with
|
|
762
|
+
* @returns A FloatExpr representing the duration in weeks
|
|
763
|
+
*
|
|
764
|
+
* @example
|
|
765
|
+
* ```ts
|
|
766
|
+
* const getDurationWk = East.function([DateTimeType, DateTimeType], FloatType, ($, date1, date2) => {
|
|
767
|
+
* $.return(date1.durationWeeks(date2));
|
|
768
|
+
* });
|
|
769
|
+
* const compiled = East.compile(getDurationWk.toIR(), []);
|
|
770
|
+
* const d1 = new Date("2025-01-01T00:00:00.000Z");
|
|
771
|
+
* const d2 = new Date("2025-01-15T00:00:00.000Z");
|
|
772
|
+
* compiled(d1, d2); // 2.0 (2 weeks later)
|
|
773
|
+
* ```
|
|
774
|
+
*/
|
|
775
|
+
durationWeeks(other) {
|
|
776
|
+
const milliseconds = this.durationMilliseconds(other);
|
|
777
|
+
// Convert to float
|
|
778
|
+
const millisecondsFloat = this[FactorySymbol]({
|
|
779
|
+
ast_type: "Builtin",
|
|
780
|
+
type: FloatType,
|
|
781
|
+
location: get_location(2),
|
|
782
|
+
builtin: "IntegerToFloat",
|
|
783
|
+
type_parameters: [],
|
|
784
|
+
arguments: [milliseconds[AstSymbol]],
|
|
785
|
+
});
|
|
786
|
+
// Divide by 604800000 to get weeks
|
|
787
|
+
const divisor = Expr.from(604800000.0, FloatType); // 7 * 24 * 60 * 60 * 1000 = 604800000ms
|
|
788
|
+
return this[FactorySymbol]({
|
|
789
|
+
ast_type: "Builtin",
|
|
790
|
+
type: FloatType,
|
|
791
|
+
location: get_location(2),
|
|
792
|
+
builtin: "FloatDivide",
|
|
793
|
+
type_parameters: [],
|
|
794
|
+
arguments: [millisecondsFloat[AstSymbol], divisor[AstSymbol]],
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
/**
|
|
798
|
+
* Converts the DateTime to milliseconds since the Unix epoch (like Date.valueOf()).
|
|
799
|
+
*
|
|
800
|
+
* @returns An IntegerExpr representing milliseconds since January 1, 1970 00:00:00 UTC
|
|
801
|
+
*
|
|
802
|
+
* @example
|
|
803
|
+
* ```ts
|
|
804
|
+
* const toEpoch = East.function([DateTimeType], IntegerType, ($, date) => {
|
|
805
|
+
* $.return(date.toEpochMilliseconds());
|
|
806
|
+
* });
|
|
807
|
+
* const compiled = East.compile(toEpoch.toIR(), []);
|
|
808
|
+
* compiled(new Date("1970-01-01T00:00:00.000Z")); // 0n
|
|
809
|
+
* compiled(new Date("2025-01-15T14:30:45.123Z")); // 1736950245123n
|
|
810
|
+
* ```
|
|
811
|
+
*/
|
|
812
|
+
toEpochMilliseconds() {
|
|
813
|
+
return this[FactorySymbol]({
|
|
814
|
+
ast_type: "Builtin",
|
|
815
|
+
type: IntegerType,
|
|
816
|
+
location: get_location(2),
|
|
817
|
+
builtin: "DateTimeToEpochMilliseconds",
|
|
818
|
+
type_parameters: [],
|
|
819
|
+
arguments: [this[AstSymbol]],
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* Format DateTime as a string using Day.js-style format tokens.
|
|
824
|
+
*
|
|
825
|
+
* @param formatString - Format string using Day.js syntax (e.g., "YYYY-MM-DD HH:mm:ss")
|
|
826
|
+
* @returns A StringExpr containing the formatted datetime
|
|
827
|
+
*
|
|
828
|
+
* @remarks
|
|
829
|
+
* The format string is parsed at compile time into structured tokens.
|
|
830
|
+
* Use backslash to escape characters: `\Y` produces literal "Y".
|
|
831
|
+
*
|
|
832
|
+
* Supported tokens:
|
|
833
|
+
* - Year: YYYY (4-digit), YY (2-digit)
|
|
834
|
+
* - Month: M (1-12), MM (01-12), MMM (Jan), MMMM (January)
|
|
835
|
+
* - Day: D (1-31), DD (01-31)
|
|
836
|
+
* - Weekday: dd (Su), ddd (Sun), dddd (Sunday)
|
|
837
|
+
* - Hour 24h: H (0-23), HH (00-23)
|
|
838
|
+
* - Hour 12h: h (1-12), hh (01-12)
|
|
839
|
+
* - Minute: m (0-59), mm (00-59)
|
|
840
|
+
* - Second: s (0-59), ss (00-59)
|
|
841
|
+
* - Millisecond: SSS (000-999)
|
|
842
|
+
* - AM/PM: A (AM/PM), a (am/pm)
|
|
843
|
+
*
|
|
844
|
+
* @example
|
|
845
|
+
* ```ts
|
|
846
|
+
* const date = fromComponents(2025n, 1n, 15n, 14n, 30n, 45n);
|
|
847
|
+
* date.printFormatted("YYYY-MM-DD HH:mm:ss");
|
|
848
|
+
* // Returns: "2025-01-15 14:30:45"
|
|
849
|
+
* ```
|
|
850
|
+
*
|
|
851
|
+
* @example
|
|
852
|
+
* ```ts
|
|
853
|
+
* const date = fromComponents(2025n, 1n, 15n, 14n, 30n);
|
|
854
|
+
* date.printFormatted("MMMM D, YYYY \\a\\t h:mm A");
|
|
855
|
+
* // Returns: "January 15, 2025 at 2:30 PM"
|
|
856
|
+
* ```
|
|
857
|
+
*/
|
|
858
|
+
printFormatted(formatString) {
|
|
859
|
+
// Parse format string at compile time to tokens
|
|
860
|
+
const tokens = tokenizeDateTimeFormat(formatString);
|
|
861
|
+
// Create a literal array of format tokens
|
|
862
|
+
const tokensArray = Expr.from(tokens, ArrayType(DateTimeFormatTokenType));
|
|
863
|
+
return this[FactorySymbol]({
|
|
864
|
+
ast_type: "Builtin",
|
|
865
|
+
type: StringType,
|
|
866
|
+
location: get_location(2),
|
|
867
|
+
builtin: "DateTimePrintFormat",
|
|
868
|
+
type_parameters: [],
|
|
869
|
+
arguments: [this[AstSymbol], tokensArray[AstSymbol]],
|
|
870
|
+
});
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
export function fromEpochMilliseconds(milliseconds) {
|
|
874
|
+
const millis = typeof milliseconds === 'bigint' ? Expr.from(milliseconds, IntegerType) : milliseconds;
|
|
875
|
+
if (millis[TypeSymbol] !== IntegerType) {
|
|
876
|
+
throw new Error("Expected integer or bigint");
|
|
877
|
+
}
|
|
878
|
+
return Expr.fromAst({
|
|
879
|
+
ast_type: "Builtin",
|
|
880
|
+
type: DateTimeType,
|
|
881
|
+
location: get_location(2),
|
|
882
|
+
builtin: "DateTimeFromEpochMilliseconds",
|
|
883
|
+
type_parameters: [],
|
|
884
|
+
arguments: [millis[AstSymbol]],
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
;
|
|
888
|
+
export function fromComponents(year, month = 1n, day = 1n, hour = 0n, minute = 0n, second = 0n, millisecond = 0n) {
|
|
889
|
+
const y = typeof year === 'bigint' ? Expr.from(year, IntegerType) : year;
|
|
890
|
+
const mo = typeof month === 'bigint' ? Expr.from(month, IntegerType) : month;
|
|
891
|
+
const d = typeof day === 'bigint' ? Expr.from(day, IntegerType) : day;
|
|
892
|
+
const h = typeof hour === 'bigint' ? Expr.from(hour, IntegerType) : hour;
|
|
893
|
+
const mi = typeof minute === 'bigint' ? Expr.from(minute, IntegerType) : minute;
|
|
894
|
+
const s = typeof second === 'bigint' ? Expr.from(second, IntegerType) : second;
|
|
895
|
+
const ms = typeof millisecond === 'bigint' ? Expr.from(millisecond, IntegerType) : millisecond;
|
|
896
|
+
if (y[TypeSymbol] !== IntegerType || mo[TypeSymbol] !== IntegerType || d[TypeSymbol] !== IntegerType ||
|
|
897
|
+
h[TypeSymbol] !== IntegerType || mi[TypeSymbol] !== IntegerType || s[TypeSymbol] !== IntegerType ||
|
|
898
|
+
ms[TypeSymbol] !== IntegerType) {
|
|
899
|
+
throw new Error("Expected integer or bigint for all components");
|
|
900
|
+
}
|
|
901
|
+
return Expr.fromAst({
|
|
902
|
+
ast_type: "Builtin",
|
|
903
|
+
type: DateTimeType,
|
|
904
|
+
location: get_location(2),
|
|
905
|
+
builtin: "DateTimeFromComponents",
|
|
906
|
+
type_parameters: [],
|
|
907
|
+
arguments: [y[AstSymbol], mo[AstSymbol], d[AstSymbol], h[AstSymbol], mi[AstSymbol], s[AstSymbol], ms[AstSymbol]],
|
|
908
|
+
});
|
|
909
|
+
}
|
|
910
|
+
;
|
|
911
|
+
/**
|
|
912
|
+
* Parse a string into a DateTime using Day.js-style format tokens.
|
|
913
|
+
*
|
|
914
|
+
* @param input - The string to parse (literal string or StringExpr)
|
|
915
|
+
* @param formatString - Format string using Day.js syntax (e.g., "YYYY-MM-DD HH:mm:ss")
|
|
916
|
+
* @returns A DateTimeExpr containing the parsed datetime
|
|
917
|
+
* @throws {EastError} At runtime if the string doesn't match the format or contains invalid values
|
|
918
|
+
*
|
|
919
|
+
* @remarks
|
|
920
|
+
* The format string is parsed at compile time into structured tokens.
|
|
921
|
+
* Use backslash to escape characters: `\Y` produces literal "Y".
|
|
922
|
+
* All parsing is done in UTC (naive datetime).
|
|
923
|
+
*
|
|
924
|
+
* Supported tokens:
|
|
925
|
+
* - Year: YYYY (4-digit), YY (2-digit, 00-99 → 2000-2099)
|
|
926
|
+
* - Month: M (1-12), MM (01-12), MMM (Jan), MMMM (January) - case insensitive
|
|
927
|
+
* - Day: D (1-31), DD (01-31)
|
|
928
|
+
* - Weekday: dd (Su), ddd (Sun), dddd (Sunday) - consumed but not validated
|
|
929
|
+
* - Hour 24h: H (0-23), HH (00-23)
|
|
930
|
+
* - Hour 12h: h (1-12), hh (01-12) - requires A/a token
|
|
931
|
+
* - Minute: m (0-59), mm (00-59)
|
|
932
|
+
* - Second: s (0-59), ss (00-59)
|
|
933
|
+
* - Millisecond: SSS (000-999)
|
|
934
|
+
* - AM/PM: A (AM/PM), a (am/pm) - case insensitive
|
|
935
|
+
*
|
|
936
|
+
* @example
|
|
937
|
+
* ```ts
|
|
938
|
+
* // Parse ISO 8601 datetime
|
|
939
|
+
* const date = parseFormatted("2025-01-15 14:30:45", "YYYY-MM-DD HH:mm:ss");
|
|
940
|
+
* ```
|
|
941
|
+
*
|
|
942
|
+
* @example
|
|
943
|
+
* ```ts
|
|
944
|
+
* // Parse with month names and 12-hour time
|
|
945
|
+
* const date = parseFormatted("January 15, 2025 at 2:30 PM", "MMMM D, YYYY \\a\\t h:mm A");
|
|
946
|
+
* ```
|
|
947
|
+
*
|
|
948
|
+
* @example
|
|
949
|
+
* ```ts
|
|
950
|
+
* // Parse runtime string expression
|
|
951
|
+
* const userInput = Expr.from(getUserInput(), StringType);
|
|
952
|
+
* const date = parseFormatted(userInput, "MM/DD/YYYY");
|
|
953
|
+
* ```
|
|
954
|
+
*/
|
|
955
|
+
export function parseFormatted(input, formatString) {
|
|
956
|
+
// Convert input to StringExpr if it's a literal string
|
|
957
|
+
const inputExpr = typeof input === 'string' ? Expr.from(input, StringType) : input;
|
|
958
|
+
if (inputExpr[TypeSymbol] !== StringType) {
|
|
959
|
+
throw new Error("Expected string or StringExpr");
|
|
960
|
+
}
|
|
961
|
+
// Parse format string at compile time to tokens
|
|
962
|
+
const tokens = tokenizeDateTimeFormat(formatString);
|
|
963
|
+
// Validate format tokens at compile time for early error detection
|
|
964
|
+
const validation = validateDateTimeFormatTokens(tokens);
|
|
965
|
+
if (!validation.valid) {
|
|
966
|
+
throw new Error(`Invalid datetime format string "${formatString}": ${validation.error}`);
|
|
967
|
+
}
|
|
968
|
+
// Create a literal array of format tokens
|
|
969
|
+
const tokensArray = Expr.from(tokens, ArrayType(DateTimeFormatTokenType));
|
|
970
|
+
return Expr.fromAst({
|
|
971
|
+
ast_type: "Builtin",
|
|
972
|
+
type: DateTimeType,
|
|
973
|
+
location: get_location(2),
|
|
974
|
+
builtin: "DateTimeParseFormat",
|
|
975
|
+
type_parameters: [],
|
|
976
|
+
arguments: [inputExpr[AstSymbol], tokensArray[AstSymbol]],
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
;
|
|
980
|
+
//# sourceMappingURL=datetime.js.map
|