Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc96342d

Browse files
committed
Fix ToSql
This sets the smallest unit to microseconds when calculating time deltas.Previously, the number of microseconds was expressed improperly becausethe rounding was not set.
1 parente19b3dc commitc96342d

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

‎postgres-types/src/jiff_01.rs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bytes::BytesMut;
22
use jiff_01::{
33
civil::{Date,DateTime,Time},
4-
Span,Timestamp,
4+
Span,SpanRound,Timestamp,Unit,
55
};
66
use postgres_protocol::types;
77
use std::error::Error;
@@ -12,13 +12,17 @@ const fn base() -> DateTime {
1212
DateTime::constant(2000,1,1,0,0,0,0)
1313
}
1414

15-
/// The number of seconds from the Unix epoch to 2000-01-01 00:00:00 UTC.
15+
/// The number of seconds from the Unix epoch to 2000-01-01 00:00:00 UTC.
1616
constPG_EPOCH:i64 =946684800;
1717

1818
fnbase_ts() ->Timestamp{
1919
Timestamp::new(PG_EPOCH,0).unwrap()
2020
}
2121

22+
fnround_us<'a>() ->SpanRound<'a>{
23+
SpanRound::new().largest(Unit::Microsecond)
24+
}
25+
2226
impl<'a>FromSql<'a>forDateTime{
2327
fnfrom_sql(_:&Type,raw:&[u8]) ->Result<DateTime,Box<dynError +Sync +Send>>{
2428
let t = types::timestamp_from_sql(raw)?;
@@ -30,7 +34,8 @@ impl<'a> FromSql<'a> for DateTime {
3034

3135
implToSqlforDateTime{
3236
fnto_sql(&self, _:&Type,w:&mutBytesMut) ->Result<IsNull,Box<dynError +Sync +Send>>{
33-
types::timestamp_to_sql(self.since(base())?.get_microseconds(), w);
37+
let span =self.since(base())?.round(round_us())?;
38+
types::timestamp_to_sql(span.get_microseconds(), w);
3439
Ok(IsNull::No)
3540
}
3641

@@ -49,7 +54,8 @@ impl<'a> FromSql<'a> for Timestamp {
4954

5055
implToSqlforTimestamp{
5156
fnto_sql(&self, _:&Type,w:&mutBytesMut) ->Result<IsNull,Box<dynError +Sync +Send>>{
52-
types::timestamp_to_sql(self.since(base_ts())?.get_microseconds(), w);
57+
let span =self.since(base_ts())?.round(round_us())?;
58+
types::timestamp_to_sql(span.get_microseconds(), w);
5359
Ok(IsNull::No)
5460
}
5561

@@ -88,8 +94,8 @@ impl<'a> FromSql<'a> for Time {
8894

8995
implToSqlforTime{
9096
fnto_sql(&self, _:&Type,w:&mutBytesMut) ->Result<IsNull,Box<dynError +Sync +Send>>{
91-
letdelta =self.since(Time::midnight())?;
92-
types::time_to_sql(delta.get_microseconds(), w);
97+
letspan =self.since(Time::midnight())?.round(round_us())?;
98+
types::time_to_sql(span.get_microseconds(), w);
9399
Ok(IsNull::No)
94100
}
95101

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp