-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstringify.ts
More file actions
229 lines (202 loc) · 5.68 KB
/
stringify.ts
File metadata and controls
229 lines (202 loc) · 5.68 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
// stringify.ts
// Convert AST back to temporal string representation
import type {
AnnotationAst,
DateAst,
DateTimeAst,
DurationAst,
OffsetAst,
RangeAst,
TemporalAst,
TimeAst,
TimeZoneAst,
} from './parser-types.js';
/**
* Convert a temporal AST back to its string representation.
*
* @param ast - The AST to stringify
* @returns ISO 8601 / IXDTF formatted string
*
* @example
* ```typescript
* const ast = parseTemporal('2025-01-12T10:00:00+08:00');
* const str = stringifyTemporal(ast);
* // '2025-01-12T10:00:00+08:00'
* ```
*/
export function stringifyTemporal(ast: TemporalAst): string {
if (ast.kind === 'Range') {
return stringifyRange(ast);
}
if (ast.kind === 'Duration') {
return stringifyDuration(ast);
}
return stringifyDateTime(ast);
}
/**
* Stringify a date AST to ISO 8601 format.
*
* @param date - The date AST
* @returns Date string (YYYY, YYYY-MM, or YYYY-MM-DD)
*/
export function stringifyDate(date: DateAst): string {
// Handle negative years (BC dates) - ISO 8601 uses negative years
// Year 0 = 1 BC, Year -1 = 2 BC, etc.
let yearStr: string;
if (date.year < 0) {
// For negative years, pad the absolute value and prepend the minus sign
yearStr = '-' + Math.abs(date.year).toString().padStart(4, '0');
} else {
yearStr = date.year.toString().padStart(4, '0');
}
const parts: string[] = [yearStr];
if (date.month != null) {
parts.push(date.month.toString().padStart(2, '0'));
if (date.day != null) {
parts.push(date.day.toString().padStart(2, '0'));
}
}
return parts.join('-');
}
/**
* Stringify a time AST to ISO 8601 format.
*
* @param time - The time AST
* @returns Time string (HH:MM, HH:MM:SS, or HH:MM:SS.fff)
*/
export function stringifyTime(time: TimeAst): string {
const parts: string[] = [
time.hour.toString().padStart(2, '0'),
time.minute.toString().padStart(2, '0'),
];
if (time.second != null) {
let secondStr = time.second.toString().padStart(2, '0');
if (time.fraction != null) {
secondStr += `.${time.fraction}`;
}
parts.push(secondStr);
}
return parts.join(':');
}
/**
* Stringify a timezone offset AST to canonical format.
*
* @param offset - The offset AST
* @returns Offset string in canonical format (Z or ±HH:MM)
*/
export function stringifyOffset(offset: OffsetAst): string {
if (offset.kind === 'UtcOffset') {
return 'Z';
}
// Return canonical format: +HH:MM or -HH:MM
const hours = offset.hours.toString().padStart(2, '0');
const minutes = offset.minutes.toString().padStart(2, '0');
return `${offset.sign}${hours}:${minutes}`;
}
/**
* Stringify a timezone AST.
*
* @param timeZone - The timezone AST
* @returns Timezone string [Asia/Singapore] or [!Asia/Singapore]
*/
export function stringifyTimeZone(timeZone: TimeZoneAst): string {
const id = timeZone.critical ? `!${timeZone.id}` : timeZone.id;
return `[${id}]`;
}
/**
* Stringify an annotation AST.
*
* @param annotation - The annotation AST
* @returns Annotation string [u-ca=gregory] or [!u-ca=gregory]
*/
export function stringifyAnnotation(annotation: AnnotationAst): string {
return `[${annotation.raw}]`;
}
/**
* Stringify a datetime AST to ISO 8601 / IXDTF format.
*
* @param dateTime - The datetime AST
* @returns DateTime string with optional time, offset, timezone, and annotations
*/
export function stringifyDateTime(dateTime: DateTimeAst): string {
let result = stringifyDate(dateTime.date);
if (dateTime.time) {
result += `T${stringifyTime(dateTime.time)}`;
}
if (dateTime.offset) {
result += stringifyOffset(dateTime.offset);
}
if (dateTime.timeZone) {
result += stringifyTimeZone(dateTime.timeZone);
}
for (const annotation of dateTime.annotations) {
result += stringifyAnnotation(annotation);
}
return result;
}
/**
* Stringify a duration AST to ISO 8601 format.
*
* @param duration - The duration AST
* @returns Duration string (P1Y2M3DT4H5M6S)
*/
export function stringifyDuration(duration: DurationAst): string {
// Always reconstruct from components to ensure normalization
let result = 'P';
// Date part - include component if defined (even if zero)
if (duration.years != null) {
result += `${duration.years}Y`;
}
if (duration.months != null) {
result += `${duration.months}M`;
}
if (duration.weeks != null) {
result += `${duration.weeks}W`;
}
if (duration.days != null) {
result += `${duration.days}D`;
}
// Time part - add T separator if any time component is defined
const hasTimePart =
duration.hours != null || duration.minutes != null || duration.seconds != null;
if (hasTimePart) {
result += 'T';
if (duration.hours != null) {
result += `${duration.hours}H`;
}
if (duration.minutes != null) {
result += `${duration.minutes}M`;
}
if (duration.seconds != null) {
result += `${duration.seconds}`;
if (duration.secondsFraction != null && duration.secondsFraction.length > 0) {
result += `.${duration.secondsFraction}`;
}
result += 'S';
}
}
// Annotations
for (const annotation of duration.annotations) {
result += stringifyAnnotation(annotation);
}
return result;
}
/**
* Stringify a range AST to ISO 8601 format.
*
* @param range - The range AST
* @returns Range string (start/end, /end, or start/)
*/
export function stringifyRange(range: RangeAst): string {
const start = range.start
? range.start.kind === 'Duration'
? stringifyDuration(range.start)
: stringifyDateTime(range.start)
: '';
const end = range.end
? range.end.kind === 'Duration'
? stringifyDuration(range.end)
: stringifyDateTime(range.end)
: '';
return `${start}/${end}`;
}