Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix time display #585

Merged
merged 6 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/tests/00-base.result
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ a 1 true [1,2]
1
2
3
1740-10-08 10:16:40.001000 1740-10-08 10:16:40.000000 1740-10-08 10:16:39.999000
[] {}
with comment
1
Expand Down
1 change: 1 addition & 0 deletions cli/tests/00-base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ insert into test select to_string(number), number, false, number from numbers(10
select min(a), max(b), max(d), count() from test;

select '1';select 2; select 1+2;
select TO_TIMESTAMP(-7233803000000+1), TO_TIMESTAMP(-7233803000000), TO_TIMESTAMP(-7233803000000-1);

select [], {};

Expand Down
16 changes: 10 additions & 6 deletions sql/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const DAYS_FROM_CE: i32 = 719_163;
const NULL_VALUE: &str = "NULL";
const TRUE_VALUE: &str = "1";
const FALSE_VALUE: &str = "0";
const TIMESTAMP_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.6f";

#[cfg(feature = "flight-sql")]
use {
Expand Down Expand Up @@ -837,15 +838,18 @@ fn encode_value(f: &mut std::fmt::Formatter<'_>, val: &Value, raw: bool) -> std:
write!(f, "'{}'", s)
}
}
Value::Timestamp(i) => {
let secs = i / 1_000_000;
let nanos = ((i % 1_000_000) * 1000) as u32;
let t = DateTime::from_timestamp(secs, nanos).unwrap_or_default();
Value::Timestamp(micros) => {
let (mut secs, mut nanos) = (*micros / 1_000_000, (*micros % 1_000_000) * 1_000);
if nanos < 0 {
secs -= 1;
nanos += 1_000_000_000;
}
let t = DateTime::from_timestamp(secs, nanos as _).unwrap_or_default();
let t = t.naive_utc();
if raw {
write!(f, "{}", t)
write!(f, "{}", t.format(TIMESTAMP_FORMAT))
} else {
write!(f, "'{}'", t)
write!(f, "'{}'", t.format(TIMESTAMP_FORMAT))
}
}
Value::Date(i) => {
Expand Down
Loading