r19166: better bug and reporting and proper authentication tracking
[kai/samba.git] / services / jsondate.esp
1 <%
2 /*
3  * Copyright:
4  *   (C) 2006 by Derrell Lipman
5  *       All rights reserved
6  *
7  * License:
8  *   LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
9  */
10
11 /*
12  * Date class for JSON-RPC
13  */
14
15
16 function _JSON_Date_create(secondsSinceEpoch)
17 {
18     var o = new Object();
19     o.__type = "_JSON_Date";
20
21     function _setUtcDateTimeFields(year, month, day, hour, minute, second, millisecond)
22     {
23         this.year = year + 0;
24         this.month = month + 0;
25         this.day =  day + 0;
26         this.hour = hour + 0;
27         this.minute = minute + 0;
28         this.second = second + 0;
29         this.millisecond = millisecond + 0;
30     }
31
32     o.setUtcYear = _setUtcDateTimeFields;
33
34     function _setUtcYear(year)
35     {
36         this.year = year + 0;
37     }
38     o.setUtcYear = _setUtcYear;
39
40     function _setUtcMonth(month)
41     {
42         this.month = month + 0;
43     }
44     o.setUtcMonth = _setUtcMonth;
45
46     function _setUtcDay(day)
47     {
48         this.day = day + 0;
49     }
50     o.setUtcDay = _setUtcDay;
51
52     function _setUtcHour(hour)
53     {
54         this.hour = hour + 0;
55     }
56     o.setUtcHour = _setUtcHour;
57
58     function _setUtcMinute(minute)
59     {
60         this.minute = minute + 0;
61     }
62     o.setUtcMinute = _setUtcMinute;
63
64     function _setUtcSecond(second)
65     {
66         this.second = second + 0;
67     }
68     o.setUtcSecond = _setUtcSecond;
69
70     function _setUtcMillisecond(millisecond)
71     {
72         this.millisecond = millisecond + 0;
73     }
74     o.setUtcMillisecond = _setUtcMillisecond;
75
76     function _setEpochTime(secondsSinceEpoch)
77     {
78         var microseconds = 0;
79
80         if (typeof(secondsSinceEpoch) != "number")
81         {
82             var currentTime = gettimeofday();
83             secondsSinceEpoch = currentTime.sec;
84             microseconds = currentTime.usec;
85         }
86
87         var tm = gmtime(secondsSinceEpoch);
88
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;
95         this.millisecond = 0;
96     }
97     o.setEpochTime = _setEpochTime;
98
99     function _getUtcYear()
100     {
101         return this.year;
102     }
103     o.getUtcYear = _getUtcYear;
104
105     function _getUtcMonth()
106     {
107         return this.month;
108     }
109     o.getUtcMonth = _getUtcMonth;
110
111     function _getUtcDay()
112     {
113         return this.day;
114     }
115     o.getUtcDay = _getUtcDay;
116
117     function _getUtcHour()
118     {
119         return this.hour;
120     }
121     o.getUtcHour = _getUtcHour;
122
123     function _getUtcMinute()
124     {
125         return this.minute;
126     }
127     o.getUtcMinute = _getUtcMinute;
128
129     function _getUtcSecond()
130     {
131         return this.second;
132     }
133     o.getUtcSecond = _getUtcSecond;
134
135     function _getUtcMillisecond()
136     {
137         return this.millisecond;
138     }
139     o.getUtcMillisecond = _getUtcMillisecond;
140
141     function _getEpochTime()
142     {
143         var tm = new Object();
144         tm.tm_sec = this.second;
145         tm.tm_min = this.minute;
146         tm.tm_hour = this.hour;
147         tm.tm_mday = -1;
148         tm.tm_mon = this.month;
149         tm.tm_year = this.year;
150         tm.tm_wday = -1;
151         tm.tm_yday = -1;
152         tm.isdst = 0;
153         return gmmktime(tm);
154     }
155     o.getEpochTime = _getEpochTime;
156
157     function _encoding()
158     {
159         /* Encode the date in a well-documented fashion */
160         return sprintf("new Date(Date.UTC(%d,%d,%d,%d,%d,%d,%d))",
161                        this.year,
162                        this.month,
163                        this.day,
164                        this.hour,
165                        this.minute,
166                        this.second,
167                        this.millisecond);
168     }
169     o.encoding = _encoding;
170
171     if (! secondsSinceEpoch)
172     {
173         var now = gettimeofday();
174         o.setEpochTime(now.sec);
175     }
176     else
177     {
178         o.setEpochTime(secondsSinceEpoch);
179     }
180     o.year = 0;
181     o.month = 0;
182     o.day = 0;
183     o.hour = 0;
184     o.minute = 0;
185     o.second = 0;
186     o.millisecond = 0;
187     return o;
188 }
189
190 JSON_Date = new Object();
191 JSON_Date.create = _JSON_Date_create;
192 _JSON_Date_create = null;
193
194
195 /*
196  * Local Variables:
197  * mode: c
198  * End:
199  */
200 %>