diff options
Diffstat (limited to 'jsonrpc/jsondate.esp')
-rw-r--r-- | jsonrpc/jsondate.esp | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/jsonrpc/jsondate.esp b/jsonrpc/jsondate.esp new file mode 100644 index 0000000000..af2c7e2e3f --- /dev/null +++ b/jsonrpc/jsondate.esp @@ -0,0 +1,189 @@ + +/* + * Copyright: + * (C) 2006 by Derrell Lipman + * All rights reserved + * + * License: + * LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/ + */ + +/* + * Date class for JSON-RPC + */ + + +function _JSON_Date_create(secondsSinceEpoch) +{ + var o = new Object(); + + function _setUtcDateTimeFields(year, month, day, hour, minute, second, millisecond) + { + this.year = year + 0; + this.month = month + 0; + this.day = day + 0; + this.hour = hour + 0; + this.minute = minute + 0; + this.second = second + 0; + this.millisecond = millisecond + 0; + } + + o.setUtcYear = _setUtcDateTimeFields; + + function _setUtcYear(year) + { + this.year = year + 0; + } + o.setUtcYear = _setUtcYear; + + function _setUtcMonth(month) + { + this.month = month + 0; + } + o.setUtcMonth = _setUtcMonth; + + function _setUtcDay(day) + { + this.day = day + 0; + } + o.setUtcDay = _setUtcDay; + + function _setUtcHour(hour) + { + this.hour = hour + 0; + } + o.setUtcHour = _setUtcHour; + + function _setUtcMinute(minute) + { + this.minute = minute + 0; + } + o.setUtcMinute = _setUtcMinute; + + function _setUtcSecond(second) + { + this.second = second + 0; + } + o.setUtcSecond = _setUtcSecond; + + function _setUtcMillisecond(millisecond) + { + this.millisecond = millisecond + 0; + } + o.setUtcMillisecond = _setUtcMillisecond; + + function _setEpochTime(secondsSinceEpoch) + { + var microseconds = 0; + + if (typeof(secondsSinceEpoch) != "number") + { + var currentTime = getTimeOfDay(); + secondsSinceEpoch = currentTime.sec; + microseconds = currentTime.usec; + } + + var tm = gmtime(secondsSinceEpoch); + + this.year = 1900 + tm.tm_year; + this.month = tm.tm_mon; + this.day = tm.tm_mday; + this.hour = tm.tm_hour; + this.minute = tm.tm_min; + this.second = tm.tm_sec; + this.millisecond = 0; + } + o.setEpochTime = _setEpochTime; + + function _getUtcYear() + { + return this.year; + } + o.getUtcYear = _getUtcYear; + + function _getUtcMonth() + { + return this.month; + } + o.getUtcMonth = getUtcMonth; + + function _getUtcDay() + { + return this.day; + } + o.getUtcDay = _getUtcDay; + + function _getUtcHour() + { + return this.hour; + } + o.getUtcHour = _getUtcHour; + + function _getUtcMinute() + { + return this.minute; + } + o.getUtcMinute = _getUtcMinute; + + function _getUtcSecond() + { + return this.second; + } + o.getUtcSecond = _getUtcSecond; + + function _getUtcMillisecond() + { + return this.millisecond; + } + o.getUtcMillisecond = _getUtcMillisecond; + + function getEpochTime() + { + var tm = new Object(); + tm.tm_sec = this.second; + tm.tm_min = this.minute; + tm.tm_hour = this.hour; + tm.tm_mday = -1; + tm.tm_mon = this.month; + tm.tm_year = this.year; + tm.tm_wday = -1; + tm.tm_yday = -1; + tm.isdst = 0; + return gmmktime(tm); + } + + function encoding() + { + /* Encode the date in a well-documented fashion */ + return sprintf("new Date(Date.UTC(%d,%d,%d,%d,%d,%d,%d))", + this.year, + this.month, + this.day, + this.hour, + this.minute, + this.second, + this.millisecond); + } + + if (! secondsSinceEpoch) + { + var now = getTimeOfDay(); + o.setEpochTime(now.sec); + } + else + { + o.setEpochTime(secondsSinceEpoch); + } + o.year = null; + o.month = null; + o.day = null; + o.hour = null; + o.minute = null; + o.second = null; + o.millisecond = null; + return o; +} + +JSON_Date = new Object(); +JSON_Date.create = _JSON_Date_create; +_JSON_Date_create = null; |