1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
1422 |
alexandre_ |
11 |
|
|
|
12 |
|
1318 |
alexandre_ |
13 |
dojo.provide("dojo.date.serialize");
|
|
|
14 |
dojo.require("dojo.string.common");
|
|
|
15 |
dojo.date.setIso8601 = function (dateObject, formattedString) {
|
|
|
16 |
var comps = (formattedString.indexOf("T") == -1) ? formattedString.split(" ") : formattedString.split("T");
|
|
|
17 |
dateObject = dojo.date.setIso8601Date(dateObject, comps[0]);
|
|
|
18 |
if (comps.length == 2) {
|
|
|
19 |
dateObject = dojo.date.setIso8601Time(dateObject, comps[1]);
|
|
|
20 |
}
|
|
|
21 |
return dateObject;
|
|
|
22 |
};
|
|
|
23 |
dojo.date.fromIso8601 = function (formattedString) {
|
|
|
24 |
return dojo.date.setIso8601(new Date(0, 0), formattedString);
|
|
|
25 |
};
|
|
|
26 |
dojo.date.setIso8601Date = function (dateObject, formattedString) {
|
|
|
27 |
var regexp = "^([0-9]{4})((-?([0-9]{2})(-?([0-9]{2}))?)|" + "(-?([0-9]{3}))|(-?W([0-9]{2})(-?([1-7]))?))?$";
|
|
|
28 |
var d = formattedString.match(new RegExp(regexp));
|
|
|
29 |
if (!d) {
|
|
|
30 |
dojo.debug("invalid date string: " + formattedString);
|
|
|
31 |
return null;
|
|
|
32 |
}
|
|
|
33 |
var year = d[1];
|
|
|
34 |
var month = d[4];
|
|
|
35 |
var date = d[6];
|
|
|
36 |
var dayofyear = d[8];
|
|
|
37 |
var week = d[10];
|
|
|
38 |
var dayofweek = d[12] ? d[12] : 1;
|
|
|
39 |
dateObject.setFullYear(year);
|
|
|
40 |
if (dayofyear) {
|
|
|
41 |
dateObject.setMonth(0);
|
|
|
42 |
dateObject.setDate(Number(dayofyear));
|
|
|
43 |
} else {
|
|
|
44 |
if (week) {
|
|
|
45 |
dateObject.setMonth(0);
|
|
|
46 |
dateObject.setDate(1);
|
|
|
47 |
var gd = dateObject.getDay();
|
|
|
48 |
var day = gd ? gd : 7;
|
|
|
49 |
var offset = Number(dayofweek) + (7 * Number(week));
|
|
|
50 |
if (day <= 4) {
|
|
|
51 |
dateObject.setDate(offset + 1 - day);
|
|
|
52 |
} else {
|
|
|
53 |
dateObject.setDate(offset + 8 - day);
|
|
|
54 |
}
|
|
|
55 |
} else {
|
|
|
56 |
if (month) {
|
|
|
57 |
dateObject.setDate(1);
|
|
|
58 |
dateObject.setMonth(month - 1);
|
|
|
59 |
}
|
|
|
60 |
if (date) {
|
|
|
61 |
dateObject.setDate(date);
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
return dateObject;
|
|
|
66 |
};
|
|
|
67 |
dojo.date.fromIso8601Date = function (formattedString) {
|
|
|
68 |
return dojo.date.setIso8601Date(new Date(0, 0), formattedString);
|
|
|
69 |
};
|
|
|
70 |
dojo.date.setIso8601Time = function (dateObject, formattedString) {
|
|
|
71 |
var timezone = "Z|(([-+])([0-9]{2})(:?([0-9]{2}))?)$";
|
|
|
72 |
var d = formattedString.match(new RegExp(timezone));
|
|
|
73 |
var offset = 0;
|
|
|
74 |
if (d) {
|
|
|
75 |
if (d[0] != "Z") {
|
|
|
76 |
offset = (Number(d[3]) * 60) + Number(d[5]);
|
|
|
77 |
offset *= ((d[2] == "-") ? 1 : -1);
|
|
|
78 |
}
|
|
|
79 |
offset -= dateObject.getTimezoneOffset();
|
|
|
80 |
formattedString = formattedString.substr(0, formattedString.length - d[0].length);
|
|
|
81 |
}
|
|
|
82 |
var regexp = "^([0-9]{2})(:?([0-9]{2})(:?([0-9]{2})(.([0-9]+))?)?)?$";
|
|
|
83 |
d = formattedString.match(new RegExp(regexp));
|
|
|
84 |
if (!d) {
|
|
|
85 |
dojo.debug("invalid time string: " + formattedString);
|
|
|
86 |
return null;
|
|
|
87 |
}
|
|
|
88 |
var hours = d[1];
|
|
|
89 |
var mins = Number((d[3]) ? d[3] : 0);
|
|
|
90 |
var secs = (d[5]) ? d[5] : 0;
|
|
|
91 |
var ms = d[7] ? (Number("0." + d[7]) * 1000) : 0;
|
|
|
92 |
dateObject.setHours(hours);
|
|
|
93 |
dateObject.setMinutes(mins);
|
|
|
94 |
dateObject.setSeconds(secs);
|
|
|
95 |
dateObject.setMilliseconds(ms);
|
|
|
96 |
if (offset !== 0) {
|
|
|
97 |
dateObject.setTime(dateObject.getTime() + offset * 60000);
|
|
|
98 |
}
|
|
|
99 |
return dateObject;
|
|
|
100 |
};
|
|
|
101 |
dojo.date.fromIso8601Time = function (formattedString) {
|
|
|
102 |
return dojo.date.setIso8601Time(new Date(0, 0), formattedString);
|
|
|
103 |
};
|
|
|
104 |
dojo.date.toRfc3339 = function (dateObject, selector) {
|
|
|
105 |
if (!dateObject) {
|
|
|
106 |
dateObject = new Date();
|
|
|
107 |
}
|
|
|
108 |
var _ = dojo.string.pad;
|
|
|
109 |
var formattedDate = [];
|
|
|
110 |
if (selector != "timeOnly") {
|
|
|
111 |
var date = [_(dateObject.getFullYear(), 4), _(dateObject.getMonth() + 1, 2), _(dateObject.getDate(), 2)].join("-");
|
|
|
112 |
formattedDate.push(date);
|
|
|
113 |
}
|
|
|
114 |
if (selector != "dateOnly") {
|
|
|
115 |
var time = [_(dateObject.getHours(), 2), _(dateObject.getMinutes(), 2), _(dateObject.getSeconds(), 2)].join(":");
|
|
|
116 |
var timezoneOffset = dateObject.getTimezoneOffset();
|
|
|
117 |
time += (timezoneOffset > 0 ? "-" : "+") + _(Math.floor(Math.abs(timezoneOffset) / 60), 2) + ":" + _(Math.abs(timezoneOffset) % 60, 2);
|
|
|
118 |
formattedDate.push(time);
|
|
|
119 |
}
|
|
|
120 |
return formattedDate.join("T");
|
|
|
121 |
};
|
|
|
122 |
dojo.date.fromRfc3339 = function (rfcDate) {
|
|
|
123 |
if (rfcDate.indexOf("Tany") != -1) {
|
|
|
124 |
rfcDate = rfcDate.replace("Tany", "");
|
|
|
125 |
}
|
|
|
126 |
var dateObject = new Date();
|
|
|
127 |
return dojo.date.setIso8601(dateObject, rfcDate);
|
|
|
128 |
};
|
|
|
129 |
|