...
Code Block | ||
---|---|---|
| ||
function getTimeZoneOffsetisLogicalIncompleteDatetime(timeZoneIdvalue) { var// timezoneOffsetsSplit =the {datetime into date and time parts "US/Pacific": -7 * 60 * 60 * 1000, "US/Mountain": -6 * 60 * 60 * 1000, "US/Central": -5 * 60 * 60 * 1000, "US/Eastern": -4 * 60 * 60 * 1000, "Europe/London": 0 * 60 * 60 * 1000, var parts = value.split('T'); var datePart = parts[0].split('-'); var timePart = parts.length > 1 ? parts[1].split(':') : []; // Define helper function to check if an array has any missing parts function hasMissingParts(arr) { return arr.some(function (part) { "Europe/Berlin": 1 * 60return *part 60=== *null 1000, || part.trim() === ''; "Europe/Paris": 1 * 60 * 60 * 1000, }); } "Europe//Madrid": 1Check *for 60logical *date 60parts * 1000, if (datePart.length === "Europe/Rome": 1 * 60 * 60 * 1000,3) { "Europe/Warsaw": 1 * 60 * 60 * 1000,// Year should not be missing if "Europe/Moscow": 3 * 60 * 60 * 1000,(datePart[0].trim() === '') { "UTC": 0 * 60 * 60 * 1000, "GMT": 0 * 60 * 60 * 1000,customErrorMessage('Year is missing, but month or day is present.'); return }false; return timezoneOffsets[timeZoneId] || 0; } function normalizeIncompleteDatetime(value, timeZoneId) { logger("Original value: " + value); // SplitIf themonth dateis andpresent, timeyear partsshould be present var parts = value.split('T'); varif (datePart = parts[01].splittrim('-'); // Default year, month, and day var year = datePart[0] ? parseInt(datePart[0], 10) : 0; var month = datePart[1] !== '' && datePart[10].trim() !=== '' ? parseInt(datePart[1], 10) -{ 1 : 0; // Default to January (month = 0) customErrorMessage('Month is present, varbut dayyear = datePart[2] && datePart[2].trim() !== '' ? parseInt(datePart[2], 10) : 1is missing.'); // Default to 1st of thereturn monthfalse; // Split the time} part and pad missing parts with defaults // If varday timePartis = parts.length > 1 ? parts[1].split(':') : [];present, both year and month should be present var hour = timePart[0] && timePart[0 if (datePart[2].trim() !== '' ?&& parseInt(timePartdatePart[0], 10) : 0; var minute = timePart[1] && timePart.trim() === '' || datePart[1].trim() !=== '')) ? parseInt(timePart[1], 10) : 0;{ var second = timePart.length > 2 && timePart[2].trim() !== '' ? parseInt(timePart[2], 10) : 0 customErrorMessage('Day is present, but year or month is missing.'); // Create the Date object directly in thereturn localfalse; timezone var normalizedDatetime = new Date(year, month, day, hour, minute, second);} } // Check iffor thelogical datetime is validparts if (isNaN(normalizedDatetime.getTime())) { logger("Error: Invalid Date created from input value."); timePart.length > 0) { customErrorMessage('The datetime "' + value + '" could // Hour should not be interpretedmissing asif aminutes valid date.'); or seconds are present return null; if (timePart[0].trim() === '' } // Adjust the datetime according to the timezone offset var timezoneOffset = getTimeZoneOffset(timeZoneId); normalizedDatetime = new Date(normalizedDatetime.getTime() - timezoneOffset); // Corrected: Subtracting the offset to convert from MT to UTC logger("Date object adjusted for " + timeZoneId + ": " + normalizedDatetime);&& (timePart[1].trim() !== '' || timePart.length > 2 && timePart[2].trim() !== '')) { customErrorMessage('Hour is missing, but minute or second is present.'); return normalizedDatetimefalse; } function isNotInFuture(datetimeValue, timeZoneId) { } logger("Starting isNotInFuture check..."); // If Normalizeminutes theare incompletepresent, datetimehour valueshould directlybe inpresent the timezone var normalizedDatetime = normalizeIncompleteDatetime(datetimeValue, timeZoneId); if (!normalizedDatetime) {if (timePart[1].trim() !== '' && timePart[0].trim() === '') { // Return false if we couldn't create a valid date customErrorMessage('Minute is present, but hour is missing.'); return false; } logger("Normalized datetime for comparison: " + normalizedDatetime);} // GetIf theseconds currentare timepresent, adjustedboth tohour theand localminute timezoneshould be present var now = new Date(); if logger("Current local time: " + now); // Compare the datetime if (normalizedDatetime.getTime() >= now.getTime(timePart.length > 2 && timePart[2].trim() !== '' && (timePart[0].trim() === '' || timePart[1].trim() === '')) { var errorMessage = 'The datetime "' + datetimeValue + '" customErrorMessage('Second is inpresent, thebut futurehour or exactly at the current time, which is not allowed.'minute is missing.'); logger("Error: " + errorMessage); return false; customErrorMessage(errorMessage); } return} false; }// If all checks pass, the logger("Datetimedatetime is not in the future.");logically consistent return true; } // Extract the timezone from formJson var timeZoneId = formJson.form.subject.volunteer.site.timeZoneId; logger("TimeZone ID: " + timeZoneId); // RunExecute the check againston the provided item value logger("Item value: " + itemJson.item.value);'s value var isValid = isNotInFutureisLogicalIncompleteDatetime(itemJson.item.value, timeZoneId); logger("Validation result: " + isValid); return isValid; |
Edit Check Walkthrough
...