2 * JavaScript for Special:Preferences: Timezone field enhancements.
7 $tzSelect
, $tzTextbox
, $localtimeHolder
, servertime
;
10 // Guesses Timezone from browser and updates fields onchange.
12 $tzSelect
= $( '#mw-input-wptimecorrection' );
13 $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
14 $localtimeHolder
= $( '#wpLocalTime' );
15 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
17 function minutesToHours( min
) {
18 var tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
19 tzMin
= Math
.abs( min
) % 60,
20 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
21 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
25 function hoursToMinutes( hour
) {
27 arr
= hour
.split( ':' );
29 arr
[ 0 ] = parseInt( arr
[ 0 ], 10 );
31 if ( arr
.length
=== 1 ) {
32 // Specification is of the form [-]XX
33 minutes
= arr
[ 0 ] * 60;
35 // Specification is of the form [-]XX:XX
36 minutes
= Math
.abs( arr
[ 0 ] ) * 60 + parseInt( arr
[ 1 ], 10 );
41 // Gracefully handle non-numbers.
42 if ( isNaN( minutes
) ) {
49 function updateTimezoneSelection() {
50 var minuteDiff
, localTime
,
51 type
= $tzSelect
.val();
53 if ( type
=== 'other' ) {
54 // User specified time zone manually in <input>
55 // Grab data from the textbox, parse it.
56 minuteDiff
= hoursToMinutes( $tzTextbox
.val() );
58 // Time zone not manually specified by user
59 if ( type
=== 'guess' ) {
60 // Get browser timezone & fill it in
61 minuteDiff
= -( new Date().getTimezoneOffset() );
62 $tzTextbox
.val( minutesToHours( minuteDiff
) );
63 $tzSelect
.val( 'other' );
65 // Grab data from the $tzSelect value
66 minuteDiff
= parseInt( type
.split( '|' )[ 1 ], 10 ) || 0;
70 // Determine local time from server time and minutes difference, for display.
71 localTime
= servertime
+ minuteDiff
;
73 // Bring time within the [0,1440) range.
74 localTime
= ( ( localTime
% 1440 ) + 1440 ) % 1440;
76 $localtimeHolder
.text( mw
.language
.convertNumber( minutesToHours( localTime
) ) );
79 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
80 $tzSelect
.change( updateTimezoneSelection
);
81 $tzTextbox
.blur( updateTimezoneSelection
);
82 updateTimezoneSelection();
86 }( mediaWiki
, jQuery
) );