@@ -23,7 +23,6 @@ use std::fmt::{Display, Formatter};
23
23
use std:: str:: FromStr ;
24
24
use std:: { any:: Any , collections:: BTreeMap } ;
25
25
26
- use crate :: error:: Result ;
27
26
use bitvec:: vec:: BitVec ;
28
27
use chrono:: { DateTime , NaiveDate , NaiveDateTime , NaiveTime , TimeZone , Utc } ;
29
28
use ordered_float:: OrderedFloat ;
@@ -32,16 +31,17 @@ use serde_bytes::ByteBuf;
32
31
use serde_json:: { Map as JsonMap , Number , Value as JsonValue } ;
33
32
use uuid:: Uuid ;
34
33
35
- use crate :: { ensure_data_valid, Error , ErrorKind } ;
36
-
37
- use super :: datatypes:: { PrimitiveType , Type } ;
34
+ pub use _serde:: RawLiteral ;
38
35
36
+ use crate :: error:: Result ;
39
37
use crate :: spec:: values:: date:: { date_from_naive_date, days_to_date, unix_epoch} ;
40
38
use crate :: spec:: values:: time:: microseconds_to_time;
41
39
use crate :: spec:: values:: timestamp:: microseconds_to_datetime;
42
40
use crate :: spec:: values:: timestamptz:: microseconds_to_datetimetz;
43
41
use crate :: spec:: MAX_DECIMAL_PRECISION ;
44
- pub use _serde:: RawLiteral ;
42
+ use crate :: { ensure_data_valid, Error , ErrorKind } ;
43
+
44
+ use super :: datatypes:: { PrimitiveType , Type } ;
45
45
46
46
/// Maximum value for [`PrimitiveType::Time`] type in microseconds, e.g. 23 hours 59 minutes 59 seconds 999999 microseconds.
47
47
const MAX_TIME_VALUE : i64 = 24 * 60 * 60 * 1_000_000i64 - 1 ;
@@ -443,7 +443,7 @@ impl Datum {
443
443
/// assert_eq!(&format!("{t}"), "1992-03-01 01:02:03.000088");
444
444
/// ```
445
445
pub fn timestamp_from_datetime ( dt : NaiveDateTime ) -> Self {
446
- Self :: timestamp_micros ( dt. timestamp_micros ( ) )
446
+ Self :: timestamp_micros ( dt. and_utc ( ) . timestamp_micros ( ) )
447
447
}
448
448
449
449
/// Parse a timestamp in [`%Y-%m-%dT%H:%M:%S%.f`] format.
@@ -1530,7 +1530,7 @@ impl Literal {
1530
1530
}
1531
1531
1532
1532
mod date {
1533
- use chrono:: { DateTime , NaiveDate , NaiveDateTime , TimeZone , Utc } ;
1533
+ use chrono:: { DateTime , NaiveDate , TimeDelta , TimeZone , Utc } ;
1534
1534
1535
1535
pub ( crate ) fn date_to_days ( date : & NaiveDate ) -> i32 {
1536
1536
date. signed_duration_since (
@@ -1542,8 +1542,8 @@ mod date {
1542
1542
1543
1543
pub ( crate ) fn days_to_date ( days : i32 ) -> NaiveDate {
1544
1544
// This shouldn't fail until the year 262000
1545
- NaiveDateTime :: from_timestamp_opt ( days as i64 * 86_400 , 0 )
1546
- . unwrap ( )
1545
+ ( chrono :: DateTime :: UNIX_EPOCH + TimeDelta :: try_days ( days as i64 ) . unwrap ( ) )
1546
+ . naive_utc ( )
1547
1547
. date ( )
1548
1548
}
1549
1549
@@ -1578,22 +1578,21 @@ mod time {
1578
1578
}
1579
1579
1580
1580
mod timestamp {
1581
- use chrono:: NaiveDateTime ;
1581
+ use chrono:: { DateTime , NaiveDateTime } ;
1582
1582
1583
1583
pub ( crate ) fn datetime_to_microseconds ( time : & NaiveDateTime ) -> i64 {
1584
- time. timestamp_micros ( )
1584
+ time. and_utc ( ) . timestamp_micros ( )
1585
1585
}
1586
1586
1587
1587
pub ( crate ) fn microseconds_to_datetime ( micros : i64 ) -> NaiveDateTime {
1588
- let ( secs, rem) = ( micros / 1_000_000 , micros % 1_000_000 ) ;
1589
-
1590
1588
// This shouldn't fail until the year 262000
1591
- NaiveDateTime :: from_timestamp_opt ( secs , rem as u32 * 1_000 ) . unwrap ( )
1589
+ DateTime :: from_timestamp_micros ( micros ) . unwrap ( ) . naive_utc ( )
1592
1590
}
1593
1591
}
1594
1592
1595
1593
mod timestamptz {
1596
- use chrono:: { DateTime , NaiveDateTime , TimeZone , Utc } ;
1594
+ use chrono:: DateTime ;
1595
+ use chrono:: Utc ;
1597
1596
1598
1597
pub ( crate ) fn datetimetz_to_microseconds ( time : & DateTime < Utc > ) -> i64 {
1599
1598
time. timestamp_micros ( )
@@ -1602,22 +1601,13 @@ mod timestamptz {
1602
1601
pub ( crate ) fn microseconds_to_datetimetz ( micros : i64 ) -> DateTime < Utc > {
1603
1602
let ( secs, rem) = ( micros / 1_000_000 , micros % 1_000_000 ) ;
1604
1603
1605
- Utc . from_utc_datetime (
1606
- // This shouldn't fail until the year 262000
1607
- & NaiveDateTime :: from_timestamp_opt ( secs, rem as u32 * 1_000 ) . unwrap ( ) ,
1608
- )
1604
+ DateTime :: from_timestamp ( secs, rem as u32 * 1_000 ) . unwrap ( )
1609
1605
}
1610
1606
}
1611
1607
1612
1608
mod _serde {
1613
1609
use std:: collections:: BTreeMap ;
1614
1610
1615
- use crate :: {
1616
- spec:: { PrimitiveType , Type , MAP_KEY_FIELD_NAME , MAP_VALUE_FIELD_NAME } ,
1617
- Error , ErrorKind ,
1618
- } ;
1619
-
1620
- use super :: { Literal , PrimitiveLiteral } ;
1621
1611
use serde:: {
1622
1612
de:: Visitor ,
1623
1613
ser:: { SerializeMap , SerializeSeq , SerializeStruct } ,
@@ -1627,6 +1617,13 @@ mod _serde {
1627
1617
use serde_derive:: Deserialize as DeserializeDerive ;
1628
1618
use serde_derive:: Serialize as SerializeDerive ;
1629
1619
1620
+ use crate :: {
1621
+ spec:: { PrimitiveType , Type , MAP_KEY_FIELD_NAME , MAP_VALUE_FIELD_NAME } ,
1622
+ Error , ErrorKind ,
1623
+ } ;
1624
+
1625
+ use super :: { Literal , PrimitiveLiteral } ;
1626
+
1630
1627
#[ derive( SerializeDerive , DeserializeDerive , Debug ) ]
1631
1628
#[ serde( transparent) ]
1632
1629
/// Raw literal representation used for serde. The serialize way is used for Avro serializer.
@@ -2186,7 +2183,6 @@ mod _serde {
2186
2183
2187
2184
#[ cfg( test) ]
2188
2185
mod tests {
2189
-
2190
2186
use apache_avro:: { to_value, types:: Value } ;
2191
2187
2192
2188
use crate :: {
0 commit comments