/*!
* Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
*
* This Software is licenced under the LGPL Licence (GNU Lesser General
* Public License).
* In addition to the LGPL Licence the Software is subject to the
* following conditions:
*
* i every modification must be public and comunicated to the Author
* ii every "jsbean" added to this library must be self consistent
* except for the dependence from jsbeans-x.x.x.js
* iii copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* A collection of utilities for date
* @namespace jsbeans
* @class date
* @static
*/
jsbeans.date = {
/**
* @property YEAR
* @type Integer
* @default 1
* @static
* @final
* */
YEAR: 1,
/**
* @property MONTH
* @type Integer
* @default 2
* @static
* @final
* */
MONTH: 2,
/**
* @property DATE
* @type Integer
* @default 3
* @static
* @final
* */
DATE: 3,
/**
* @property HOUR
* @type Integer
* @default 4
* @static
* @final
* */
HOUR: 4,
/**
* @property MINUTE
* @type Integer
* @default 5
* @static
* @final
* */
MINUTE: 5,
/**
* @property SECOND
* @type Integer
* @default 6
* @static
* @final
* */
SECOND: 6,
/**
* @property MILLIS
* @type Integer
* @default 7
* @static
* @final
* */
MILLIS: 7,
/**
* @property OFFSET
* @type Integer
* @default new Date().getTimezoneOffset() / 60
* @static
* @final
* */
OFFSET: new Date().getTimezoneOffset() / 60,
/**
* Starting by a date it let you go up or down in calendar for the chosen item (year, month, ..) by the step of your choice (1 by default).
* @method add
* @param date {Date} a starting date
* @param rollerType {Integer} use <code class="prop">jsbeans.date.YEAR</code>, <code class="prop">jsbeans.date.MONTH</code>, <code class="prop">jsbeans.date.DATE</code>, ...
* @param [amount] {Integer} how much to roll. May be negative. Default 1
* @return {Date} a new date that differs from <code class="param">date</code> in <code class="param">rollerType</code> by <code class="param">amount</code>
* @static
* */
add: function(d, rollerType /*, amount*/) {
var amount = arguments[2] || 1;
switch (rollerType) {
case 1 /*year*/ :
var next = d.getFullYear() + amount;
return new Date(Date.UTC(
next,
d.getMonth(),
d.getDate(),
d.getHours() + jsbeans.date.OFFSET,
d.getMinutes(),
d.getSeconds(),
d.getMilliseconds()));
case 2 /*month*/ :
var next = d.getMonth() + amount;
return new Date(Date.UTC(
d.getFullYear(),
next,
d.getDate(),
d.getHours() + jsbeans.date.OFFSET,
d.getMinutes(),
d.getSeconds(),
d.getMilliseconds()));
case 3 /*date*/ :
return new Date(Date.UTC(
d.getFullYear(),
d.getMonth(),
d.getDate() + amount,
d.getHours() + jsbeans.date.OFFSET,
d.getMinutes(),
d.getSeconds(),
d.getMilliseconds()));
case 4 /*hour*/ :
return new Date(Date.UTC(
d.getFullYear(),
d.getMonth(),
d.getDate(),
d.getHours() + amount + jsbeans.date.OFFSET,
d.getMinutes(),
d.getSeconds(),
d.getMilliseconds()));
case 5 /*minute*/ :
return new Date(Date.UTC(
d.getFullYear(),
d.getMonth(),
d.getDate(),
d.getHours() + jsbeans.date.OFFSET,
d.getMinutes() + amount,
d.getSeconds(),
d.getMilliseconds()));
case 6 /*second*/ :
return new Date(Date.UTC(
d.getFullYear(),
d.getMonth(),
d.getDate(),
d.getHours() + jsbeans.date.OFFSET,
d.getMinutes(),
d.getSeconds() + amount,
d.getMilliseconds()));
case 7 /*milliseconds*/ :
return new Date(Date.UTC(
d.getFullYear(),
d.getMonth(),
d.getDate(),
d.getHours() + jsbeans.date.OFFSET,
d.getMinutes(),
d.getSeconds(),
d.getMilliseconds() + amount));
}
},
/**
* Formula taken from <a href="http://en.wikipedia.org/wiki/Leap_year#Algorithm">Wikipedia</a>.<br/>
* If {@param} <code class="param">object</code> is an Integer it will be treated as year.
* @method isLeapYear
* @param object {Date | Integer}
* @return {boolean}
* @static
* */
isLeapYear: function(o) {
var y = null;
if (typeof o == "number") {
y = o;
}
else if (o.constructor && (""+o.constructor).indexOf("Date") != -1) {
y = o.getFullYear();
}
else {
return false;
}
if ((y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0)) ) {
return true;
}
else {
return false;
}
},
/**
* Returns how many days has the month in given {@param} <code class="param">date</code>
* @method getDaysInMonth
* @param date {Date}
* @return {Integer}
* @static
* */
getDaysInMonth: function(d) {
var m = d.getMonth();
if (m == 3 || m == 5 || m == 8 || m == 10) {
return 30;
}
else if (m == 1) {
if (jsbeans.date.isLeapYear(d)) {
return 29;
}
else {
return 28;
}
}
else {
return 31;
}
}
};