/* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| BI and PM: prmt *| (C) Copyright IBM Corp. 2002, 2013 *| *| US Government Users Restricted Rights - Use, duplication or *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp. *| *+------------------------------------------------------------------------+ */ /* PRMT_TimeHelper.js This script is used to provide helper functions for the selectTime prompt control */ //Constructor to create a TimeHelper object is empty function PRMT_TimeHelper(bRequired) { //specify whether the user must enter a date or not this.m_bRequired = bRequired; } //returns a locale formatted time for a given date // // iDisplay: determine the display // 0 = h:m:s:ms // 1 = h:m:s // 2 = h:m PRMT_TimeHelper.prototype.getTimeFormatForDisplay = function(dTime, iDisplay) { var sValue = K_PRMT_sEMPTY; if(dTime){ //format hours var iHours = dTime.getHours(); if (g_24HourClock == 'false'){ if (iHours == 0){ iHours = 12; } else if (iHours > 12) { iHours = iHours - 12; } } var sHours = iHours.toString(); if ((sHours.length == 1) && (g_hourFormatMedium == 'hh')) { sHours = '0' + sHours; } sValue += sHours; //format minutes var sMinutes = dTime.getMinutes().toString(); if ((sMinutes.length == 1) && (g_minuteFormatMedium == 'mm')) { sMinutes = '0' + sMinutes; } sValue += g_timeSeparator + sMinutes; //format seconds if (iDisplay <= 1) { var sSeconds = dTime.getSeconds().toString(); if ((sSeconds.length == 1) && (g_secondFormatMedium == 'ss')) { sSeconds = '0' + sSeconds; } sValue += g_timeSeparator + sSeconds; } //format milliseconds if (iDisplay == 0) { var sMilliseconds = dTime.getMilliseconds().toString(); if (sMilliseconds.length == 2) { sMilliseconds = '0' + sMilliseconds; } else if (sMilliseconds.length == 1) { sMilliseconds = '00' + sMilliseconds; } sValue += K_PRMT_sDOT + sMilliseconds; } if ((g_24HourClock == 'false') && (g_AMPMLocation == 'right')) { if (dTime.getHours() >= 12) { sValue = sValue + K_PRMT_sSP + g_pmString; } else { sValue = sValue + K_PRMT_sSP + g_amString; } } else if ((g_24HourClock == "false") && (g_AMPMLocation == "left")) { if (dTime.getHours() >= 12) { sValue = g_pmString + K_PRMT_sSP + sValue; }else{ sValue = g_amString + K_PRMT_sSP + sValue; } } } return sValue; }; PRMT_TimeHelper.prototype.getTimeFormatForInput = function (dTime, iDisplay) { var v_sDisplay = K_PRMT_sEMPTY; if(dTime) { var sHours = this.getTimePartWithZeroPadding(dTime.getHours().toString(), 2); var sMinutes = this.getTimePartWithZeroPadding(dTime.getMinutes().toString(), 2); v_sDisplay = sHours + K_PRMT_sCOLON + sMinutes; if(iDisplay < 2){ var sSeconds = this.getTimePartWithZeroPadding(dTime.getSeconds().toString(), 2); v_sDisplay += K_PRMT_sCOLON + sSeconds; if(iDisplay < 1){ var sMilliseconds = this.getTimePartWithZeroPadding(dTime.getMilliseconds().toString(), 3); v_sDisplay += K_PRMT_sDOT + sMilliseconds; } } } return v_sDisplay; }; //process the input and convert into the proper format PRMT_TimeHelper.prototype.getTimeFormatForSubmit = function (dTime) { var v_sValue = K_PRMT_sEMPTY; if(dTime) { //return a valid time data type //00:00:00.000 var sHours = this.getTimePartWithZeroPadding(dTime.getHours().toString(), 2); var sMinutes = this.getTimePartWithZeroPadding(dTime.getMinutes().toString(), 2); var sSeconds = this.getTimePartWithZeroPadding(dTime.getSeconds().toString(), 2); var sMilliseconds = this.getTimePartWithZeroPadding(dTime.getMilliseconds().toString(), 3); v_sValue = sHours + K_PRMT_sCOLON + sMinutes + K_PRMT_sCOLON + sSeconds + K_PRMT_sDOT + sMilliseconds; } return v_sValue; }; PRMT_TimeHelper.prototype.getTimePartWithZeroPadding = function (sTimePart, iLength){ var v_sPaddedValue = sTimePart; while(v_sPaddedValue.length < iLength){ v_sPaddedValue = '0' + v_sPaddedValue; } return v_sPaddedValue; }; PRMT_TimeHelper.prototype.getTimeValuesArray = function (sTimeString) { var v_aSplit = sTimeString.split(":"); var v_aTimeValues = new Array(); v_aTimeValues[0] = v_aSplit[0]; v_aTimeValues[1] = v_aSplit[1]; if(v_aSplit.length > 2){ var v_aSplitSecs = v_aSplit[2].split("."); v_aTimeValues[2] = v_aSplitSecs[0]; if(v_aSplitSecs.length > 1){ v_aTimeValues[3] = v_aSplit[2].split(".")[1]; } } return v_aTimeValues; }; //returns a date object for a given time PRMT_TimeHelper.prototype.dParseTime = function (sTime) { var dTime = (sTime) ? new Date(1970,0,1,0,0,0,0) : null; //set a default time if a default has been specified var sDefaultTime = (sTime) ? sTime : K_PRMT_sEMPTY; //convert from a valid date time format (XSD format and SQL) var arDateTime = sDefaultTime.split(/[T\s]/g); if (arDateTime.length == 2){ sDefaultTime = arDateTime[1]; } var arDefaultTime = sDefaultTime.split(/[:\.\,]/g); //do we have a valid date? if ((arDefaultTime.length >= 2)) { var sHours = parseInt(arDefaultTime[0],10); if (sHours != NaN && sHours <= 23 && sHours >= 0) { dTime.setHours(sHours); } else { // going to default to 12 dTime.setHours(0); } var sMinutes = parseInt(arDefaultTime[1],10); if (sMinutes != NaN && sMinutes <= 59 && sMinutes >= 0) { dTime.setMinutes(sMinutes); } else { // going to default to 0 minutes dTime.setMinutes(0); } if (arDefaultTime.length >= 3 ) { var sSeconds = parseInt(arDefaultTime[2],10); if (sSeconds != NaN && sSeconds <= 59 && sSeconds >= 0) { dTime.setSeconds(sSeconds); } else { // going to default to 0 seconds dTime.setSeconds(0); } } //milliseconds are optional if (arDefaultTime.length == 4) { var sMilliseconds = parseInt(arDefaultTime[3],10); if (sMilliseconds != NaN && sMilliseconds <= 999 && sMilliseconds >= 0) { dTime.setMilliseconds(sMilliseconds); } else { // going to default to 0 milliseconds dTime.setMilliseconds(0); } } } return dTime; }; PRMT_TimeHelper.prototype.bCheckTime = function ( sTimeValue, iDisplay ) { var v_bValid = true; var v_sValue = this.sStripNonAlphanumerics(sTimeValue).f_trim(); if( this.m_bRequired || (v_sValue !== K_PRMT_sEMPTY) ){ if(v_sValue === K_PRMT_sEMPTY){ v_bValid = false; } else { var v_aTimeValues = this.getTimeValuesArray(v_sValue); var hours = v_aTimeValues[0]; var minutes = v_aTimeValues[1]; var seconds = v_aTimeValues[2]; var milliseconds = v_aTimeValues[3]; //check time parts if ( !this.bisValidHours(hours) || !this.bisValidMinutes(minutes) || ( iDisplay <= 1 && !this.bisValidSeconds(seconds) ) || ( iDisplay == 0 && !this.bisValidMilliseconds(milliseconds) ) ) { v_bValid = false; } } } return v_bValid; }; PRMT_TimeHelper.prototype.bisValidHours = function (hours) { var iHours = parseInt(hours,10); return (bNumbersOnly(hours) && iHours >= MIN_HOURS && iHours <= MAX_HOURS); }; PRMT_TimeHelper.prototype.bisValidMinutes = function (min) { var iMinutes = parseInt(min,10); return ( bNumbersOnly(min) && iMinutes >= MIN_MINUTES && iMinutes <= MAX_MINUTES ); }; PRMT_TimeHelper.prototype.bisValidSeconds = function (sec) { var iSeconds = parseInt(sec,10); return ( bNumbersOnly(sec) && iSeconds >= MIN_SECONDS && iSeconds <= MAX_SECONDS ); }; PRMT_TimeHelper.prototype.bisValidMilliseconds = function (msec) { var iMilliseconds = parseInt(msec,10); return ( bNumbersOnly(msec) && iMilliseconds >= MIN_MILLISECONDS && iMilliseconds <= MAX_MILLISECONDS ); }; PRMT_TimeHelper.prototype.sStripNonAlphanumerics = function (sString) { var charDelimiters = /[\s,-]/g; var arTestDateStrip = sString.split(charDelimiters); var sTestDateStrip = arTestDateStrip.join(K_PRMT_sSP); return sTestDateStrip; }; /* Messages */ // declare message strings var g_amString = "AM"; var g_pmString = "PM"; var g_mediumFormat = "MMM d, yyyy h:mm:ss a"; var g_shortFormat = "M/d/yy h:mm a"; var g_hourFormatMedium = "h"; var g_minuteFormatMedium = "mm"; var g_secondFormatMedium = "ss"; var g_hourFormatShort = "h"; var g_minuteFormatShort = "mm"; var g_secondFormatShort = K_PRMT_sEMPTY; var g_timeSeparator = K_PRMT_sCOLON; var g_AMPMLocation = "right"; var g_24HourClock = "false"; /* Constants */ var MAX_HOURS = 23; var MAX_MINUTES = 59; var MAX_SECONDS = 59; var MAX_MILLISECONDS = 999; var MIN_HOURS = 0; var MIN_MINUTES = 0; var MIN_SECONDS = 0; var MIN_MILLISECONDS = 0;