@@ -567,13 +567,7 @@ fn encode_timestamp(
567567///////////////////////////////////////////////////////////////////////////////
568568
569569fn encode_date_time ( dt : & DateTime < Utc > , tz : bool ) -> BytesText < ' static > {
570- let mut s = dt. to_rfc3339_opts ( chrono:: SecondsFormat :: Millis , tz) ;
571- if !tz {
572- let d = s. split ( '+' ) . collect :: < Vec < & str > > ( ) ;
573- if d. len ( ) == 2 {
574- s = d[ 0 ] . to_string ( ) ;
575- }
576- }
570+ let s = dt. to_rfc3339_opts ( chrono:: SecondsFormat :: Millis , tz) ;
577571 BytesText :: from_escaped ( s)
578572}
579573
@@ -599,10 +593,79 @@ mod tests {
599593 use super :: * ;
600594
601595 use datafusion:: arrow:: {
602- array:: { Array , Date64Array , Int64Array } ,
596+ array:: {
597+ Array , Date64Array , Int64Array , TimestampMicrosecondArray , TimestampMillisecondArray ,
598+ TimestampSecondArray ,
599+ } ,
603600 datatypes:: { ArrowPrimitiveType , Date64Type } ,
604601 } ;
605602
603+ #[ test]
604+ fn test_encode_timestamp ( ) {
605+ let expected = vec ! [ "2020-01-01T12:00:00.000Z" , "2020-01-01T12:01:00.000Z" ] ;
606+ let expected_no_tz = vec ! [
607+ "2020-01-01T12:00:00.000+00:00" ,
608+ "2020-01-01T12:01:00.000+00:00" ,
609+ ] ;
610+ let ts_milli = Arc :: new (
611+ TimestampMillisecondArray :: from ( vec ! [
612+ // 2020-01-01T12:00:00Z
613+ 1_577_880_000_000 ,
614+ // 2020-01-01T12:01:00Z
615+ 1_577_880_060_000 ,
616+ ] )
617+ . with_timezone ( Arc :: from ( "UTC" ) ) ,
618+ ) as Arc < dyn Array > ;
619+
620+ for i in 0 ..expected. len ( ) {
621+ let result = encode_primitive_dyn ( & ts_milli, i) . unwrap ( ) ;
622+ assert_eq ! ( result, BytesText :: new( expected[ i] ) ) ;
623+ }
624+
625+ let ts_micro = Arc :: new (
626+ TimestampMicrosecondArray :: from ( vec ! [
627+ // 2020-01-01T12:00:00Z
628+ 1_577_880_000_000_000 ,
629+ // 2020-01-01T12:01:00Z
630+ 1_577_880_060_000_000 ,
631+ ] )
632+ . with_timezone ( Arc :: from ( "UTC" ) ) ,
633+ ) as Arc < dyn Array > ;
634+
635+ for i in 0 ..expected. len ( ) {
636+ let result = encode_primitive_dyn ( & ts_micro, i) . unwrap ( ) ;
637+ assert_eq ! ( result, BytesText :: new( expected[ i] ) ) ;
638+ }
639+
640+ let ts_second = Arc :: new (
641+ TimestampSecondArray :: from ( vec ! [
642+ // 2020-01-01T12:00:00
643+ 1_577_880_000 ,
644+ // 2020-01-01T12:01:00
645+ 1_577_880_060 ,
646+ ] )
647+ . with_timezone ( Arc :: from ( "UTC" ) ) ,
648+ ) as Arc < dyn Array > ;
649+
650+ for i in 0 ..expected. len ( ) {
651+ let result = encode_primitive_dyn ( & ts_second, i) . unwrap ( ) ;
652+ assert_eq ! ( result, BytesText :: new( expected[ i] ) ) ;
653+ }
654+
655+ // with no timezone
656+ let ts_micro_no_tz = Arc :: new ( TimestampMicrosecondArray :: from ( vec ! [
657+ // 2020-01-01T12:00:00
658+ 1_577_880_000_000_000 ,
659+ // 2020-01-01T12:01:00
660+ 1_577_880_060_000_000 ,
661+ ] ) ) as Arc < dyn Array > ;
662+
663+ for i in 0 ..expected_no_tz. len ( ) {
664+ let result = encode_primitive_dyn ( & ts_micro_no_tz, i) . unwrap ( ) ;
665+ assert_eq ! ( result, BytesText :: new( expected_no_tz[ i] ) ) ;
666+ }
667+ }
668+
606669 #[ test]
607670 fn test_encode_primitive_dyn ( ) {
608671 let values: Int64Array = vec ! [ 1 , 2 , 3 ] . into ( ) ;
@@ -620,6 +683,9 @@ mod tests {
620683 let values = Arc :: new ( values) as Arc < dyn Array > ;
621684
622685 let result = encode_primitive_dyn ( & values, 0 ) . unwrap ( ) ;
623- assert_eq ! ( result. borrow( ) , BytesText :: new( "2024-09-11T00:00:00.000" ) ) ;
686+ assert_eq ! (
687+ result. borrow( ) ,
688+ BytesText :: new( "2024-09-11T00:00:00.000+00:00" )
689+ ) ;
624690 }
625691}
0 commit comments