/** * angular-strap * @version v2.2.1 - 2015-03-10 * @link http://mgcrea.github.io/angular-strap * @author Olivier Louvignes (olivier@mg-crea.com) * @license MIT License, http://www.opensource.org/licenses/MIT */ 'use strict'; angular.module('mgcrea.ngStrap.helpers.dateParser', []) .provider('$dateParser', ["$localeProvider", function($localeProvider) { // define a custom ParseDate object to use instead of native Date // to avoid date values wrapping when setting date component values function ParseDate() { this.year = 1970; this.month = 0; this.day = 1; this.hours = 0; this.minutes = 0; this.seconds = 0; this.milliseconds = 0; } ParseDate.prototype.setMilliseconds = function(value) { this.milliseconds = value; }; ParseDate.prototype.setSeconds = function(value) { this.seconds = value; }; ParseDate.prototype.setMinutes = function(value) { this.minutes = value; }; ParseDate.prototype.setHours = function(value) { this.hours = value; }; ParseDate.prototype.getHours = function() { return this.hours; }; ParseDate.prototype.setDate = function(value) { this.day = value; }; ParseDate.prototype.setMonth = function(value) { this.month = value; }; ParseDate.prototype.setFullYear = function(value) { this.year = value; }; ParseDate.prototype.fromDate = function(value) { this.year = value.getFullYear(); this.month = value.getMonth(); this.day = value.getDate(); this.hours = value.getHours(); this.minutes = value.getMinutes(); this.seconds = value.getSeconds(); this.milliseconds = value.getMilliseconds(); return this; }; ParseDate.prototype.toDate = function() { return new Date(this.year, this.month, this.day, this.hours, this.minutes, this.seconds, this.milliseconds); }; var proto = ParseDate.prototype; function noop() { } function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); } function indexOfCaseInsensitive(array, value) { var len = array.length, str=value.toString().toLowerCase(); for (var i=0; i 12 when midnight changeover, but then cannot generate * midnight datetime, so jump to 1AM, otherwise reset. * @param date (Date) the date to check * @return (Date) the corrected date * * __ copied from jquery ui datepicker __ */ $dateParser.daylightSavingAdjust = function(date) { if (!date) { return null; } date.setHours(date.getHours() > 12 ? date.getHours() + 2 : 0); return date; }; /* Correct the date for timezone offset. * @param date (Date) the date to adjust * @param timezone (string) the timezone to adjust for * @param undo (boolean) to add or subtract timezone offset * @return (Date) the corrected date */ $dateParser.timezoneOffsetAdjust = function(date, timezone, undo) { if (!date) { return null; } // Right now, only 'UTC' is supported. if (timezone && timezone === 'UTC') { date = new Date(date.getTime()); date.setMinutes(date.getMinutes() + (undo?-1:1)*date.getTimezoneOffset()); } return date; }; // Private functions function setMapForFormat(format) { var keys = Object.keys(setFnMap), i; var map = [], sortedMap = []; // Map to setFn var clonedFormat = format; for(i = 0; i < keys.length; i++) { if(format.split(keys[i]).length > 1) { var index = clonedFormat.search(keys[i]); format = format.split(keys[i]).join(''); if(setFnMap[keys[i]]) { map[index] = setFnMap[keys[i]]; } } } // Sort result map angular.forEach(map, function(v) { // conditional required since angular.forEach broke around v1.2.21 // related pr: https://github.com/angular/angular.js/pull/8525 if(v) sortedMap.push(v); }); return sortedMap; } function escapeReservedSymbols(text) { return text.replace(/\//g, '[\\/]').replace('/-/g', '[-]').replace(/\./g, '[.]').replace(/\\s/g, '[\\s]'); } function regExpForFormat(format) { var keys = Object.keys(regExpMap), i; var re = format; // Abstract replaces to avoid collisions for(i = 0; i < keys.length; i++) { re = re.split(keys[i]).join('${' + i + '}'); } // Replace abstracted values for(i = 0; i < keys.length; i++) { re = re.split('${' + i + '}').join('(' + regExpMap[keys[i]] + ')'); } format = escapeReservedSymbols(format); return new RegExp('^' + re + '$', ['i']); } $dateParser.init(); return $dateParser; }; return DateParserFactory; }]; }]);