@@ -13,6 +13,13 @@ const fn base() -> DateTime {
13
13
DateTime :: constant ( 2000 , 1 , 1 , 0 , 0 , 0 , 0 )
14
14
}
15
15
16
+ /// The number of seconds from 2000-01-01 00:00:00 UTC to the Unix epoch.
17
+ const Y2K_EPOCH : i64 = 946684800 ;
18
+
19
+ fn base_ts ( ) -> Timestamp {
20
+ Timestamp :: new ( Y2K_EPOCH , 0 ) . unwrap ( )
21
+ }
22
+
16
23
impl < ' a > FromSql < ' a > for DateTime {
17
24
fn from_sql ( _: & Type , raw : & [ u8 ] ) -> Result < DateTime , Box < dyn Error + Sync + Send > > {
18
25
let t = types:: timestamp_from_sql ( raw) ?;
@@ -33,22 +40,17 @@ impl ToSql for DateTime {
33
40
}
34
41
35
42
impl < ' a > FromSql < ' a > for Timestamp {
36
- fn from_sql ( type_ : & Type , raw : & [ u8 ] ) -> Result < Timestamp , Box < dyn Error + Sync + Send > > {
37
- Ok ( DateTime :: from_sql ( type_, raw) ?
38
- . to_zoned ( TimeZone :: UTC ) ?
39
- . timestamp ( ) )
43
+ fn from_sql ( _: & Type , raw : & [ u8 ] ) -> Result < Timestamp , Box < dyn Error + Sync + Send > > {
44
+ let t = types:: timestamp_from_sql ( raw) ?;
45
+ Ok ( base_ts ( ) . checked_add ( Span :: new ( ) . microseconds ( t) ) ?)
40
46
}
41
47
42
48
accepts ! ( TIMESTAMPTZ ) ;
43
49
}
44
50
45
51
impl ToSql for Timestamp {
46
52
fn to_sql ( & self , _: & Type , w : & mut BytesMut ) -> Result < IsNull , Box < dyn Error + Sync + Send > > {
47
- types:: timestamp_to_sql (
48
- self . since ( base ( ) . to_zoned ( TimeZone :: UTC ) ?) ?
49
- . get_microseconds ( ) ,
50
- w,
51
- ) ;
53
+ types:: timestamp_to_sql ( self . since ( base_ts ( ) ) ?. get_microseconds ( ) , w) ;
52
54
Ok ( IsNull :: No )
53
55
}
54
56
0 commit comments