-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
400 lines (347 loc) · 10.4 KB
/
utils.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* Author:
*/
$(document).ready(function() {
log('hi',this,$(document));
$.getJSON("/ba-simple-proxy.php?url=/reservation/v1/person/KW69-7BG?dataFormat=application/json",
{
dataFormat: "application/json"
},
function(data) {
log("data: ",data);
$.each(data.persons.person, function(i,item){
log("item: ",i,item);
$("<span/>").text(item.ref).appendTo("#main");
if ( i == 3 ) return false;
});
});
});
// from http://www.onlineconversion.com/julian_date.htm
//-------
// convert calendar to Julian date
// (Julian day number algorithm adopted from Press et al.)
//-------
function cal_to_jd( era, y, m, d, h, mn, s )
{
var jy, ja, jm; //scratch
if( y == 0 ) {
alert("There is no year 0 in the Julian system!");
return "invalid";
}
if( y == 1582 && m == 10 && d > 4 && d < 15 ) {
alert("The dates 5 through 14 October, 1582, do not exist in the Gregorian system!");
return "invalid";
}
// if( y < 0 ) ++y;
if( era == "BCE" ) y = -y + 1;
if( m > 2 ) {
jy = y;
jm = m + 1;
} else {
jy = y - 1;
jm = m + 13;
}
var intgr = Math.floor( Math.floor(365.25*jy) + Math.floor(30.6001*jm) + d + 1720995 );
//check for switch to Gregorian calendar
var gregcal = 15 + 31*( 10 + 12*1582 );
if( d + 31*(m + 12*y) >= gregcal ) {
ja = Math.floor(0.01*jy);
intgr += 2 - ja + Math.floor(0.25*ja);
}
//correct for half-day offset
var dayfrac = h/24.0 - 0.5;
if( dayfrac < 0.0 ) {
dayfrac += 1.0;
--intgr;
}
//now set the fraction of a day
var frac = dayfrac + (mn + s/60.0)/60.0/24.0;
//round to nearest second
var jd0 = (intgr + frac)*100000;
var jd = Math.floor(jd0);
if( jd0 - jd > 0.5 ) ++jd;
return jd/100000;
}
//-------
// convert Julian date to calendar date
// (algorithm adopted from Press et al.)
//-------
function jd_to_cal( jd, form )
{
var j1, j2, j3, j4, j5; //scratch
//
// get the date from the Julian day number
//
var intgr = Math.floor(jd);
var frac = jd - intgr;
var gregjd = 2299161;
if( intgr >= gregjd ) { //Gregorian calendar correction
var tmp = Math.floor( ( (intgr - 1867216) - 0.25 ) / 36524.25 );
j1 = intgr + 1 + tmp - Math.floor(0.25*tmp);
} else
j1 = intgr;
//correction for half day offset
var dayfrac = frac + 0.5;
if( dayfrac >= 1.0 ) {
dayfrac -= 1.0;
++j1;
}
j2 = j1 + 1524;
j3 = Math.floor( 6680.0 + ( (j2 - 2439870) - 122.1 )/365.25 );
j4 = Math.floor(j3*365.25);
j5 = Math.floor( (j2 - j4)/30.6001 );
var d = Math.floor(j2 - j4 - Math.floor(j5*30.6001));
var m = Math.floor(j5 - 1);
if( m > 12 ) m -= 12;
var y = Math.floor(j3 - 4715);
if( m > 2 ) --y;
if( y <= 0 ) --y;
//
// get time of day from day fraction
//
var hr = Math.floor(dayfrac * 24.0);
var mn = Math.floor((dayfrac*24.0 - hr)*60.0);
f = ((dayfrac*24.0 - hr)*60.0 - mn)*60.0;
var sc = Math.floor(f);
f -= sc;
if( f > 0.5 ) ++sc;
if( y < 0 ) {
y = -y;
form.era[1].checked = true;
} else
form.era[0].checked = true;
form.year.value = y;
form.month[m-1].selected = true;
form.day[d-1].selected = d;
form.hour.value = hr;
form.minute.value = mn;
form.second.value = sc;
}
//-------
// calculate Julian date from calendar date or calendar date from Julian date
//-------
function JDcalc( form ) {
var era;
for( k=0; k < form.era.length; ++k ) {
if( form.era[k].checked ) {
era = form.era[k].value;
break;
}
}
var calctype;
for( k=0; k < form.calctype.length; ++k ) {
if( form.calctype[k].checked ) {
calctype = form.calctype[k].value;
break;
}
}
if( calctype == "JD" ) {
var m;
for( var k=0; k < form.month.length; ++k ) { //Netscape 4.7 is stoopid
if( form.month[k].selected ) {
m = k+1;
break;
}
}
var d;
for( var k=1; k <= form.day.length; ++k ) { //Netscape 4.7 is stoopid
if( form.day[k-1].selected ) {
d = k;
break;
}
}
var y = parseFloat(form.year.value);
// var m = parseFloat(form.month.value);
// var d = parseFloat(form.day.value);
var h = parseFloat(form.hour.value);
var mn = parseFloat(form.minute.value);
var s = parseFloat(form.second.value);
if( y < 0 ) {
y = -y;
era = "BCE";
form.year.value = y;
form.era[1].checked = true;
}
form.JDedit.value = cal_to_jd(era,y,m,d,h,mn,s);
} else {
jd_to_cal(form.JDedit.value,form);
}
//weekday
var weekday = new Array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
var t = parseFloat(form.JDedit.value) + 0.5;
var wd = Math.floor( (t/7 - Math.floor(t/7))*7 + 0.000000000317 ); //add 0.01 sec for truncation error correction
form.wkday.value = weekday[wd];
}
var month = new Array("January","February","March","April","May","June","July",
"August","September","October","November","December");
var numdays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
function ModifiedDate() {
updated = new Date(document.lastModified);
Month = month[updated.getMonth()];
Day = updated.getDate();
Year = updated.getYear();
Hour = updated.getHours();
Min = updated.getMinutes();
if( Year < 2000 ) Year += 1900;
if( Year < 2000 ) Year += 100; //Netscape 3 and IE 4.7 return 0 instead of 100 for 2000
if( Hour < 10 ) Hour = "0" + Hour;
if( Min < 10 ) Min = "0" + Min;
document.write("Last modified " + Month + " " + Day + ", " + Year + " at " + Hour + ":" + Min);
}
function set_form_fields(form) {
//grab the current USNO clock time
var today = new Date();
var year = today.getYear();
if( year < 2000 ) year += 1900;
if( year < 2000 ) year += 100; //Netscape 3 and IE 4.7 return 0 instead of 100 for 2000
var month = today.getMonth();
var day = today.getDate();
var hour = today.getHours();
var mins = today.getMinutes();
var secs = today.getSeconds();
//convert to UT
var TZ = today.getTimezoneOffset()/60;
hour += TZ;
if( hour >= 24 ) {
hour -= 24;
++day;
if( day > numdays[month-1] ) {
day = 1;
++month;
if( month > 11 ) {
month -= 12;
++year;
}
}
}
//set the form fields
form.year.value = year;
for( k=0; k < form.month.length; ++k ) { //Netscape 4.7 is stoopid
if( k == month ) {
form.month[k].selected = true;
break;
}
}
form.day.value = day;
for( k=1; k <= form.day.length; ++k ) { //Netscape 4.7 is stoopid
if( k == day ) {
form.day[k-1].selected = true;
break;
}
}
form.hour.value = hour;
form.minute.value = mins;
form.second.value = secs;
doJD(form);
}
function dodate(form) {
for( k=0; k < form.calctype.length; ++k ) {
if( form.calctype[k].value == "date" ) {
form.calctype[k].checked = true;
break;
}
}
}
function doJD(form) {
for( k=0; k < form.calctype.length; ++k ) {
if( form.calctype[k].value == "JD" ) {
form.calctype[k].checked = true;
break;
}
}
}
// from http://www.imcce.fr/langues/en/grandpublic/temps/jour_julien.php
function compute(form) {
MM=(form.nmonth.value=="")? "0" : eval(form.nmonth.value);
DD=(form.nday.value=="")? "0": eval(form.nday.value);
YY=(form.nyear.value=="") ? "0" :eval(form.nyear.value);
HR=(form.nhour.value=="")? "0" :eval(form.nhour.value);
MN=(form.nminute.value=="") ? "0" :eval(form.nminute.value);
SS=(form.nsecondes.value=="") ? "0" : eval(form.nsecondes.value);
with (Math) {
HR = HR + (MN / 60) + (SS / 3600);
GGG = 1;
if( YY < 1582 ) GGG = 0;
if( YY <= 1582 && MM < 10 ) GGG = 0;
if( YY <= 1582 && MM == 10 && DD < 5 ) GGG = 0;
JD = -1 * floor(7 * (floor((MM + 9) / 12) + YY) / 4);
S = 1;
if ((MM - 9)<0) S=-1;
A = abs(MM - 9);
J1 = floor(YY + S * floor(A / 7));
J1 = -1 * floor((floor(J1 / 100) + 1) * 3 / 4);
JD = JD + floor(275 * MM / 9) + DD + (GGG * J1);
JD = JD + 1721027 + 2 * GGG + 367 * YY - 0.5;
JD = JD + (HR / 24);
}
form.result.value = JD;
}
function reverse(form) {
JD = eval(form.result.value)
with (Math) {
Z = floor(JD+0.5);
F = JD+0.5 - Z;
if (Z < 2299161) {
A = Z
} else
{I = floor((Z - 1867216.25)/36524.25);
A = Z + 1 + I - floor(I/4);
}
B = A + 1524;
C = floor((B - 122.1)/365.25);
D = floor(365.25 * C);
T = floor((B - D)/ 30.6001);
RJ = B - D - floor(30.6001 * T) + F;
JJ = floor(RJ);
RH = (RJ - floor(RJ)) * 24;
Heure = floor(RH);
Mn = floor((RH - Heure )*60);
Sec = ((RH - Heure )*60 - Mn )*60;
if (T < 14) {
MM = T - 1
} else {
if ((T == 14) || (T == 15)) MM = T - 13
}
if (MM > 2) {
AA = C - 4716
} else {
if ((MM == 1) || (MM == 2)) AA = C - 4715
}
}
form.nmonth.value = MM;
form.nday.value = JJ;
form.nhour.value = Heure;
form.nyear.value = AA;
form.nminute.value= Mn;
form.nsecondes.value=Sec;
}
function Newdate(form) {
form.nmonth.value = "";
form.nday.value = "";
form.nyear.value = "";
form.nhour.value = "";
form.nminute.value = "";
form.nsecondes.value="";
form.nday.focus();
}
function startdate() {
currentDate()
}
function currentDate() {
Todays = new Date();
TheYear = Todays.getUTCFullYear() ;
TheMonth = (Todays.getUTCMonth() + 1);
TheDay = Todays.getUTCDate();
TheHour = Todays.getUTCHours();
TheMinu = Todays.getUTCMinutes();
TheSec = Todays.getUTCSeconds();
document.form.nyear.value = TheYear;
document.form.nmonth.value = TheMonth;
document.form.nday.value = TheDay;
document.form.nhour.value = TheHour;
document.form.nminute.value = TheMinu;
document.form.nsecondes.value = TheSec;
}
function Newday(form) {
form.result.value = "";
form.result.focus();
}