<%
/*
 * 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();
    o.__type = "_JSON_Date";

    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);
    }
    o.getEpochTime = _getEpochTime;

    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);
    }
    o.encoding = _encoding;

    if (! secondsSinceEpoch)
    {
        var now = gettimeofday();
        o.setEpochTime(now.sec);
    }
    else
    {
        o.setEpochTime(secondsSinceEpoch);
    }
    o.year = 0;
    o.month = 0;
    o.day = 0;
    o.hour = 0;
    o.minute = 0;
    o.second = 0;
    o.millisecond = 0;
    return o;
}

JSON_Date = new Object();
JSON_Date.create = _JSON_Date_create;
_JSON_Date_create = null;


/*
 * Local Variables:
 * mode: c
 * End:
 */
%>