Timezone.js

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