MCPcopy Create free account
hub / github.com/adobe/react-spectrum / toAbsolute

Function toAbsolute

packages/@internationalized/date/src/conversion.ts:151–213  ·  view source on GitHub ↗
(
  date: CalendarDate | CalendarDateTime,
  timeZone: string,
  disambiguation: Disambiguation = 'compatible'
)

Source from the content-addressed store, hash-verified

149}
150
151export function toAbsolute(
152 date: CalendarDate | CalendarDateTime,
153 timeZone: string,
154 disambiguation: Disambiguation = 'compatible'
155): number {
156 let dateTime = toCalendarDateTime(date);
157
158 // Fast path: if the time zone is UTC, use native Date.
159 if (timeZone === 'UTC') {
160 return epochFromDate(dateTime);
161 }
162
163 // Fast path: if the time zone is the local timezone and disambiguation is compatible, use native Date.
164 // Skip this fast path if the local timezone was explicitly overridden via setLocalTimeZone,
165 // since native Date always uses the browser's timezone, not the overridden one.
166 if (
167 timeZone === getLocalTimeZone() &&
168 disambiguation === 'compatible' &&
169 !isLocalTimeZoneOverridden()
170 ) {
171 dateTime = toCalendar(dateTime, new GregorianCalendar());
172
173 // Don't use Date constructor here because two-digit years are interpreted in the 20th century.
174 let date = new Date();
175 let year = getExtendedYear(dateTime.era, dateTime.year);
176 date.setFullYear(year, dateTime.month - 1, dateTime.day);
177 date.setHours(dateTime.hour, dateTime.minute, dateTime.second, dateTime.millisecond);
178 return date.getTime();
179 }
180
181 let ms = epochFromDate(dateTime);
182 let offsetBefore = getTimeZoneOffset(ms - DAYMILLIS, timeZone);
183 let offsetAfter = getTimeZoneOffset(ms + DAYMILLIS, timeZone);
184 let valid = getValidWallTimes(dateTime, timeZone, ms - offsetBefore, ms - offsetAfter);
185
186 if (valid.length === 1) {
187 return valid[0];
188 }
189
190 if (valid.length > 1) {
191 switch (disambiguation) {
192 // 'compatible' means 'earlier' for "fall back" transitions
193 case 'compatible':
194 case 'earlier':
195 return valid[0];
196 case 'later':
197 return valid[valid.length - 1];
198 case 'reject':
199 throw new RangeError('Multiple possible absolute times found');
200 }
201 }
202
203 switch (disambiguation) {
204 case 'earlier':
205 return Math.min(ms - offsetBefore, ms - offsetAfter);
206 // 'compatible' means 'later' for "spring forward" transitions
207 case 'compatible':
208 case 'later':

Callers 8

addZonedFunction · 0.90
cycleZonedFunction · 0.90
setZonedFunction · 0.90
parseZonedDateTimeFunction · 0.90
getHoursInDayFunction · 0.90
conversion.test.jsFile · 0.90
toDateFunction · 0.85
toZonedFunction · 0.85

Calls 8

getLocalTimeZoneFunction · 0.90
getExtendedYearFunction · 0.90
toCalendarDateTimeFunction · 0.85
epochFromDateFunction · 0.85
toCalendarFunction · 0.85
getTimeZoneOffsetFunction · 0.85
getValidWallTimesFunction · 0.85

Tested by

no test coverage detected