/[drupal]/contributions/modules/worldclock/worldclock.js
ViewVC logotype

Contents of /contributions/modules/worldclock/worldclock.js

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Sat Apr 19 01:39:19 2008 UTC (19 months, 1 week ago) by pixture
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, DRUPAL-6--1-0, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
File MIME type: text/javascript
*** empty log message ***
1 <!--
2
3 var timerId;
4 var delay=1000;
5 var dst_str;
6
7 //--------------------------------------------------------------------
8 function DisplayClock(maxnum, showsec, showdst, param)
9 {
10 // In: maxnum 1 to 8 (number of locations to display)
11 // showsec 0 or 1
12 // showdst 0 or 1
13 // param array - param[1], param[2], param[3], param[4] ....
14 var loc, diff, dst, i;
15
16 for(i = 1 ; i <= maxnum ; i++) {
17 loc = param[i];
18 // convert string parameters to integer
19 for(j = 0; j <= 11; j++) {
20 switch(j) {
21 case 1: // loc_name (= string)
22 continue;
23 case 2: // loc_diff (= float)
24 loc[j] = parseFloat(loc[j]);
25 break;
26 default: // others (= integer)
27 loc[j] = parseInt(loc[j]);
28 break;
29 }
30 }
31 // ---- loc array ----
32 // [0] loc_no
33 // [1] loc_name
34 // [2] loc_diff
35 // [3] loc_dst
36 // [4] loc_smon
37 // [5] loc_sth
38 // [6] loc_sdow
39 // [7] loc_stime
40 // [8] loc_emon
41 // [9] loc_eth
42 // [10] loc_edow
43 // [11] loc_etime
44 if(loc[0] == 0) continue;
45 diff = loc[2];
46 dst = loc[3];
47 if(showdst) {
48 if(dst == 1) {
49 if(IsDST(loc)) {
50 diff++;
51 dst_str = " *";
52 }
53 else {
54 dst_str = " ";
55 }
56 }
57 else {
58 dst_str = " ";
59 }
60 }
61 else {
62 dst_str = "";
63 }
64 idname = "worldclock" + i;
65 document.getElementById(idname).innerHTML
66 = GenerateTimeString(diff, showsec) + dst_str;
67 }
68 timerId = setTimeout("DisplayClock(maxnum, showsec, showdst, param)", delay);
69 }
70
71 //--------------------------------------------------------------------
72 function IsDST(loc)
73 {
74 // NOTE: this function is called only for the countries
75 // which has the summer time (DST)
76 // ---- loc array ----
77 // [0] loc_no
78 // [1] loc_name
79 // [2] loc_diff
80 // [3] loc_dst
81 // [4] loc_smon
82 // [5] loc_sth
83 // [6] loc_sdow
84 // [7] loc_stime
85 // [8] loc_emon
86 // [9] loc_eth
87 // [10] loc_edow
88 // [11] loc_etime
89 var localDate, localTime, localYear, startYear, endYear;
90 var tzDiff, tzOffset, dstStartGMT, dstEndGMT, nowGMT;
91
92 // calculate time difference from GMT (in minute)
93 tzDiff = loc[2] * 60;
94
95 localDate = new Date();
96 localTime = localDate.getTime(); // in msec
97 localYear = localDate.getFullYear();
98 startYear = endYear = localYear;
99 if(loc[4] > loc[8]) {
100 // e.g DST starts Nov 1 and ends March 30 of the next year
101 // This happens with the countries in southern half
102 // such as Australia, New Zealand
103 endYear++;
104 }
105
106 // time difference between GMT and local time in minute
107 tzOffset = localDate.getTimezoneOffset();
108
109 dstStartGMT = GetDstTime(startYear, loc[4], loc[5], loc[6], loc[7]);
110 dstEndGMT = GetDstTime(endYear, loc[8], loc[9], loc[10], loc[11]);
111
112 nowGMT = localTime + (tzOffset + tzDiff) * 60 * 1000;
113
114 if((dstStartGMT <= nowGMT) && (nowGMT < dstEndGMT)) {
115 return 1;
116 }
117 return 0;
118 }
119
120 //--------------------------------------------------------------------
121 function GenerateTimeString(nDiff, bShowSec)
122 {
123 var dtNowLocal = new Date;
124 var dtNow = new Date;
125 var strHour, strMin, strSec, strAmPm, strColon;
126 var strMon, strDate;
127 var strDay = new Array("SUN", "MON", "TUE", "WED", "THR", "FRI", "SAT");
128
129 var today = new Date();
130 var year = today.getFullYear();
131
132 dtNow.setTime(dtNowLocal.getTime() + (dtNowLocal.getTimezoneOffset() + (nDiff * 60)) * 60 * 1000);
133
134 strMon = dtNow.getMonth() + 1;
135 strDate = dtNow.getDate();
136 if(strMon < 10) strMon = " " + strMon;
137 if(strDate < 10) strDate = " " + strDate;
138
139 strHour = dtNow.getHours();
140 strMin = dtNow.getMinutes();
141 strSec = dtNow.getSeconds();
142 strAmPm = ((strHour < 12)? " AM": " PM");
143 if(strHour >= 12) strHour -= 12;
144 if(strHour < 10) strHour = "0" + strHour;
145 if(strMin < 10) strMin = "0" + strMin;
146 if(strSec < 10) strSec = "0" + strSec;
147 strColon = ":";
148 //-- flashing colon
149 // strColon = ((strSec % 2)? ":" : " ");
150
151 if(bShowSec) {
152 return(strMon+"/"+strDate+"&nbsp;&nbsp;"+strHour+strColon+strMin+strColon+strSec+strAmPm);
153 }
154 else {
155 return(strMon+"/"+strDate+"&nbsp;&nbsp;"+strHour+strColon+strMin+strAmPm);
156 }
157 }
158
159 //--------------------------------------------------------------------
160 function GetDstTime(year, mon, th, dow, tim)
161 {
162 var dst_day;
163 var hit_cnt;
164 var i;
165 var numDays = new Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
166
167 if(th == 9) { // last
168 dst_day = new Date(year, mon-1, numDays[mon], tim, 0, 0);
169 // decrement date starting from the last day of the month
170 for (i = numDays[mon]; i >= 1; i--) {
171 dst_day.setDate(i);
172 if (dst_day.getDay() == dow) {
173 break; // hit!
174 }
175 }
176 }
177 else { // 1st, 2nd, 3rd, 4th..
178 dst_day = new Date(year, mon-1, 1, tim, 0, 0);
179 // increment date starting from the first day of the month
180 for (i = 1, hit_cnt = 0; i <= 31; i++) {
181 dst_day.setDate(i);
182 if (dst_day.getDay() == dow) {
183 hit_cnt++; // hit!
184 if(hit_cnt == th) {
185 break;
186 }
187 }
188 }
189 }
190 return dst_day.getTime();
191 }
192
193 // -->

  ViewVC Help
Powered by ViewVC 1.1.2