summaryrefslogtreecommitdiff
path: root/services/jsondate.esp
blob: 3467228df6d6a91a224c6e36d8acb3225020bfeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<%
/*
 * 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:
 */
%>