r19051: JSON-RPC server work-in-progress. It's almost working.
[jelmer/samba4-debian.git] / jsonrpc / 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
20     function _setUtcDateTimeFields(year, month, day, hour, minute, second, millisecond)
21     {
22         this.year = year + 0;
23         this.month = month + 0;
24         this.day =  day + 0;
25         this.hour = hour + 0;
26         this.minute = minute + 0;
27         this.second = second + 0;
28         this.millisecond = millisecond + 0;
29     }
30
31     o.setUtcYear = _setUtcDateTimeFields;
32
33     function _setUtcYear(year)
34     {
35         this.year = year + 0;
36     }
37     o.setUtcYear = _setUtcYear;
38
39     function _setUtcMonth(month)
40     {
41         this.month = month + 0;
42     }
43     o.setUtcMonth = _setUtcMonth;
44
45     function _setUtcDay(day)
46     {
47         this.day = day + 0;
48     }
49     o.setUtcDay = _setUtcDay;
50
51     function _setUtcHour(hour)
52     {
53         this.hour = hour + 0;
54     }
55     o.setUtcHour = _setUtcHour;
56
57     function _setUtcMinute(minute)
58     {
59         this.minute = minute + 0;
60     }
61     o.setUtcMinute = _setUtcMinute;
62
63     function _setUtcSecond(second)
64     {
65         this.second = second + 0;
66     }
67     o.setUtcSecond = _setUtcSecond;
68
69     function _setUtcMillisecond(millisecond)
70     {
71         this.millisecond = millisecond + 0;
72     }
73     o.setUtcMillisecond = _setUtcMillisecond;
74
75     function _setEpochTime(secondsSinceEpoch)
76     {
77         var microseconds = 0;
78
79         if (typeof(secondsSinceEpoch) != "number")
80         {
81             var currentTime = gettimeofday();
82             secondsSinceEpoch = currentTime.sec;
83             microseconds = currentTime.usec;
84         }
85
86         var tm = gmtime(secondsSinceEpoch);
87
88         this.year = 1900 + tm.tm_year;
89         this.month = tm.tm_mon;
90         this.day = tm.tm_mday;
91         this.hour = tm.tm_hour;
92         this.minute = tm.tm_min;
93         this.second = tm.tm_sec;
94         this.millisecond = 0;
95     }
96     o.setEpochTime = _setEpochTime;
97
98     function _getUtcYear()
99     {
100         return this.year;
101     }
102     o.getUtcYear = _getUtcYear;
103
104     function _getUtcMonth()
105     {
106         return this.month;
107     }
108     o.getUtcMonth = _getUtcMonth;
109
110     function _getUtcDay()
111     {
112         return this.day;
113     }
114     o.getUtcDay = _getUtcDay;
115
116     function _getUtcHour()
117     {
118         return this.hour;
119     }
120     o.getUtcHour = _getUtcHour;
121
122     function _getUtcMinute()
123     {
124         return this.minute;
125     }
126     o.getUtcMinute = _getUtcMinute;
127
128     function _getUtcSecond()
129     {
130         return this.second;
131     }
132     o.getUtcSecond = _getUtcSecond;
133
134     function _getUtcMillisecond()
135     {
136         return this.millisecond;
137     }
138     o.getUtcMillisecond = _getUtcMillisecond;
139
140     function getEpochTime()
141     {
142         var tm = new Object();
143         tm.tm_sec = this.second;
144         tm.tm_min = this.minute;
145         tm.tm_hour = this.hour;
146         tm.tm_mday = -1;
147         tm.tm_mon = this.month;
148         tm.tm_year = this.year;
149         tm.tm_wday = -1;
150         tm.tm_yday = -1;
151         tm.isdst = 0;
152         return gmmktime(tm);
153     }
154
155     function encoding()
156     {
157         /* Encode the date in a well-documented fashion */
158         return sprintf("new Date(Date.UTC(%d,%d,%d,%d,%d,%d,%d))",
159                        this.year,
160                        this.month,
161                        this.day,
162                        this.hour,
163                        this.minute,
164                        this.second,
165                        this.millisecond);
166     }
167
168     if (! secondsSinceEpoch)
169     {
170         var now = gettimeofday();
171         o.setEpochTime(now.sec);
172     }
173     else
174     {
175         o.setEpochTime(secondsSinceEpoch);
176     }
177     o.year = null;
178     o.month = null;
179     o.day = null;
180     o.hour = null;
181     o.minute = null;
182     o.second = null;
183     o.millisecond = null;
184     return o;
185 }
186
187 JSON_Date = new Object();
188 JSON_Date.create = _JSON_Date_create;
189 _JSON_Date_create = null;