1 /*global BytePushers*/ 2 (function (BytePushers) { 3 'use strict'; 4 BytePushers = BytePushers || {}; 5 BytePushers.converters = BytePushers.namespace("software.bytepushers.utils.converters"); 6 BytePushers.converters.DateConverter = BytePushers.namespace("software.bytepushers.utils.converters.DateConverter"); 7 BytePushers.converters.DateConverter.MMDDYYYY_DATE_FORMAT = 0; 8 BytePushers.converters.DateConverter.MMMDDYYYY_DATE_FORMAT = 1; 9 BytePushers.converters.DateConverter.YYYYMMDDThhmmsssTZD_DATE_FORMAT = 2; 10 BytePushers.converters.DateConverter.MDDYYYY_DATE_FORMAT = 3; 11 BytePushers.converters.DateConverter.YYYYMMDD_DATE_FORMAT = 4; 12 BytePushers.converters.DateConverter.convertToDate_MDDYYYY = function (d) { 13 var month, day, year, date = new Date(); 14 if (d.length !== 7) { 15 throw new BytePushers.exceptions.InvalidParameterException("Date String: " + d + " should be in format MDDYYYY."); 16 } 17 if (BytePushers.NumberUtility.isNotANumber(d)) { 18 throw new BytePushers.exceptions.InvalidParameterException("Date String: " + d + " must be numeric."); 19 } 20 month = Number(d.substring(0, 2)); 21 day = Number(d.substring(2, 4)); 22 year = Number(d.substring(4)); 23 date.setFullYear(year, month, day); 24 date.setHours(0, 0, 0, 0); 25 return date; 26 }; 27 BytePushers.converters.DateConverter.convertToDate_MMDDYYYY = function (d) { 28 var month, day, year, date = new Date(); 29 if (d.length !== 8) { 30 throw new BytePushers.exceptions.InvalidParameterException("Date String: " + d + " should be in format MMDDYYYY."); 31 } 32 if (BytePushers.NumberUtility.isNotANumber(d)) { 33 throw new BytePushers.exceptions.InvalidParameterException("Date String: " + d + " must be numeric."); 34 } 35 month = Number(d.substring(0, 2)) - 1; 36 day = Number(d.substring(2, 4)); 37 year = Number(d.substring(4)); 38 date.setFullYear(year, month, day); 39 date.setHours(0, 0, 0, 0); 40 return date; 41 }; 42 BytePushers.converters.DateConverter.convertToDate_MMMDDYYYY = function (d) { 43 var month, day, year, date = new Date(); 44 if (d.length !== 9) { 45 throw new BytePushers.exceptions.InvalidParameterException("Date String: " + d + " should be in format MMMDDYYYY."); 46 } 47 if (BytePushers.NumberUtility.isNotANumber(d.substring(3))) { 48 throw new BytePushers.exceptions.InvalidParameterException("Date String: " + d + " must be numeric."); 49 } 50 month = Number(BytePushers.models.Month.getMonthIndex(d.substring(0, 3))); 51 day = Number(d.substring(3, 5)); 52 year = Number(d.substring(5)); 53 date.setFullYear(year, month, day); 54 date.setHours(0, 0, 0, 0); 55 return date; 56 }; 57 BytePushers.converters.DateConverter.convertToDate_YYYYMMDD = function (d) { 58 var month, day, year, date = new Date(); 59 if (d.length !== 8) { 60 throw new BytePushers.exceptions.InvalidParameterException("Date String: " + d + " should be in format YYYYMMDD."); 61 } 62 if (BytePushers.NumberUtility.isNotANumber(d.substring(0, 4))) { 63 throw new BytePushers.exceptions.InvalidParameterException("Date String: " + d.substring(0, 4) + " must be numeric."); 64 } 65 if (BytePushers.NumberUtility.isNotANumber(d.substring(4, 6))) { 66 throw new BytePushers.exceptions.InvalidParameterException("Date String: " + d.substring(4, 6) + " must be numeric."); 67 } 68 if (BytePushers.NumberUtility.isNotANumber(d.substring(6))) { 69 throw new BytePushers.exceptions.InvalidParameterException("Date String: " + d.substring(6) + " must be numeric."); 70 } 71 year = Number(d.substring(0, 4)); 72 month = Number(d.substring(4, 6)) - 1; 73 day = Number(d.substring(6)); 74 date.setFullYear(year, month, day); 75 date.setHours(0, 0, 0, 0); 76 return date; 77 }; 78 BytePushers.converters.DateConverter.convertToDate_YYYYMMDDThhmmsssTZD = function (iso8601DateString) { 79 return BytePushers.converters.DateConverter.convertToISO8601Date(iso8601DateString); 80 }; 81 BytePushers.converters.DateConverter.convertToString_YYYYMMDD = function (d, delimeter) { 82 delimeter = (Object.isDefined(delimeter)) ? delimeter : ""; 83 if (!Object.isDate(d)) { 84 throw new Error("parameter d must be a Date Object."); 85 } 86 87 return d.getFullYear() + delimeter + BytePushers.NumberUtility.padLeft(d.getMonth() + 1, 1) + delimeter + d.getDate(); 88 }; 89 BytePushers.converters.DateConverter.convertToString_YYYYMMDDThhmmsssTZD = function (d, delimeter) { 90 delimeter = (Object.isDefined(delimeter)) ? delimeter : ""; 91 if (!Object.isDate(d)) { 92 throw new Error("parameter d must be a Date Object."); 93 } 94 95 return d.toISOString(); 96 }; 97 BytePushers.converters.DateConverter.convertToISO8601Date = function (iso8601DateString) { 98 var regexp = new RegExp("([0-9]{4})(-([0-9]{2})(-([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\\.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?"), 99 d = iso8601DateString.match(new RegExp(regexp)), 100 date;/*, 101 offset = 0;*/ 102 103 if (d === null) { 104 throw new BytePushers.exceptions.InvalidParameterException("ISO 8601 Date String: " + d + " should be in ISO 8601 format YYYY-MM-DDThh:mm:ss:sTZD."); 105 } 106 107 108 date = new Date(d[1], 0, 1); 109 110 if (d[3]) { date.setMonth(d[3] - 1); } 111 if (d[5]) { date.setDate(d[5]); } 112 if (d[7]) { date.setHours(d[7]); } 113 if (d[8]) { date.setMinutes(d[8]); } 114 if (d[10]) { date.setSeconds(d[10]); } 115 if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); } 116 /*if (d[14]) { 117 offset = (Number(d[16]) * 60) + Number(d[17]); 118 offset *= ((d[15] === '-') ? 1 : -1); 119 }*/ 120 121 //offset -= date.getTimezoneOffset(); 122 //time = (date.getTime() + (offset * 60 * 1000)); 123 //date.setTime(time); 124 return date; 125 }; 126 BytePushers.converters.DateConverter.convertToDate = function (d, dateFormat) { 127 var date = null; 128 switch (dateFormat) { 129 case BytePushers.converters.DateConverter.MDDYYYY_DATE_FORMAT: 130 date = BytePushers.converters.DateConverter.convertToDate_MDDYYYY(d); 131 break; 132 case BytePushers.converters.DateConverter.MMDDYYYY_DATE_FORMAT: 133 date = BytePushers.converters.DateConverter.convertToDate_MMDDYYYY(d); 134 break; 135 case BytePushers.converters.DateConverter.MMMDDYYYY_DATE_FORMAT: 136 date = BytePushers.converters.DateConverter.convertToDate_MMMDDYYYY(d); 137 break; 138 case BytePushers.converters.DateConverter.YYYYMMDDThhmmsssTZD_DATE_FORMAT: 139 date = BytePushers.converters.DateConverter.convertToDate_YYYYMMDDThhmmsssTZD(d); 140 break; 141 case BytePushers.converters.DateConverter.YYYYMMDD_DATE_FORMAT: 142 date = BytePushers.converters.DateConverter.convertToDate_YYYYMMDD(d); 143 break; 144 } 145 return date; 146 }; 147 BytePushers.converters.DateConverter.convertToString = function (d, dateFormat, delimeter) { 148 var date = null; 149 switch (dateFormat) { 150 case BytePushers.converters.DateConverter.YYYYMMDD_DATE_FORMAT: 151 date = BytePushers.converters.DateConverter.convertToString_YYYYMMDD(d, delimeter); 152 break; 153 case BytePushers.converters.DateConverter.YYYYMMDDThhmmsssTZD_DATE_FORMAT: 154 date = BytePushers.converters.DateConverter.convertToString_YYYYMMDDThhmmsssTZD(d, delimeter); 155 break; 156 } 157 return date; 158 }; 159 160 161 BytePushers.models = BytePushers.models || BytePushers.namespace("software.bytepushers.models"); 162 BytePushers.models.Month = BytePushers.namespace("software.bytepushers.models.Month"); 163 164 /** 165 * <p>Static field that is used to get calendar full name, abbreviated names, and total calendar days.</p> 166 * @static 167 * @field 168 * @author <a href="mailto:pouncilt.developer@gmail.com">Tonté Pouncil</a> 169 */ 170 BytePushers.models.Month.getMonthIndex = function (abbr) { 171 var i = -1; 172 BytePushers.models.Month.monthNames.forEach(function (monthName, index) { 173 if (monthName.abbr === abbr) { 174 i = index; 175 } 176 }); 177 return i; 178 }; 179 /** 180 * <p>Static field that is used to get calendar total calendar days of the previous month.</p> 181 * @static 182 * @function 183 * @param {@link <a href="http://www.w3schools.com/jsref/jsref_obj_date.asp">Date</a>} date Represents some arbitrary calendar date. 184 * @returns {@link <a href="http://www.w3schools.com/jsref/jsref_obj_number.asp">Number</a>} The total days in the previous month. 185 * @author <a href="mailto:pouncilt.developer@gmail.com">Tonté Pouncil</a> 186 */ 187 BytePushers.models.Month.getPreviousMonthTotalDays = function (date) { 188 if (date.getMonth() === 0) { 189 return BytePushers.models.Month.monthNames[11].getTotalDays(date.getFullYear()); 190 } 191 192 return BytePushers.models.Month.monthNames[date.getMonth() - 1].getTotalDays(date.getFullYear()); 193 }; 194 195 /** 196 * <p>Static function that is used to get the total calendar days of the next month.</p> 197 * @static 198 * @function 199 * @param <a href="http://www.w3schools.com/jsref/jsref_obj_date.asp">Date</a> date Represents some arbitrary calendar date. 200 * @returns {@link <a href="http://www.w3schools.com/jsref/jsref_obj_number.asp">Number</a>} The total days in the next month. 201 * @author <a href="mailto:pouncilt.developer@gmail.com">Tonté Pouncil</a> 202 */ 203 BytePushers.models.Month.getNextMonthTotalDays = function (date) { 204 if (date.getMonth() === 11) { 205 return BytePushers.models.Month.monthNames[0].getTotalDays(date.getFullYear()); 206 } 207 208 return BytePushers.models.Month.monthNames[date.getMonth() + 1].getTotalDays(date.getFullYear()); 209 }; 210 /** 211 * <p>Static field for the list of month.</p> 212 * @static 213 * @field 214 * @author <a href="mailto:pouncilt.developer@gmail.com">Tonté Pouncil</a> 215 */ 216 BytePushers.models.Month.monthNames = [ 217 {"name": "January", "abbr": "Jan", "getTotalDays": function () {return 31; } }, 218 {"name": "February", "abbr": "Feb", "getTotalDays": function (year) {if (year) { return (year % 4 === 0) ? 29 : 28; } throw ("Expected parameter(Year) is not defined."); } }, 219 {"name": "March", "abbr": "Mar", "getTotalDays": function () {return 31; }}, 220 {"name": "April", "abbr": "Apr", "getTotalDays": function () {return 30; }}, 221 {"name": "May", "abbr": "May", "getTotalDays": function () {return 31; }}, 222 {"name": "June", "abbr": "Jun", "getTotalDays": function () {return 30; }}, 223 {"name": "July", "abbr": "Jul", "getTotalDays": function () {return 31; }}, 224 {"name": "August", "abbr": "Aug", "getTotalDays": function () {return 31; }}, 225 {"name": "September", "abbr": "Sep", "getTotalDays": function () {return 30; }}, 226 {"name": "October", "abbr": "Oct", "getTotalDays": function () {return 31; }}, 227 {"name": "November", "abbr": "Nov", "getTotalDays": function () {return 30; }}, 228 {"name": "December", "abbr": "Dec", "getTotalDays": function () {return 31; }} 229 ]; 230 /** 231 * <p>Static field for the list of weekdays.</p> 232 * @static 233 * @field 234 * @author <a href="mailto:pouncilt.developer@gmail.com">Tonté Pouncil</a> 235 */ 236 BytePushers.models.Month.weekdayNames = [ 237 {"name": "Sundays", "abbr": "Sun."}, 238 {"name": "Monday", "abbr": "Mon."}, 239 {"name": "Tuesday", "abbr": "Tue."}, 240 {"name": "Wednesday", "abbr": "Wed."}, 241 {"name": "Thursday", "abbr": "Thu."}, 242 {"name": "Friday", "abbr": "Fri."}, 243 {"name": "Saturday", "abbr": "Sat."} 244 ]; 245 }(BytePushers)); 246 247