diff --git a/src/index.ts b/src/index.ts index 3a39cb6..c0a872f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -124,70 +124,6 @@ const formatDate = (date: Date, locale:string): string => { return formattedDate } - -function convertUTCDateToTimezone(utcDate: Date, timezoneId: string): Date { - // Check if the date is in UTC format by examining its string representation - const utcDateString = utcDate.toISOString(); - if (!utcDateString.endsWith('Z') && !utcDateString.includes('+00:00') && !utcDateString.includes('-00:00')) { - // The date is not in UTC format, return the input date unchanged - console.error('\x1b[31m'," - The provided date is not in UTC format, return the provided date", '\x1b[0m') - return utcDate; - } - - // Get the UTC time parts from the Date object - const utcYear = utcDate.getUTCFullYear(); - const utcMonth = utcDate.getUTCMonth(); - const utcDay = utcDate.getUTCDate(); - const utcHour = utcDate.getUTCHours(); - const utcMinute = utcDate.getUTCMinutes(); - const utcSecond = utcDate.getUTCSeconds(); - const utcMillisecond = utcDate.getUTCMilliseconds(); - - // Construct an ISO string for the UTC date - const isoDateString = new Date(Date.UTC( - utcYear, - utcMonth, - utcDay, - utcHour, - utcMinute, - utcSecond, - utcMillisecond - )).toISOString(); - - // Use Intl.DateTimeFormat to format the UTC date to the target timezone - const options = { - timeZone: timezoneId, - year: 'numeric', - month: 'numeric', - day: 'numeric', - hour: 'numeric', - minute: 'numeric', - second: 'numeric', - fractionalSecondDigits: 3, // Include milliseconds - hour12: false - }; - const formatter = new Intl.DateTimeFormat('en-US', options); - const parts = formatter.formatToParts(new Date(isoDateString)); - - // Extract the relevant parts from the formatted date - const year = parseInt(parts.find(part => part.type === 'year')?.value || '0', 10); - const month = parseInt(parts.find(part => part.type === 'month')?.value || '0', 10) - 1; // JavaScript months are 0-based - const day = parseInt(parts.find(part => part.type === 'day')?.value || '0', 10); - const hour = parseInt(parts.find(part => part.type === 'hour')?.value || '0', 10); - const minute = parseInt(parts.find(part => part.type === 'minute')?.value || '0', 10); - const second = parseInt(parts.find(part => part.type === 'second')?.value || '0', 10); - const millisecond = parseInt(parts.find(part => part.type === 'fractionalSecond')?.value || '0', 10); - - // Create a new Date object in the target timezone - const targetDate = new Date(year, month, day, hour, minute, second, millisecond); - - // Adjust for the timezone offset manually because JavaScript Date uses local time by default - const offsetInMinutes = targetDate.getTimezoneOffset(); - const adjustedTime = targetDate.getTime() - offsetInMinutes * 60000; - - return new Date(adjustedTime); -} - const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) const getProgress = async (downloadPath: string): Promise => {