@@ -468,4 +468,124 @@ describe('dates', function() {
468468 } ) ;
469469 } ) ;
470470 } ) ;
471+
472+ describe ( 'formatDate' , function ( ) {
473+ function assertFormatRounds ( ms , calendar , results ) {
474+ [ 'y' , 'm' , 'd' , 'M' , 'S' , 1 , 2 , 3 , 4 ] . forEach ( function ( tr , i ) {
475+ expect ( Lib . formatDate ( ms , '' , tr , calendar ) )
476+ . toBe ( results [ i ] , calendar ) ;
477+ } ) ;
478+ }
479+
480+ it ( 'should pick a format based on tickround if no format is provided' , function ( ) {
481+ var ms = Lib . dateTime2ms ( '2012-08-13 06:19:34.5678' ) ;
482+ assertFormatRounds ( ms , 'gregorian' , [
483+ '2012' ,
484+ 'Aug 2012' ,
485+ 'Aug 13\n2012' ,
486+ '06:19\nAug 13, 2012' ,
487+ '06:19:35\nAug 13, 2012' ,
488+ '06:19:34.6\nAug 13, 2012' ,
489+ '06:19:34.57\nAug 13, 2012' ,
490+ '06:19:34.568\nAug 13, 2012' ,
491+ '06:19:34.5678\nAug 13, 2012'
492+ ] ) ;
493+
494+ // and for world calendars - in coptic this is 1728-12-07 (month=Meso)
495+ assertFormatRounds ( ms , 'coptic' , [
496+ '1728' ,
497+ 'Meso 1728' ,
498+ 'Meso 7\n1728' ,
499+ '06:19\nMeso 7, 1728' ,
500+ '06:19:35\nMeso 7, 1728' ,
501+ '06:19:34.6\nMeso 7, 1728' ,
502+ '06:19:34.57\nMeso 7, 1728' ,
503+ '06:19:34.568\nMeso 7, 1728' ,
504+ '06:19:34.5678\nMeso 7, 1728'
505+ ] ) ;
506+ } ) ;
507+
508+ it ( 'should accept custom formats using d3 specs even for world cals' , function ( ) {
509+ var ms = Lib . dateTime2ms ( '2012-08-13 06:19:34.5678' ) ;
510+ [
511+ // some common formats (plotly workspace options)
512+ [ '%Y-%m-%d' , '2012-08-13' , '1728-12-07' ] ,
513+ [ '%H:%M:%S' , '06:19:34' , '06:19:34' ] ,
514+ [ '%Y-%m-%e %H:%M:%S' , '2012-08-13 06:19:34' , '1728-12-7 06:19:34' ] ,
515+ [ '%A, %b %e' , 'Monday, Aug 13' , 'Pesnau, Meso 7' ] ,
516+
517+ // test padding behavior
518+ // world doesn't support space-padded (yet?)
519+ [ '%Y-%_m-%_d' , '2012- 8-13' , '1728-12-7' ] ,
520+ [ '%Y-%-m-%-d' , '2012-8-13' , '1728-12-7' ] ,
521+
522+ // and some strange ones to cover all fields
523+ [ '%a%j!%-j' , 'Mon226!226' , 'Pes337!337' ] ,
524+ [
525+ '%W or un or space padded-> %-W,%_W' ,
526+ '33 or un or space padded-> 33,33' ,
527+ '48 or un or space padded-> 48,48'
528+ ] ,
529+ [
530+ '%B \'%y WOY:%U DOW:%w' ,
531+ 'August \'12 WOY:32 DOW:1' ,
532+ 'Mesori \'28 WOY:## DOW:##' // world-cals doesn't support U or w
533+ ] ,
534+ [
535+ '%c && %x && .%2f .%f' , // %<n>f is our addition
536+ 'Mon Aug 13 06:19:34 2012 && 08/13/2012 && .57 .5678' ,
537+ 'Pes Meso 7 06:19:34 1728 && 12/07/1728 && .57 .5678'
538+ ]
539+
540+ ] . forEach ( function ( v ) {
541+ var fmt = v [ 0 ] ,
542+ expectedGregorian = v [ 1 ] ,
543+ expectedCoptic = v [ 2 ] ;
544+
545+ // tickround is irrelevant here...
546+ expect ( Lib . formatDate ( ms , fmt , 'y' ) )
547+ . toBe ( expectedGregorian , fmt ) ;
548+ expect ( Lib . formatDate ( ms , fmt , 4 , 'gregorian' ) )
549+ . toBe ( expectedGregorian , fmt ) ;
550+ expect ( Lib . formatDate ( ms , fmt , 'y' , 'coptic' ) )
551+ . toBe ( expectedCoptic , fmt ) ;
552+ } ) ;
553+ } ) ;
554+
555+ it ( 'should not round up to 60 seconds' , function ( ) {
556+ // see note in dates.js -> formatTime about this rounding
557+ assertFormatRounds ( - 0.1 , 'gregorian' , [
558+ '1969' ,
559+ 'Dec 1969' ,
560+ 'Dec 31\n1969' ,
561+ '23:59\nDec 31, 1969' ,
562+ '23:59:59\nDec 31, 1969' ,
563+ '23:59:59.9\nDec 31, 1969' ,
564+ '23:59:59.99\nDec 31, 1969' ,
565+ '23:59:59.999\nDec 31, 1969' ,
566+ '23:59:59.9999\nDec 31, 1969'
567+ ] ) ;
568+
569+ // in coptic this is Koi 22, 1686
570+ assertFormatRounds ( - 0.1 , 'coptic' , [
571+ '1686' ,
572+ 'Koi 1686' ,
573+ 'Koi 22\n1686' ,
574+ '23:59\nKoi 22, 1686' ,
575+ '23:59:59\nKoi 22, 1686' ,
576+ '23:59:59.9\nKoi 22, 1686' ,
577+ '23:59:59.99\nKoi 22, 1686' ,
578+ '23:59:59.999\nKoi 22, 1686' ,
579+ '23:59:59.9999\nKoi 22, 1686'
580+ ] ) ;
581+
582+ // and using the custom format machinery
583+ expect ( Lib . formatDate ( - 0.1 , '%Y-%m-%d %H:%M:%S.%f' ) )
584+ . toBe ( '1969-12-31 23:59:59.9999' ) ;
585+ expect ( Lib . formatDate ( - 0.1 , '%Y-%m-%d %H:%M:%S.%f' , null , 'coptic' ) )
586+ . toBe ( '1686-04-22 23:59:59.9999' ) ;
587+
588+ } ) ;
589+
590+ } ) ;
471591} ) ;
0 commit comments