MCPcopy Index your code
hub / github.com/adobe/react-spectrum / parseZonedDateTime

Function parseZonedDateTime

packages/@internationalized/date/src/string.ts:110–159  ·  view source on GitHub ↗
(value: string, disambiguation?: Disambiguation)

Source from the content-addressed store, hash-verified

108 * `disambiguation` parameter.
109 */
110export function parseZonedDateTime(value: string, disambiguation?: Disambiguation): ZonedDateTime {
111 let m = value.match(ZONED_DATE_TIME_RE);
112 if (!m) {
113 throw new Error('Invalid ISO 8601 date time string: ' + value);
114 }
115
116 let year = parseNumber(m[1], -9999, 9999);
117 let era = year < 1 ? 'BC' : 'AD';
118
119 let date: Mutable<ZonedDateTime> = new ZonedDateTime(
120 era,
121 year < 1 ? -year + 1 : year,
122 parseNumber(m[2], 1, 12),
123 1,
124 m[11],
125 0,
126 m[4] ? parseNumber(m[4], 0, 23) : 0,
127 m[5] ? parseNumber(m[5], 0, 59) : 0,
128 m[6] ? parseNumber(m[6], 0, 59) : 0,
129 m[7] ? parseNumber(m[7], 0, Infinity) * 1000 : 0
130 );
131
132 date.day = parseNumber(m[3], 0, date.calendar.getDaysInMonth(date));
133
134 let plainDateTime = toCalendarDateTime(date as ZonedDateTime);
135
136 let ms: number;
137 if (m[8]) {
138 let hourOffset = parseNumber(m[8], -23, 23);
139 date.offset =
140 Math.sign(hourOffset) *
141 (Math.abs(hourOffset) * 60 * 60 * 1000 +
142 parseNumber(m[9] ?? '0', 0, 59) * 60 * 1000 +
143 parseNumber(m[10] ?? '0', 0, 59) * 1000);
144 ms = epochFromDate(date as ZonedDateTime) - date.offset;
145
146 // Validate offset against parsed date.
147 let absolutes = possibleAbsolutes(plainDateTime, date.timeZone);
148 if (!absolutes.includes(ms)) {
149 throw new Error(
150 `Offset ${offsetToString(date.offset)} is invalid for ${dateTimeToString(date)} in ${date.timeZone}`
151 );
152 }
153 } else {
154 // Convert to absolute and back to fix invalid times due to DST.
155 ms = toAbsolute(toCalendarDateTime(plainDateTime), date.timeZone, disambiguation);
156 }
157
158 return fromAbsolute(ms, date.timeZone);
159}
160
161/**
162 * Parses an ISO 8601 date and time string with a UTC offset (e.g. "2021-11-07T07:45:00Z"

Callers 14

TimeField.test.jsFile · 0.90
DatePicker.test.jsFile · 0.90
CalendarWithZonedTimeFunction · 0.90
ZonedFunction · 0.90
ZonedPlaceholderFunction · 0.90

Calls 9

toCalendarDateTimeFunction · 0.90
epochFromDateFunction · 0.90
possibleAbsolutesFunction · 0.90
toAbsoluteFunction · 0.90
fromAbsoluteFunction · 0.90
parseNumberFunction · 0.85
offsetToStringFunction · 0.85
dateTimeToStringFunction · 0.85
getDaysInMonthMethod · 0.65

Tested by

no test coverage detected