@@ -32,6 +32,8 @@ function Timepicker() {
3232 currentText : 'Now' ,
3333 closeText : 'Done' ,
3434 ampm : false ,
35+ amNames : [ 'AM' , 'A' ] ,
36+ pmNames : [ 'PM' , 'P' ] ,
3537 timeFormat : 'hh:mm tt' ,
3638 timeSuffix : '' ,
3739 timeOnlyTitle : 'Choose Time' ,
@@ -158,6 +160,8 @@ $.extend(Timepicker.prototype, {
158160 } ,
159161 timepicker : tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
160162 } ) ;
163+ tp_inst . amNames = $ . map ( tp_inst . _defaults . amNames , function ( val ) { return val . toUpperCase ( ) } ) ;
164+ tp_inst . pmNames = $ . map ( tp_inst . _defaults . pmNames , function ( val ) { return val . toUpperCase ( ) } ) ;
161165
162166 if ( tp_inst . _defaults . timezoneList === null ) {
163167 var timezoneList = [ ] ;
@@ -225,10 +229,11 @@ $.extend(Timepicker.prototype, {
225229 . replace ( / m { 1 , 2 } / ig, '(\\d?\\d)' )
226230 . replace ( / s { 1 , 2 } / ig, '(\\d?\\d)' )
227231 . replace ( / l { 1 } / ig, '(\\d?\\d?\\d)' )
228- . replace ( / t { 1 , 2 } / ig, '(am|pm|a|p)?' )
232+ . replace ( / t { 1 , 2 } / ig, this . _getPatternAmpm ( ) )
229233 . replace ( / z { 1 } / ig, '(z|[-+]\\d\\d:?\\d\\d)?' )
230234 . replace ( / \s / g, '\\s?' ) + this . _defaults . timeSuffix + '$' ,
231235 order = this . _getFormatPositions ( ) ,
236+ ampm = '' ,
232237 treg ;
233238
234239 if ( ! this . inst ) this . inst = $ . datepicker . _getInst ( this . $input [ 0 ] ) ;
@@ -245,15 +250,20 @@ $.extend(Timepicker.prototype, {
245250 treg = timeString . match ( new RegExp ( regstr , 'i' ) ) ;
246251
247252 if ( treg ) {
248- if ( order . t !== - 1 )
249- this . ampm = ( ( treg [ order . t ] === undefined || treg [ order . t ] . length === 0 ) ?
250- '' :
251- ( treg [ order . t ] . charAt ( 0 ) . toUpperCase ( ) == 'A' ) ? 'AM' : 'PM' ) . toUpperCase ( ) ;
253+ if ( order . t !== - 1 ) {
254+ if ( treg [ order . t ] === undefined || treg [ order . t ] . length === 0 ) {
255+ ampm = '' ;
256+ this . ampm = '' ;
257+ } else {
258+ ampm = $ . inArray ( treg [ order . t ] . toUpperCase ( ) , this . amNames ) !== - 1 ? 'AM' : 'PM' ;
259+ this . ampm = this . _defaults [ ampm == 'AM' ? 'amNames' : 'pmNames' ] [ 0 ] ;
260+ }
261+ }
252262
253263 if ( order . h !== - 1 ) {
254- if ( this . ampm == 'AM' && treg [ order . h ] == '12' )
264+ if ( ampm == 'AM' && treg [ order . h ] == '12' )
255265 this . hour = 0 ; // 12am = 0 hour
256- else if ( this . ampm == 'PM' && treg [ order . h ] != '12' )
266+ else if ( ampm == 'PM' && treg [ order . h ] != '12' )
257267 this . hour = ( parseFloat ( treg [ order . h ] ) + 12 ) . toFixed ( 0 ) ; // 12pm = 12 hour, any other pm = hour + 12
258268 else this . hour = Number ( treg [ order . h ] ) ;
259269 }
@@ -291,6 +301,20 @@ $.extend(Timepicker.prototype, {
291301 return false ;
292302 } ,
293303
304+ //########################################################################
305+ // pattern for standard and localized AM/PM markers
306+ //########################################################################
307+ _getPatternAmpm : function ( ) {
308+ var markers = [ ] ;
309+ o = this . _defaults ;
310+ if ( o . amNames )
311+ $ . merge ( markers , o . amNames ) ;
312+ if ( o . pmNames )
313+ $ . merge ( markers , o . pmNames ) ;
314+ markers = $ . map ( markers , function ( val ) { return val . replace ( / [ . * + ? | ( ) \[ \] { } \\ ] / g, '\\$&' ) } ) ;
315+ return '(' + markers . join ( '|' ) + ')?' ;
316+ } ,
317+
294318 //########################################################################
295319 // figure out position of time elements.. cause js cant do named captures
296320 //########################################################################
@@ -734,7 +758,8 @@ $.extend(Timepicker.prototype, {
734758 minute = ( this . minute_slider ) ? this . minute_slider . slider ( 'value' ) : false ,
735759 second = ( this . second_slider ) ? this . second_slider . slider ( 'value' ) : false ,
736760 millisec = ( this . millisec_slider ) ? this . millisec_slider . slider ( 'value' ) : false ,
737- timezone = ( this . timezone_select ) ? this . timezone_select . val ( ) : false ;
761+ timezone = ( this . timezone_select ) ? this . timezone_select . val ( ) : false ,
762+ o = this . _defaults ;
738763
739764 if ( typeof ( hour ) == 'object' ) hour = false ;
740765 if ( typeof ( minute ) == 'object' ) minute = false ;
@@ -747,11 +772,15 @@ $.extend(Timepicker.prototype, {
747772 if ( second !== false ) second = parseInt ( second , 10 ) ;
748773 if ( millisec !== false ) millisec = parseInt ( millisec , 10 ) ;
749774
750- var ampm = ( hour < 12 ) ? 'AM ' : 'PM' ;
775+ var ampm = o [ hour < 12 ? 'amNames ' : 'pmNames' ] [ 0 ] ;
751776
752777 // If the update was done in the input field, the input field should not be updated.
753778 // If the update was done using the sliders, update the input field.
754- var hasChanged = ( hour != this . hour || minute != this . minute || second != this . second || millisec != this . millisec || ( this . ampm . length > 0 && this . ampm != ampm ) || timezone != this . timezone ) ;
779+ var hasChanged = ( hour != this . hour || minute != this . minute
780+ || second != this . second || millisec != this . millisec
781+ || ( this . ampm . length > 0
782+ && ( hour < 12 ) != ( $ . inArray ( this . ampm . toUpperCase ( ) , this . amNames ) !== - 1 ) )
783+ || timezone != this . timezone ) ;
755784
756785 if ( hasChanged ) {
757786
@@ -765,10 +794,10 @@ $.extend(Timepicker.prototype, {
765794
766795 this . _limitMinMaxDateTime ( this . inst , true ) ;
767796 }
768- if ( this . _defaults . ampm ) this . ampm = ampm ;
797+ if ( o . ampm ) this . ampm = ampm ;
769798
770799 this . _formatTime ( ) ;
771- if ( this . $timeObj ) this . $timeObj . text ( this . formattedTime + this . _defaults . timeSuffix ) ;
800+ if ( this . $timeObj ) this . $timeObj . text ( this . formattedTime + o . timeSuffix ) ;
772801 this . timeDefined = true ;
773802 if ( hasChanged ) this . _updateDateTime ( ) ;
774803 } ,
@@ -791,38 +820,35 @@ $.extend(Timepicker.prototype, {
791820 _formatTime : function ( time , format , ampm ) {
792821 if ( ampm == undefined ) ampm = this . _defaults . ampm ;
793822 time = time || { hour : this . hour , minute : this . minute , second : this . second , millisec : this . millisec , ampm : this . ampm , timezone : this . timezone } ;
794- var tmptime = format || this . _defaults . timeFormat . toString ( ) ;
823+ var tmptime = ( format || this . _defaults . timeFormat ) . toString ( ) ;
795824
825+ var hour = parseInt ( time . hour , 10 ) ;
796826 if ( ampm ) {
797- var hour12 = ( ( time . ampm == 'AM' ) ? ( time . hour ) : ( time . hour % 12 ) ) ;
798- hour12 = ( Number ( hour12 ) === 0 ) ? 12 : hour12 ;
799- tmptime = tmptime . toString ( )
800- . replace ( / h h / g, ( ( hour12 < 10 ) ? '0' : '' ) + hour12 )
801- . replace ( / h / g, hour12 )
802- . replace ( / m m / g, ( ( time . minute < 10 ) ? '0' : '' ) + time . minute )
803- . replace ( / m / g, time . minute )
804- . replace ( / s s / g, ( ( time . second < 10 ) ? '0' : '' ) + time . second )
805- . replace ( / s / g, time . second )
806- . replace ( / l / g, ( ( time . millisec < 10 ) ? '00' : ( time . millisec < 100 ) ? '0' : '' ) + time . millisec )
807- . replace ( / T T / g, time . ampm . toUpperCase ( ) )
808- . replace ( / T t / g, time . ampm . toUpperCase ( ) )
809- . replace ( / t T / g, time . ampm . toLowerCase ( ) )
810- . replace ( / t t / g, time . ampm . toLowerCase ( ) )
811- . replace ( / T / g, time . ampm . charAt ( 0 ) . toUpperCase ( ) )
812- . replace ( / t / g, time . ampm . charAt ( 0 ) . toLowerCase ( ) )
813- . replace ( / z / g, time . timezone ) ;
814- } else {
815- tmptime = tmptime . toString ( )
816- . replace ( / h h / g, ( ( time . hour < 10 ) ? '0' : '' ) + time . hour )
817- . replace ( / h / g, time . hour )
818- . replace ( / m m / g, ( ( time . minute < 10 ) ? '0' : '' ) + time . minute )
819- . replace ( / m / g, time . minute )
820- . replace ( / s s / g, ( ( time . second < 10 ) ? '0' : '' ) + time . second )
821- . replace ( / s / g, time . second )
822- . replace ( / l / g, ( ( time . millisec < 10 ) ? '00' : ( time . millisec < 100 ) ? '0' : '' ) + time . millisec )
823- . replace ( / z / g, time . timezone ) ;
824- tmptime = $ . trim ( tmptime . replace ( / t / gi, '' ) ) ;
827+ if ( ! $ . inArray ( time . ampm . toUpperCase ( ) , this . amNames ) !== - 1 )
828+ hour = hour % 12 ;
829+ if ( hour === 0 )
830+ hour = 12 ;
825831 }
832+ tmptime = tmptime . replace ( / (?: h h ? | m m ? | s s ? | [ t T ] { 1 , 2 } | [ l z ] ) / g, function ( match ) {
833+ switch ( match . toLowerCase ( ) ) {
834+ case 'hh' : return ( '0' + hour ) . slice ( - 2 ) ;
835+ case 'h' : return hour ;
836+ case 'mm' : return ( '0' + time . minute ) . slice ( - 2 ) ;
837+ case 'm' : return time . minute ;
838+ case 'ss' : return ( '0' + time . second ) . slice ( - 2 ) ;
839+ case 's' : return time . second ;
840+ case 'l' : return ( '00' + time . millisec ) . slice ( - 3 ) ;
841+ case 'z' : return time . timezone ;
842+ case 't' : case 'tt' :
843+ if ( ampm ) {
844+ var _ampm = time . ampm ;
845+ if ( match . length == 1 )
846+ _ampm = _ampm . charAt ( 0 ) ;
847+ return match . charAt ( 0 ) == 'T' ? _ampm . toUpperCase ( ) : _ampm . toLowerCase ( ) ;
848+ }
849+ return '' ;
850+ }
851+ } ) ;
826852
827853 if ( arguments . length ) return tmptime ;
828854 else this . formattedTime = tmptime ;
@@ -978,6 +1004,8 @@ $.datepicker._doKeyPress = function(event) {
9781004 tp_inst . _defaults . separator +
9791005 tp_inst . _defaults . timeSuffix +
9801006 ( tp_inst . _defaults . showTimezone ? tp_inst . _defaults . timezoneList . join ( '' ) : '' ) +
1007+ ( tp_inst . _defaults . amNames . join ( '' ) ) +
1008+ ( tp_inst . _defaults . pmNames . join ( '' ) ) +
9811009 dateChars ,
9821010 chr = String . fromCharCode ( event . charCode === undefined ? event . keyCode : event . charCode ) ;
9831011 return event . ctrlKey || ( chr < ' ' || ! dateChars || datetimeChars . indexOf ( chr ) > - 1 ) ;
0 commit comments