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