diff --git a/package-lock.json b/package-lock.json index adf854b..af6475e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,8 @@ "dependencies": { "ask-sdk-core": "^2.14.0", "ask-sdk-model": "^1.86.0", - "kollavarsham": "^2.5.4" + "kollavarsham": "^2.5.4", + "luxon": "^3.7.2" }, "devDependencies": { "ask-sdk-test": "^2.7.42", @@ -1999,6 +2000,15 @@ "get-func-name": "^2.0.1" } }, + "node_modules/luxon": { + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.7.2.tgz", + "integrity": "sha512-vtEhXh/gNjI9Yg1u4jX/0YVPMvxzHuGgCm6tC5kZyb08yjGWGnqAjGJvcXbqQR2P3MyMEFnRbpcdFS6PBcLqew==", + "license": "MIT", + "engines": { + "node": ">=12" + } + }, "node_modules/math-intrinsics": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", diff --git a/package.json b/package.json index 5ab3ad2..e9a6e49 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "dependencies": { "ask-sdk-core": "^2.14.0", "ask-sdk-model": "^1.86.0", - "kollavarsham": "^2.5.4" + "kollavarsham": "^2.5.4", + "luxon": "^3.7.2" }, "devDependencies": { "ask-sdk-test": "^2.7.42", diff --git a/src/calendar.mjs b/src/calendar.mjs index 2fef6a5..7938bc7 100644 --- a/src/calendar.mjs +++ b/src/calendar.mjs @@ -1,4 +1,5 @@ import { Kollavarsham } from 'kollavarsham'; +import { DateTime } from 'luxon'; const kollavarsham = new Kollavarsham({ system: 'SuryaSiddhanta', @@ -13,7 +14,7 @@ const kollavarsham = new Kollavarsham({ * @returns {{ year: number, month: string, day: number, naksatra: string }} */ export function getMalayalamDate(gregorianDate) { - const kollavarshamDate = kollavarsham.fromGregorianDate(gregorianDate); + const kollavarshamDate = kollavarsham.fromGregorianDate(toIndianStandardTime(gregorianDate)); // Safely extract nakshatra name (handles string or object returns safely) const naksatraName = @@ -31,28 +32,20 @@ export function getMalayalamDate(gregorianDate) { /** - * @returns {Date} - Current date and time in IST (Indian Standard Time) + * Returns a standard JS Date object where the UTC year/month/day/time match + * the corresponding local year/month/day/time in Indian Standard Time (IST). + * Useful for handing off to libraries like kollavarsham. + * + * @param {Date} [date=new Date()] + * @returns {Date} */ -export function getIstDate() { - // Construct date string formatted explicitly for India timezone - const now = new Date(); - const formatter = new Intl.DateTimeFormat('en-US', { - timeZone: 'Asia/Kolkata', - year: 'numeric', - month: '2-digit', - day: '2-digit' - }); - - // Formats into MM/DD/YYYY in IST - const [{ value: month }, , { value: day }, , { value: year }] = formatter.formatToParts(now); - - // Create UTC date anchored at 0:00:00 for that IST date - // Month is 0-indexed in JavaScript Date constructor, so subtract 1. - // Year and date don't need adjustment. - return new Date(Date.UTC(year, month - 1, day, 0, 0, 0)); +export function toIndianStandardTime(date = new Date()) { + const ist = DateTime.fromJSDate(date).setZone('Asia/Kolkata'); + return new Date(Date.UTC(ist.year, ist.month - 1, ist.day, ist.hour, ist.minute, ist.second)); } + /** * Converts a number to its ordinal string representation (1st, 2nd, 3rd, 4th, etc.) * @param {number} day - The day number (1-31) diff --git a/src/index.mjs b/src/index.mjs index 4f11288..9e55b6d 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -1,5 +1,5 @@ import Alexa from 'ask-sdk-core'; -import { getMalayalamDate, getOrdinalDay, getIstDate } from './calendar.mjs'; +import { getMalayalamDate, getOrdinalDay } from './calendar.mjs'; // 1. Core Logic: Handles explicit date/star question requests const GetMalayalamDateIntentHandler = { @@ -76,7 +76,7 @@ const ErrorHandler = { // 3. Central Response Builder function generateCalendarResponse(handlerInput) { - const date = getMalayalamDate(getIstDate()); + const date = getMalayalamDate(Date()); // Property mappings for kollavarsham package: const dayNum = date.day; // Numeric date (e.g. 4)