4 * (C) 2006 by Derrell Lipman
8 * LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
12 * Date class for JSON-RPC
16 function _JSON_Date_create(secondsSinceEpoch)
19 o.__type = "_JSON_Date";
21 function _setUtcDateTimeFields(year, month, day, hour, minute, second, millisecond)
24 this.month = month + 0;
27 this.minute = minute + 0;
28 this.second = second + 0;
29 this.millisecond = millisecond + 0;
32 o.setUtcYear = _setUtcDateTimeFields;
34 function _setUtcYear(year)
38 o.setUtcYear = _setUtcYear;
40 function _setUtcMonth(month)
42 this.month = month + 0;
44 o.setUtcMonth = _setUtcMonth;
46 function _setUtcDay(day)
50 o.setUtcDay = _setUtcDay;
52 function _setUtcHour(hour)
56 o.setUtcHour = _setUtcHour;
58 function _setUtcMinute(minute)
60 this.minute = minute + 0;
62 o.setUtcMinute = _setUtcMinute;
64 function _setUtcSecond(second)
66 this.second = second + 0;
68 o.setUtcSecond = _setUtcSecond;
70 function _setUtcMillisecond(millisecond)
72 this.millisecond = millisecond + 0;
74 o.setUtcMillisecond = _setUtcMillisecond;
76 function _setEpochTime(secondsSinceEpoch)
80 if (typeof(secondsSinceEpoch) != "number")
82 var currentTime = gettimeofday();
83 secondsSinceEpoch = currentTime.sec;
84 microseconds = currentTime.usec;
87 var tm = gmtime(secondsSinceEpoch);
89 this.year = 1900 + tm.tm_year;
90 this.month = tm.tm_mon;
91 this.day = tm.tm_mday;
92 this.hour = tm.tm_hour;
93 this.minute = tm.tm_min;
94 this.second = tm.tm_sec;
97 o.setEpochTime = _setEpochTime;
99 function _getUtcYear()
103 o.getUtcYear = _getUtcYear;
105 function _getUtcMonth()
109 o.getUtcMonth = _getUtcMonth;
111 function _getUtcDay()
115 o.getUtcDay = _getUtcDay;
117 function _getUtcHour()
121 o.getUtcHour = _getUtcHour;
123 function _getUtcMinute()
127 o.getUtcMinute = _getUtcMinute;
129 function _getUtcSecond()
133 o.getUtcSecond = _getUtcSecond;
135 function _getUtcMillisecond()
137 return this.millisecond;
139 o.getUtcMillisecond = _getUtcMillisecond;
141 function _getEpochTime()
143 var tm = new Object();
144 tm.tm_sec = this.second;
145 tm.tm_min = this.minute;
146 tm.tm_hour = this.hour;
148 tm.tm_mon = this.month;
149 tm.tm_year = this.year;
155 o.getEpochTime = _getEpochTime;
159 /* Encode the date in a well-documented fashion */
160 return sprintf("new Date(Date.UTC(%d,%d,%d,%d,%d,%d,%d))",
169 o.encoding = _encoding;
171 if (! secondsSinceEpoch)
173 var now = gettimeofday();
174 o.setEpochTime(now.sec);
178 o.setEpochTime(secondsSinceEpoch);
190 JSON_Date = new Object();
191 JSON_Date.create = _JSON_Date_create;
192 _JSON_Date_create = null;