git_sitools_idoc / flarecast / workspace / client-public-3.0 / js / utils / Date.js @ master
1 |
/***************************************
|
---|---|
2 |
* Copyright 2010-2014 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
|
3 |
*
|
4 |
* This file is part of SITools2.
|
5 |
*
|
6 |
* SITools2 is free software: you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation, either version 3 of the License, or
|
9 |
* (at your option) any later version.
|
10 |
*
|
11 |
* SITools2 is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with SITools2. If not, see <http://www.gnu.org/licenses/>.
|
18 |
***************************************/
|
19 |
Ext.namespace('sitools.public.utils');
|
20 |
|
21 |
/**
|
22 |
* An utility class to use for specific sitools dates.
|
23 |
*/
|
24 |
Ext.define("sitools.public.utils.Date", {
|
25 |
singleton : true, |
26 |
/**
|
27 |
* The regExp to test if a string can be transformed to a date
|
28 |
*/
|
29 |
regToday : new RegExp("^\{\\$TODAY\} *(\\+ *[0-9]*|\\- *[0-9]*)?$"), |
30 |
/**
|
31 |
* in the template {$TODAY} + x, determine the x unit (currently it is a Day)
|
32 |
* @type
|
33 |
*/
|
34 |
timeInterval : Ext.Date.DAY,
|
35 |
/**
|
36 |
* Transform a String value containing {$TODAY} into a valid date.
|
37 |
* @param {String} val the string value
|
38 |
* @return {Date} the date Value.
|
39 |
*/
|
40 |
stringWithTodayToDate : function(val) { |
41 |
if (Ext.isDate(val)) {
|
42 |
return val;
|
43 |
} |
44 |
if (!this.regToday.test(val)) { |
45 |
return null; |
46 |
} |
47 |
|
48 |
var regNbJour = new RegExp("[0-9]+", "g"); |
49 |
var regOp = new RegExp("[+]|[-]"); |
50 |
var nbJour = parseFloat(regNbJour.exec(val));
|
51 |
var op = regOp.exec(val);
|
52 |
|
53 |
var result = new Date(); |
54 |
if (Ext.isEmpty(nbJour) && !Ext.isEmpty(op)) {
|
55 |
return null; |
56 |
} |
57 |
if (!Ext.isEmpty(nbJour) && !Ext.isEmpty(op)) {
|
58 |
if (op == "-") { |
59 |
nbJour = nbJour * -1;
|
60 |
} |
61 |
result = Ext.Date.add(result, this.timeInterval, nbJour);
|
62 |
} |
63 |
return result;
|
64 |
|
65 |
}, |
66 |
/**
|
67 |
* Test if a date object is valid or not.
|
68 |
* @param {Date} d date value
|
69 |
* @return {Boolean} valid or not valid
|
70 |
*/
|
71 |
isValidDate : function (d) { |
72 |
if ( Object.prototype.toString.call(d) !== "[object Date]" ) |
73 |
return false; |
74 |
return !isNaN(d.getTime());
|
75 |
} |
76 |
}); |
77 |
|