Timezone.js

1       /*
2        * Sybase Hybrid App version 2.3.4
3        *
4        * Timezone.js
5        * This file will not be regenerated, so it is possible to modify it, but it
6        * is not recommended.
7        *
8        * Copyright (c) 2012 Sybase Inc. All rights reserved.
9        */
10      
11      /**
12      * The namespace for the Hybrid Web Container javascript
13      * @namespace
14      */
15      hwc = (typeof hwc === "undefined" || !hwc) ? {} : hwc;      // SUP 'namespace'
16      
17      (function(hwc, window, undefined) {
18      
19      /**
20       * Returns the current locale. The platform's locale string should be available. However, if it is
21       * missing the function queries available JavaScript APIs for a suitable value.
22       * @desc Timezone
23       * @memberOf hwc
24       * @public
25       * @returns {string} Returns a string containing the current locale, or null if it is not available.
26       * @example
27       * var sLocale = hwc.getCurrentLocale();
28       *
29       */
30      hwc.getCurrentLocale = function() {
31      	hwc.traceEnteringMethod("hwc.getCurrentLocale");
32      	
33      	try {
34      		if(hwc.lang) {
35      			return hwc.lang;
36      		}
37      		else {
38      		   if ( navigator ) {
39      				if ( navigator.language ) {
40      					if (hwc.isAndroid()) {
41      						return navigator.userAgent.match(/Android \d+(?:\.\d+){1,2}; [a-z]{2}-[a-z]{2}/).toString().match(/[a-z]{2}-[a-z]{2}/).toString();
42      					}
43      					else {
44      						return navigator.language;
45      					}
46      				}
47      				else if ( navigator.browserLanguage ) {
48      					return navigator.browserLanguage;
49      				}
50      				else if ( navigator.systemLanguage ) {
51      					return navigator.systemLanguage;
52      				}
53      				else if ( navigator.userLanguage ) {
54      					return navigator.userLanguage;
55      				}
56      			}
57      		}
58      	} finally {
59      		hwc.traceLeavingMethod("hwc.getCurrentLocale");
60      	}
61      };
62      
63      /**
64       * Returns a localized representation of the given Date object. Queries the platform OS for a locale-
65       * formatted date/time string.
66       * @desc Timezone
67       * @memberOf hwc
68       * @public
69       * @param {Date} date Date to be localized, initialized to some valid time.
70       * @returns {string} Returns a localized date/time string, or undefined if platform is unsupported.
71       * @example
72       * var sDT = hwc.getLocalizedDateTime( date );
73       *
74       */
75      hwc.getLocalizedDateTime = function( date ) {
76          var result, dMilliseconds, sTzId, response;
77      	hwc.traceEnteringMethod("hwc.getLocalizedDateTime");
78      	try {
79      		if (hwc.isAndroid()) {
80      			dMilliseconds = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds() );
81      			sTzId = _HWC.getLocalizedDateTime( dMilliseconds )+ '';
82      			result = sTzId;
83      		}
84      		else if (hwc.isWindowsMobile()) {
85      			// Feature was not needed on this platform
86      			result = undefined;
87      		}
88      		else if (hwc.isIOS()) {
89      			dMilliseconds = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds() );
90      			response = hwc.getDataFromContainer("tz", "&command=tzdatetime&time=" + dMilliseconds);
91      			result = (response);
92      		}
93      		else if (hwc.isBlackBerry()) {
94      			dMilliseconds = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds() );
95      			sTzId = TimeZone.tzdatetime( dMilliseconds );
96      			result = sTzId;
97      		}
98      		else {
99      			result = undefined;
100     		}
101     		return result;
102     	} finally {
103     		hwc.traceLeavingMethod("hwc.getLocalizedDateTime");
104     	}
105     };
106     
107     /**
108      * Returns a localized representation of the given Date object. Queries the platform OS for a locale-
109      * formatted date string.
110      * @desc Timezone
111      * @memberOf hwc
112      * @public
113      * @param {Date} date Date to be localized, initialized to some valid time.
114      * @returns {string} Returns a localized date string, or undefined if platform is unsupported.
115      * @example
116      * var sD = hwc.getLocalizedDate( date );
117      *
118      */
119     hwc.getLocalizedDate = function( date ) {
120         var dMilliseconds, sTzId, response, result;
121     	hwc.traceEnteringMethod("hwc.getLocalizedDate");
122     	try {
123     		if (hwc.isAndroid()) {
124     			dMilliseconds = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), 12, 0, 0 );
125     			sTzId = _HWC.getLocalizedDate( dMilliseconds ) + '';
126     			result = sTzId;
127     		}
128     		else if (hwc.isWindowsMobile()) {
129     			// Feature was not needed on this platform
130     			result = undefined;
131     		}
132     		else if (hwc.isIOS()) {
133     			dMilliseconds = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), 12, 0, 0 );
134     			response = hwc.getDataFromContainer("tz", "&command=tzdate&time=" + dMilliseconds);
135     			result = (response);
136     		}
137     		else if (hwc.isBlackBerry()){
138     			dMilliseconds = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), 12, 0, 0 );
139     			sTzId = TimeZone.tzdate( dMilliseconds );
140     			result = sTzId;
141     		}
142     		else {
143     			result = undefined;
144     		}
145     		return result;
146     	} finally {
147     		hwc.traceLeavingMethod("hwc.getLocalizedDate");
148     	}
149     };
150     
151     /**
152      * Returns a localized representation of the given Date object. Queries the platform OS for a locale-
153      * formatted time string.
154      * @desc Timezone
155      * @memberOf hwc
156      * @public
157      * @param {Date} date Date to be localized, initialized to some valid time.
158      * @returns {string} Returns a localized time string, or undefined if platform is unsupported.
159      * @example
160      * var sT = hwc.getLocalizedTime( date );
161      *
162      */
163     hwc.getLocalizedTime = function( date ) {
164         var dMilliseconds, sTzId, response, result;
165     	hwc.traceEnteringMethod("hwc.getLocalizedTime");
166     	try {
167     		if (hwc.isAndroid()) {
168     			dMilliseconds = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds() );
169     			sTzId = _HWC.getLocalizedTime( dMilliseconds ) + '';
170     			result = sTzId;
171     		}
172     		else if (hwc.isWindowsMobile()) {
173     			// Feature was not needed on this platform
174     			result = undefined;
175     		}
176     		else if (hwc.isIOS()) {
177     			dMilliseconds = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds() );
178     			response = hwc.getDataFromContainer("tz", "&command=tztime&time=" + dMilliseconds);
179     			result = (response);
180     		}
181     		else if (hwc.isBlackBerry()){
182     			dMilliseconds = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds() );
183     			sTzId = TimeZone.tztime( dMilliseconds );
184     			result = sTzId;
185     		}
186     		else if (hwc.isWindows()){
187     			// For debugging on a browser of windows platform
188     			result = date.toString();
189     		}
190     		else {
191     			result = undefined;
192     		}
193     		return result;
194     	} finally {
195     		hwc.traceLeavingMethod("hwc.getLocalizedTime");
196     	}
197     };
198     
199     /**
200      * Converts the given Date object to the device's local time, and returns the new Date.
201      * @desc Timezone
202      * @memberOf hwc
203      * @public
204      * @param {Date} date Date to be converted, initialized to some valid UTC time.
205      * @returns {Date} Returns the converted Date object.
206      * @example
207      * var localDate = hwc.convertUtcToLocalTime( date );
208      *
209      */
210     hwc.convertUtcToLocalTime = function( date )
211     {
212     	hwc.traceEnteringMethod("hwc.convertUtcToLocalTime");
213     	try {
214     		var iMilliseconds, totalOffsetInMinutes, time, localDate;
215     		iMilliseconds = date.valueOf();
216     		totalOffsetInMinutes = hwc.getOffsetFromUTC( date );
217     		totalOffsetInMinutes = totalOffsetInMinutes * 60000;
218     		time = iMilliseconds + totalOffsetInMinutes;
219     		localDate = new Date();
220     		localDate.setTime( time );
221     		return localDate;
222     	} finally {
223     		hwc.traceLeavingMethod("hwc.convertUtcToLocalTime");
224     	}
225     };
226     
227     /**
228      * Converts the given Date object to UTC time, and returns the new Date.
229      * @desc Timezone
230      * @memberOf hwc
231      * @public
232      * @param {Date} date Date to be converted, initialized to some valid local time.
233      * @returns {Date} Returns the converted Date object.
234      * @example
235      * var utcDate = hwc.convertLocalTimeToUtc( date );
236      *
237      */
238     hwc.convertLocalTimeToUtc = function( date )
239     {
240     	hwc.traceEnteringMethod("hwc.convertLocalTimeToUtc");
241     	try {
242     		var iMilliseconds, totalOffsetInMinutes, time, utcDate;
243     		iMilliseconds = date.valueOf();
244     		totalOffsetInMinutes = hwc.getOffsetFromUTC( date );
245     		totalOffsetInMinutes = totalOffsetInMinutes * 60000;
246     		time = iMilliseconds - totalOffsetInMinutes;
247     		utcDate = new Date();
248     		utcDate.setTime( time );
249     		return utcDate;
250     	} finally {
251     		hwc.traceLeavingMethod("hwc.convertLocalTimeToUtc");
252     	}
253     };
254     
255     /**
256      * Returns the total offset (difference) between the given "local" time and UTC including any daylight
257      * savings offsets if applicable. Example: if the device was in London timezone (Gmt +1) and it is
258      * currently practicing DST, the function would return "120": 60 minutes normal offset plus 60 minutes
259      * for its daylight savings offset.
260      * @desc Timezone
261      * @memberOf hwc
262      * @public
263      * @param {Date} date Date at which time to determine offset, initialized to some valid time.
264      * @returns {int} Returns the GMT offset in minutes.
265      * @example
266      * var totalOffset = hwc.getOffsetFromUTC(date);
267      *
268      */
269     hwc.getOffsetFromUTC = function( date )
270     {
271         var lMilliseconds, iMilliseconds, iMinutesOffset, response, dt,
272             year,month, day, hour, minute, second, request, d,
273             dMilliseconds, result;
274             
275     	hwc.traceEnteringMethod("hwc.getOffsetFromUTC");
276     	try {
277     		if (hwc.isAndroid()) {
278     			lMilliseconds = date.getTime();
279     			iMinutesOffset = _HWC.getOffsetFromUTC(lMilliseconds);
280     			result = iMinutesOffset;
281     		}
282     		else if (hwc.isWindows()) {
283     			dt = new Date();
284     			iMinutesOffset = dt.getTimezoneOffset() * (-1);
285     			result = iMinutesOffset;
286     		}
287     		else if (hwc.isWindowsMobile())
288     		{
289     			// JavaScript's Date and WM's DateTime objects differs in their base starting time
290     			// and definition.  It was necessary to pass a "time" to the OS - see below comment
291     			lMilliseconds = date.getTime();
292     			// Rather than pass a date string (which might be in a different locale format)
293     			// the raw parameters of the particular "date" are sent
294     			// this also avoids a date string parse on the OS side.
295     			year = date.getFullYear();
296     			month = date.getMonth() + 1;
297     			day = date.getDate();
298     			hour = date.getHours();
299     			minute = date.getMinutes();
300     			second = date.getSeconds();
301     			request = "utcoffset=utcoffset&";
302     			request += "year=";
303     			request += year.toString();
304     			request += "&";
305     			request += "month=";
306     			request += month.toString();
307     			request += "&";
308     			request += "day=";
309     			request += day.toString();
310     			request += "&";
311     			request += "hour=";
312     			request += hour.toString();
313     			request += "&";
314     			request += "minute=";
315     			request += minute.toString();
316     			request += "&";
317     			request += "second=";
318     			request += second.toString();
319     
320     			  response = hwc.postDataToContainer("tz", request);
321     			  d = response * 1;
322     			  iMinutesOffset = d;
323     
324     			result = iMinutesOffset;
325     		}
326     		else if (hwc.isBlackBerry()){
327     			dMilliseconds = date.getTime();
328     			iMinutesOffset = TimeZone.totaloffset(dMilliseconds);
329     			result = iMinutesOffset;
330     		}
331     		else if (hwc.isIOS()) {
332     			lMilliseconds = date.getTime();
333     			result = hwc.getDataFromContainer("tz", "&command=utcoffset&time=" + lMilliseconds);
334     		}
335     		else {
336     			result = undefined;
337     		}
338     		return result;
339     	} finally {
340     		hwc.traceLeavingMethod("hwc.getOffsetFromUTC");
341     	}
342     };
343     
344     /**
345      * Returns whether daylight savings rules are in effect for the current timezone at the given time.
346      * @desc Timezone
347      * @memberOf hwc
348      * @public
349      * @param {Date} date Date at which to determine whether daylight savings is in effect.
350      * @returns {boolean} Returns true iff daylight savings rules are in effect at the given time in the
351      * current timezone.
352      * @example
353      * var isAwareAtTime = hwc.isDstActiveAtGivenTime(date);
354      *
355      */
356     hwc.isDstActiveAtGivenTime = function( date )
357     {
358         var lMilliseconds, iMilliseconds, iMinutesOffset, response, dt,
359             year,month, day, hour, minute, second, request, d,
360             dMilliseconds, result;
361     	hwc.traceEnteringMethod("hwc.isDstActiveAtGivenTime");
362     	try {
363     		if (hwc.isAndroid()) {
364     			iMilliseconds = date.getTime();
365     			result = _HWC.isDstActiveAtGivenTime(iMilliseconds);
366     		}
367     		else if (hwc.isWindowsMobile())
368     		{
369     			// JavaScript's Date and WM's DateTime objects differs in their base starting time
370     			// and definition.  It was necessary to pass a "time" to the OS - see below comment
371     			lMilliseconds = date.getTime();
372     			// Rather than pass a date string (which might be in a different locale format)
373     			// the raw parameters of the particular "date" are sent
374     			// this also avoids a date string parse on the OS side.
375     			request = "indst=indst&";
376     			response = undefined;
377     			year = date.getFullYear();
378     			month = date.getMonth() + 1;
379     			day = date.getDate();
380     			hour = date.getHours();
381     			minute = date.getMinutes();
382     			second = date.getSeconds();
383     
384     			request += "year=";
385     			request += year.toString();
386     			request += "&";
387     			request += "month=";
388     			request += month.toString();
389     			request += "&";
390     			request += "day=";
391     			request += day.toString();
392     			request += "&";
393     			request += "hour=";
394     			request += hour.toString();
395     			request += "&";
396     			request += "minute=";
397     			request += minute.toString();
398     			request += "&";
399     			request += "second=";
400     			request += second.toString();
401     
402     			response = hwc.postDataToContainer("tz", request);
403     
404     			result = (response === 'true');
405     		}
406     		else if (hwc.isBlackBerry()){
407     			dMilliseconds = date.getTime();
408     			result = TimeZone.indst(dMilliseconds);
409     		}
410     		else if (hwc.isIOS()) {
411     			lMilliseconds = date.getTime();
412     			response = hwc.getDataFromContainer("tz", "&command=indst&time=" + lMilliseconds);
413     			result = (hwc.parseBoolean(response));
414     		}
415     		else {
416     			result = false;
417     		}
418     		return result;
419     	} finally {
420     		hwc.traceLeavingMethod("hwc.isDstActiveAtGivenTime");
421     	}
422     };
423     
424     /**
425      * Returns the daylight savings offset in minutes for the current timezone at the given time.
426      * Example: for Mountain Standard Time, at March 31st (currently is practicing DST), the returned offset is 60.
427      * Example: for Mountain Standard Time, at November 31st (currently is not practicing DST), the returned offset is 0.
428      * @desc Timezone
429      * @memberOf hwc
430      * @public
431      * @param {Date} date Date at which to determine daylight savings offset.
432      * @returns {int} Returns the number of minutes offset for daylight savings for the current
433      * timezone and at the given Date, or 0 if the current timezone doesn't practice daylight savings.
434      * @example
435      * var iDstOffsetAtTime = hwc.getDstOffsetAtGivenTimeInMinutes(date);
436      *
437      */
438     hwc.getDstOffsetAtGivenTimeInMinutes = function ( date )
439     {
440         var lMilliseconds, iMilliseconds, iMinutesOffset, response, dt,
441             year,month, day, hour, minute, second, request, d,
442             dMilliseconds, result;
443             
444     	hwc.traceEnteringMethod("hwc.getDstOffsetAtGivenTimeInMinutes");
445     	try {
446     		if (hwc.isAndroid()) {
447     			iMilliseconds = date.getTime();
448     			iMinutesOffset = _HWC.getDstOffsetAtGivenTimeInMinutes(iMilliseconds);
449     			result = iMinutesOffset;
450     		}
451     		else if (hwc.isWindowsMobile())
452     		{
453     			// JavaScript's Date and WM's DateTime objects differs in their base starting time
454     			// and definition.  It was necessary to pass a "time" to the OS - see below comment
455     			lMilliseconds = date.getTime();
456     			// Rather than pass a date string (which might be in a different locale format)
457     			// the raw parameters of the particular "date" are sent
458     			// this also avoids a date string parse on the OS side.
459     			request = "dstoffset=dstoffset&";
460     			year = date.getFullYear();
461     			month = date.getMonth() + 1;
462     			day = date.getDate();
463     			hour = date.getHours();
464     			minute = date.getMinutes();
465     			second = date.getSeconds();
466     
467     			request += "year=";
468     			request += year.toString();
469     			request += "&";
470     			request += "month=";
471     			request += month.toString();
472     			request += "&";
473     			request += "day=";
474     			request += day.toString();
475     			request += "&";
476     			request += "hour=";
477     			request += hour.toString();
478     			request += "&";
479     			request += "minute=";
480     			request += minute.toString();
481     			request += "&";
482     			request += "second=";
483     			request += second.toString();
484     
485     			response = hwc.postDataToContainer("tz", request);
486     			d = response * 1;
487     			iMinutesOffset = d;
488     
489     			result = iMinutesOffset;
490     		}
491     		else if (hwc.isBlackBerry()){
492     			dMilliseconds = date.getTime();
493     			iMinutesOffset = TimeZone.dstoffset(dMilliseconds);
494     			result = iMinutesOffset;
495     		}
496     		else if (hwc.isIOS()) {
497     			lMilliseconds = date.getTime();
498     			response = hwc.getDataFromContainer("tz", "&command=dstoffset&time=" + lMilliseconds);
499     			result = parseInt(response, 10);
500     		}
501     		else {
502     			result = undefined;
503     		}
504     		return result;
505     	} finally {
506     		hwc.traceLeavingMethod("hwc.getDstOffsetAtGivenTimeInMinutes");
507     	}
508     };
509     
510     /**
511      * Returns a string containing the current Timezone's standard name. The name will not change based
512      * on daylight savings periods. The native OS returns the string in the current locale where applicable.
513      * Currently this string is derived from using available platform OS APIs. The values for the same
514      * timezone will be different among platforms.
515      * @desc Timezone
516      * @memberOf hwc
517      * @public
518      * @returns {string} Returns a string containing the current Timezone's standard name.
519      * @example
520      * var sTzId = hwc.getTimezoneId();
521      *
522      */
523     hwc.getTimezoneId = function () {
524         var sTzId, request, response, result;
525         
526     	hwc.traceEnteringMethod("hwc.getTimezoneId");
527     	try {
528     		if (hwc.isAndroid()) {
529     			sTzId = _HWC.getTimezoneId() + '';
530     			result = sTzId;
531     		}
532     		else if (hwc.isWindowsMobile())
533     		{
534     			request = "tzid=tzid";
535     			response = hwc.postDataToContainer("tz", request);
536     			result = response;
537     		}
538     		else if (hwc.isIOS()) {
539     			response = hwc.getDataFromContainer("tz", "&command=tzid");
540     			result = (response);
541     		}
542     		else if (hwc.isBlackBerry()){
543     			sTzId = TimeZone.tzid();
544     			result = sTzId;
545     		}
546     		else {
547     			result = undefined;
548     		}
549     		return result;
550     	} finally {
551     		hwc.traceLeavingMethod("hwc.getTimezoneId");
552     	}
553     };
554     
555     /**
556      * Returns whether the device's current timezone practices daylight savings. If a device's current
557      * timezone never practices daylight savings, this function returns "false". If a device's current
558      * timezone practices DST, but DST rules are not currently in effect, function returns "true".
559      * @desc Timezone
560      * @memberOf hwc
561      * @public
562      * @returns {boolean} Returns true iff the device's current timezone practices daylight savings,
563      * irrespective of whether daylight savings is currently in effect.
564      * @example
565      * var isDstAware = hwc.getUsesDST();
566      *
567      */
568     hwc.getUsesDST = function () {
569         var date, lMilliseconds, request, response, result;
570         
571     	hwc.traceEnteringMethod("hwc.getUsesDST");
572     	try {
573     		if (hwc.isAndroid()) {
574     			result = _HWC.useDaylightTimeCurrently();
575     		}
576     		else if (hwc.isWindowsMobile())
577     		{
578     			date = new Date();
579     			lMilliseconds = date.getTime();
580     			request = "dstaware=";
581     			response = undefined;
582     
583     			request += lMilliseconds.toString();  // left for potential future use
584     
585     			response = hwc.postDataToContainer("tz", request);
586     			result = (response === 'true');
587     		}
588     		else if (hwc.isIOS()) {
589     			response = hwc.getDataFromContainer("tz", "&command=dstaware");
590     			result = hwc.parseBoolean(response);
591     		}
592     		else if (hwc.isBlackBerry()){
593     			result = TimeZone.dstaware();
594     		}
595     		return result;
596     	} finally {
597     		hwc.traceLeavingMethod("hwc.getUsesDST");
598     	}
599     };
600     
601     })(hwc, window);
602