@shopify/dates
Advanced tools
Comparing version 0.0.0-snapshot-20230303190004 to 0.0.0-snapshot-20250108014630
@@ -6,3 +6,2 @@ 'use strict'; | ||
exports.TimeUnit = void 0; | ||
(function (TimeUnit) { | ||
@@ -9,0 +8,0 @@ TimeUnit[TimeUnit["Second"] = 1000] = "Second"; |
@@ -6,2 +6,3 @@ 'use strict'; | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
const deprecatedTimezones = { | ||
@@ -35,2 +36,3 @@ 'America/Indiana': 'America/Indiana/Indianapolis', | ||
'Chile/EasterIsland': 'Pacific/Easter', | ||
'Europe/Kyiv': 'Europe/Kiev', | ||
Cuba: 'America/Havana', | ||
@@ -37,0 +39,0 @@ Egypt: 'Africa/Cairo', |
@@ -169,3 +169,2 @@ 'use strict'; | ||
var _Intl$DateTimeFormat$; | ||
const { | ||
@@ -180,3 +179,2 @@ type, | ||
}).formatToParts(date).find(part => part.type === type)) === null || _Intl$DateTimeFormat$ === void 0 ? void 0 : _Intl$DateTimeFormat$.value; | ||
if (timeUnit !== undefined && extraTransform) { | ||
@@ -183,0 +181,0 @@ return extraTransform(timeUnit); |
@@ -9,2 +9,3 @@ 'use strict'; | ||
var _DateTimeParts; | ||
const TWO_DIGIT_REGEX = /(\d{2})/; | ||
@@ -22,9 +23,6 @@ function getDateTimeParts(date, timeZone) { | ||
} | ||
function dateTimeCacheKey(unit) { | ||
return (date, timeZone) => `${unit}-${date.toString()}-${timeZone}`; | ||
} | ||
var Weekday; | ||
(function (Weekday) { | ||
@@ -39,3 +37,2 @@ Weekday["Monday"] = "Monday"; | ||
})(Weekday || (Weekday = {})); | ||
const weekdays = { | ||
@@ -50,11 +47,8 @@ Monday: 0, | ||
}; | ||
function isWeekday(weekday) { | ||
return Object.keys(weekdays).some(key => key === weekday); | ||
} | ||
function assertNever(message) { | ||
throw new Error(message); | ||
} | ||
function getWeekdayValue(weekday) { | ||
@@ -64,9 +58,8 @@ if (!isWeekday(weekday)) { | ||
} | ||
return weekdays[weekday]; | ||
} // eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
class DateTimeParts {} | ||
_DateTimeParts = DateTimeParts; | ||
DateTimeParts.getYear = functionEnhancers.memoize((date, timeZone) => { | ||
@@ -76,3 +69,2 @@ if (isNaN(date.valueOf())) { | ||
} | ||
const yearString = formatDate.formatDate(date, 'en', { | ||
@@ -84,7 +76,5 @@ timeZone, | ||
const year = parseInt(sanitisedYearString, 10); | ||
if (isNaN(year)) { | ||
throw new Error(`Unable to parse year: '${yearString}'`); | ||
} | ||
return year; | ||
@@ -99,7 +89,5 @@ }, dateTimeCacheKey('year')); | ||
const month = parseInt(sanitisedMonthString, 10); | ||
if (isNaN(month)) { | ||
throw new Error(`Unable to parse month: '${monthString}'`); | ||
} | ||
return month; | ||
@@ -114,7 +102,5 @@ }, dateTimeCacheKey('month')); | ||
const day = parseInt(sanitisedDayString, 10); | ||
if (isNaN(day)) { | ||
throw new Error(`Unable to parse day: '${dayString}'`); | ||
} | ||
return day; | ||
@@ -137,7 +123,5 @@ }, dateTimeCacheKey('day')); | ||
let hour = parseInt(hourString, 10); | ||
if (isNaN(hour)) { | ||
hour = DateTimeParts.getTimePartsFallback(date, timeZone).hour; | ||
hour = _DateTimeParts.getTimePartsFallback(date, timeZone).hour; | ||
} | ||
return hour; | ||
@@ -151,7 +135,5 @@ }, dateTimeCacheKey('hour')); | ||
let minute = parseInt(minuteString, 10); | ||
if (isNaN(minute)) { | ||
minute = DateTimeParts.getTimePartsFallback(date, timeZone).minute; | ||
minute = _DateTimeParts.getTimePartsFallback(date, timeZone).minute; | ||
} | ||
return minute; | ||
@@ -165,7 +147,5 @@ }, dateTimeCacheKey('minute')); | ||
let second = parseInt(secondString, 10); | ||
if (isNaN(second)) { | ||
second = DateTimeParts.getTimePartsFallback(date, timeZone).second; | ||
second = _DateTimeParts.getTimePartsFallback(date, timeZone).second; | ||
} | ||
return second; | ||
@@ -180,4 +160,5 @@ }, dateTimeCacheKey('second')); | ||
second: '2-digit' | ||
}); // In Microsoft Edge, Intl.DateTimeFormat returns invisible characters around the individual numbers | ||
}); | ||
// In Microsoft Edge, Intl.DateTimeFormat returns invisible characters around the individual numbers | ||
const [dirtyHour, dirtyMinute, dirtySecond] = timeString.split(':'); | ||
@@ -187,3 +168,2 @@ const rawHour = new RegExp(TWO_DIGIT_REGEX).exec(dirtyHour); | ||
const rawSecond = new RegExp(TWO_DIGIT_REGEX).exec(dirtySecond); | ||
if (rawHour != null && rawMinute != null && rawSecond != null) { | ||
@@ -199,3 +179,2 @@ const hour = parseInt(rawHour[1], 10); | ||
} | ||
throw new Error(`Unable to parse timeString: '${timeString}'`); | ||
@@ -202,0 +181,0 @@ }, dateTimeCacheKey('timePartsFallback')); |
@@ -12,3 +12,2 @@ 'use strict'; | ||
} | ||
function constructZonedDateFromParts(date, timeZone) { | ||
@@ -15,0 +14,0 @@ const { |
@@ -16,8 +16,7 @@ 'use strict'; | ||
const dateTimeParts = new RegExp(DATE_TIME_PARTS_REGEX).exec(dateString); | ||
if (dateTimeParts == null) { | ||
return null; | ||
} // slice the first regex part (the full match) off | ||
} | ||
// slice the first regex part (the full match) off | ||
const [year, month, day, hour, minute, second, millisecond, timeZoneOffset, sign, timeZoneHour, timeZoneMinute] = Array.from(dateTimeParts).slice(1); | ||
@@ -24,0 +23,0 @@ return { |
@@ -10,7 +10,5 @@ 'use strict'; | ||
const dateTimeParts = parseDateStringParts.parseDateStringParts(dateString); | ||
if (dateTimeParts == null) { | ||
return null; | ||
} | ||
const { | ||
@@ -39,11 +37,8 @@ year: rawYear, | ||
const utcDate = new Date(Date.UTC(year, month - 1, day, hour, minute, second, millisecond)); | ||
if (timeZoneOffset === 'Z') { | ||
return utcDate; | ||
} | ||
if (sign == null) { | ||
return applyTimeZoneOffset.applyTimeZoneOffset(utcDate, timeZone, 'UTC'); | ||
} | ||
switch (sign) { | ||
@@ -54,3 +49,2 @@ case '+': | ||
return utcDate; | ||
case '-': | ||
@@ -60,3 +54,2 @@ utcDate.setHours(utcDate.getHours() + timeZoneHour); | ||
return utcDate; | ||
default: | ||
@@ -63,0 +56,0 @@ return null; |
@@ -8,7 +8,5 @@ 'use strict'; | ||
const key = dateTimeFormatCacheKey(locales, options); | ||
if (intl.has(key)) { | ||
return intl.get(key); | ||
} | ||
const i = new Intl.DateTimeFormat(locales, options); | ||
@@ -20,23 +18,27 @@ intl.set(key, i); | ||
hour: 'numeric' | ||
}); // hourCycle to Intl.DateTimeFormatOptions was added in TS 4.2, so we could | ||
}); | ||
// hourCycle to Intl.DateTimeFormatOptions was added in TS 4.2, so we could | ||
// remove this, but that would require consumers to update to at least TS 4.2 | ||
// and be including the libs for es2020 | ||
// hourcycle is not yet added to Intl.ResolvedDateTimeFormatOptions. This seems | ||
// to be an omission in TS, see https://github.com/microsoft/TypeScript/issues/45420 | ||
const resolvedOptions = typeof browserFeatureDetectionDate.resolvedOptions === 'undefined' ? undefined : browserFeatureDetectionDate.resolvedOptions(); | ||
function formatDate(date, locales, options = {}) { | ||
const hourCycleRequired = resolvedOptions != null && options.hour12 === false && resolvedOptions.hourCycle != null; | ||
if (hourCycleRequired) { | ||
options.hour12 = undefined; | ||
options.hourCycle = 'h23'; | ||
} // Etc/GMT+12 is not supported in most browsers and there is no equivalent fallback | ||
} | ||
// Etc/GMT+12 is not supported in most browsers and there is no equivalent fallback | ||
if (options.timeZone != null && options.timeZone === 'Etc/GMT+12') { | ||
const adjustedDate = new Date(date.valueOf() - 12 * 60 * 60 * 1000); | ||
return memoizedGetDateTimeFormat(locales, { ...options, | ||
return memoizedGetDateTimeFormat(locales, { | ||
...options, | ||
timeZone: 'UTC' | ||
}).format(adjustedDate); | ||
} | ||
return memoizedGetDateTimeFormat(locales, options).format(date); | ||
@@ -43,0 +45,0 @@ } |
@@ -1,3 +0,3 @@ | ||
import { TimeUnit } from './constants'; | ||
import type { TimeUnit } from './constants'; | ||
export declare function getDateDiff(resolution: TimeUnit, date: Date, today?: Date): number; | ||
//# sourceMappingURL=get-date-diff.d.ts.map |
{ | ||
"name": "@shopify/dates", | ||
"version": "0.0.0-snapshot-20230303190004", | ||
"version": "0.0.0-snapshot-20250108014630", | ||
"license": "MIT", | ||
@@ -24,9 +24,9 @@ "description": "Lightweight date operations library", | ||
"engines": { | ||
"node": "^14.17.0 || >=16.0.0" | ||
"node": ">=18.12.0" | ||
}, | ||
"dependencies": { | ||
"@shopify/function-enhancers": "^3.0.1" | ||
"@shopify/function-enhancers": "^3.1.0" | ||
}, | ||
"devDependencies": { | ||
"@shopify/jest-dom-mocks": "^4.1.0" | ||
"@shopify/jest-dom-mocks": "^5.2.0" | ||
}, | ||
@@ -33,0 +33,0 @@ "files": [ |
# `@shopify/dates` | ||
> [!CAUTION] | ||
> | ||
> `@shopify/dates` is deprecated. | ||
> | ||
> Shopifolk, see | ||
> [Shopify/quilt-internal](https://github.com/shopify/quilt-internal) for | ||
> information on the latest packages available for use internally. | ||
[![Build Status](https://github.com/Shopify/quilt/workflows/Node-CI/badge.svg?branch=main)](https://github.com/Shopify/quilt/actions?query=workflow%3ANode-CI) | ||
@@ -4,0 +12,0 @@ [![Build Status](https://github.com/Shopify/quilt/workflows/Ruby-CI/badge.svg?branch=main)](https://github.com/Shopify/quilt/actions?query=workflow%3ARuby-CI) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
106989
1683
386