2 // tz in format [+-]HHMM
3 window.checkTimezone = function( tz, msg ) {
4 var localclock = new Date();
5 // returns negative offset from GMT in minutes
6 var tzRaw = localclock.getTimezoneOffset();
7 var tzHour = Math.floor( Math.abs( tzRaw ) / 60 );
8 var tzMin = Math.abs( tzRaw ) % 60;
9 var tzString = ( ( tzRaw >= 0 ) ? '-' : '+' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
10 if ( tz != tzString ) {
11 var junk = msg.split('$1');
12 document.write( junk[0] + 'UTC' + tzString + junk[1] );
16 window.timezoneSetup = function() {
17 var tzSelect = document.getElementById( 'mw-input-wptimecorrection' );
18 var tzTextbox = document.getElementById( 'mw-input-wptimecorrection-other' );
20 if ( tzSelect && tzTextbox ) {
21 addHandler( tzSelect, 'change', function( e ) { updateTimezoneSelection( false ); } );
22 addHandler( tzTextbox, 'blur', function( e ) { updateTimezoneSelection( true ); } );
25 updateTimezoneSelection( false );
28 // in [-]HH:MM format...
29 // won't yet work with non-even tzs
30 window.fetchTimezone = function() {
31 // FIXME: work around Safari bug
32 var localclock = new Date();
33 // returns negative offset from GMT in minutes
34 var tzRaw = localclock.getTimezoneOffset();
35 var tzHour = Math.floor( Math.abs( tzRaw ) / 60 );
36 var tzMin = Math.abs( tzRaw ) % 60;
37 var tzString = ( ( tzRaw >= 0 ) ? '-' : '' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
38 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
42 window.guessTimezone = function() {
43 var textbox = document.getElementById( 'mw-input-wptimecorrection-other' );
44 var selector = document.getElementById( 'mw-input-wptimecorrection' );
46 selector.value = 'other';
47 textbox.value = fetchTimezone();
48 textbox.disabled = false; // The changed handler doesn't trip, obviously.
49 updateTimezoneSelection( true );
52 window.updateTimezoneSelection = function( force_offset ) {
53 var selector = document.getElementById( 'mw-input-wptimecorrection' );
55 if ( selector.value == 'guess' ) {
56 return guessTimezone();
59 var textbox = document.getElementById( 'mw-input-wptimecorrection-other' );
60 var localtimeHolder = document.getElementById( 'wpLocalTime' );
61 var servertime = document.getElementsByName( 'wpServerTime' )[0].value;
64 // Compatibility code.
65 if ( !selector.value ) {
66 selector.value = selector.options[selector.selectedIndex].value;
69 // Handle force_offset
71 selector.value = 'other';
75 if ( selector.value == 'other' ) {
76 // Grab data from the textbox, parse it.
77 var diffArr = textbox.value.split(':');
78 if ( diffArr.length == 1 ) {
79 // Specification is of the form [-]XX
80 minDiff = parseInt( diffArr[0], 10 ) * 60;
82 // Specification is of the form [-]XX:XX
83 minDiff = Math.abs( parseInt( diffArr[0], 10 ) ) * 60 + parseInt( diffArr[1], 10 );
84 if ( parseInt( diffArr[0], 10 ) < 0 ) {
89 // Grab data from the selector value
90 var diffArr = selector.value.split('|');
91 minDiff = parseInt( diffArr[1], 10 );
94 // Gracefully handle non-numbers.
95 if ( isNaN( minDiff ) ) {
99 // Determine local time from server time and minutes difference, for display.
100 var localTime = parseInt( servertime, 10 ) + minDiff;
102 // Bring time within the [0,1440) range.
103 while ( localTime < 0 ) {
106 while ( localTime >= 1440 ) {
110 // Split to hour and minute
111 var hour = String( Math.floor( localTime / 60 ) );
112 if ( hour.length < 2 ) {
115 var min = String(localTime%60);
116 if ( min.length < 2 ) {
119 changeText( localtimeHolder, hour + ':' + min );
121 // If the user selected from the drop-down, fill the offset field.
122 if ( selector.value != 'other' ) {
123 hour = String( Math.abs( Math.floor( minDiff / 60 ) ) );
124 if ( hour.length < 2 ) {
130 min = String(minDiff%60);
131 if ( min.length < 2 ) {
134 textbox.value = hour + ':' + min;
138 addOnloadHook( timezoneSetup );