@@ -31,9 +31,9 @@ use crate::spec::{Datum, PrimitiveLiteral, PrimitiveType};
31
31
use crate :: { Error , ErrorKind , Result } ;
32
32
33
33
/// Microseconds in one hour.
34
- const MICROSECONDS_PER_HOUR : i64 = 3_600_000_000 ;
34
+ const MICROSECONDS_PER_HOUR : f64 = 3_600_000_000_f64 ;
35
35
/// Nanoseconds in one hour.
36
- const NANOSECONDS_PER_HOUR : i64 = 3_600_000_000_000 ;
36
+ const NANOSECONDS_PER_HOUR : f64 = 3_600_000_000_000_f64 ;
37
37
/// Year of unix epoch.
38
38
const UNIX_EPOCH_YEAR : i32 = 1970 ;
39
39
/// One second in micros.
@@ -337,12 +337,12 @@ pub struct Hour;
337
337
impl Hour {
338
338
#[ inline]
339
339
fn hour_timestamp_micro ( v : i64 ) -> i32 {
340
- ( v / MICROSECONDS_PER_HOUR ) as i32
340
+ ( v as f64 / MICROSECONDS_PER_HOUR . as_f64 ( ) ) . floor ( ) as i32
341
341
}
342
342
343
343
#[ inline]
344
344
fn hour_timestamp_nano ( v : i64 ) -> i32 {
345
- ( v / NANOSECONDS_PER_HOUR ) as i32
345
+ ( v as f64 / NANOSECONDS_PER_HOUR . as_f64 ( ) ) . floor ( ) as i32
346
346
}
347
347
}
348
348
@@ -2390,18 +2390,8 @@ mod test {
2390
2390
transform : & BoxedTransformFunction ,
2391
2391
expect : Datum ,
2392
2392
) {
2393
- let timestamp = Datum :: timestamp_micros (
2394
- NaiveDateTime :: parse_from_str ( time, "%Y-%m-%d %H:%M:%S.%f" )
2395
- . unwrap ( )
2396
- . and_utc ( )
2397
- . timestamp_micros ( ) ,
2398
- ) ;
2399
- let timestamp_tz = Datum :: timestamptz_micros (
2400
- NaiveDateTime :: parse_from_str ( time, "%Y-%m-%d %H:%M:%S.%f" )
2401
- . unwrap ( )
2402
- . and_utc ( )
2403
- . timestamp_micros ( ) ,
2404
- ) ;
2393
+ let timestamp = Datum :: timestamp_from_str ( time) . unwrap ( ) ;
2394
+ let timestamp_tz = Datum :: timestamptz_from_str ( time. to_owned ( ) + " +00:00" ) . unwrap ( ) ;
2405
2395
let res = transform. transform_literal ( & timestamp) . unwrap ( ) . unwrap ( ) ;
2406
2396
assert_eq ! ( res, expect) ;
2407
2397
let res = transform. transform_literal ( & timestamp_tz) . unwrap ( ) . unwrap ( ) ;
@@ -2432,20 +2422,8 @@ mod test {
2432
2422
transform : & BoxedTransformFunction ,
2433
2423
expect : Datum ,
2434
2424
) {
2435
- let timestamp_ns = Datum :: timestamp_nanos (
2436
- NaiveDateTime :: parse_from_str ( time, "%Y-%m-%d %H:%M:%S.%f" )
2437
- . unwrap ( )
2438
- . and_utc ( )
2439
- . timestamp_nanos_opt ( )
2440
- . unwrap ( ) ,
2441
- ) ;
2442
- let timestamptz_ns = Datum :: timestamptz_nanos (
2443
- NaiveDateTime :: parse_from_str ( time, "%Y-%m-%d %H:%M:%S.%f" )
2444
- . unwrap ( )
2445
- . and_utc ( )
2446
- . timestamp_nanos_opt ( )
2447
- . unwrap ( ) ,
2448
- ) ;
2425
+ let timestamp_ns = Datum :: timestamp_from_str ( time) . unwrap ( ) ;
2426
+ let timestamptz_ns = Datum :: timestamptz_from_str ( time. to_owned ( ) + " +00:00" ) . unwrap ( ) ;
2449
2427
let res = transform. transform_literal ( & timestamp_ns) . unwrap ( ) . unwrap ( ) ;
2450
2428
assert_eq ! ( res, expect) ;
2451
2429
let res = transform
@@ -2760,14 +2738,15 @@ mod test {
2760
2738
fn test_transform_hours_literal ( ) {
2761
2739
let hour = Box :: new ( super :: Hour ) as BoxedTransformFunction ;
2762
2740
2763
- // Test TimestampMicrosecond
2764
- test_timestamp_and_tz_transform ( "2017-12-01 18:00:00.00" , & hour, Datum :: int ( 420042 ) ) ;
2765
- test_timestamp_and_tz_transform ( "1969-12-31 23:00:00.00" , & hour, Datum :: int ( -1 ) ) ;
2766
- test_timestamp_and_tz_transform ( "0022-05-01 22:01:01.00" , & hour, Datum :: int ( -17072905 ) ) ;
2741
+ test_timestamp_and_tz_transform ( "2017-12-01T18:00:00.000000" , & hour, Datum :: int ( 420042 ) ) ;
2742
+ test_timestamp_and_tz_transform ( "1970-01-01T22:01:01.000000" , & hour, Datum :: int ( 22 ) ) ;
2743
+ test_timestamp_and_tz_transform ( "1969-12-31T23:00:00.000000" , & hour, Datum :: int ( -1 ) ) ;
2744
+ test_timestamp_and_tz_transform ( "1969-12-31T22:01:01.000000" , & hour, Datum :: int ( -2 ) ) ;
2745
+ test_timestamp_and_tz_transform ( "0022-05-01T22:01:01.000000" , & hour, Datum :: int ( -17072906 ) ) ;
2767
2746
2768
2747
// Test TimestampNanosecond
2769
- test_timestamp_ns_and_tz_transform ( "2017-12-01 18 :00:00.00 " , & hour, Datum :: int ( 420042 ) ) ;
2770
- test_timestamp_ns_and_tz_transform ( "1969-12-31 23 :00:00.00 " , & hour, Datum :: int ( -1 ) ) ;
2771
- test_timestamp_ns_and_tz_transform ( "1900-05-01 22 :01:01.00 " , & hour, Datum :: int ( -610705 ) ) ;
2748
+ test_timestamp_ns_and_tz_transform ( "2017-12-01T18 :00:00.0000000000 " , & hour, Datum :: int ( 420042 ) ) ;
2749
+ test_timestamp_ns_and_tz_transform ( "1969-12-31T23 :00:00.0000000000 " , & hour, Datum :: int ( -1 ) ) ;
2750
+ test_timestamp_ns_and_tz_transform ( "1900-05-01T22 :01:01.0000000000 " , & hour, Datum :: int ( -610706 ) ) ;
2772
2751
}
2773
2752
}
0 commit comments