Skip to content

Commit

Permalink
fix: Timestamps with fractional seconds now work even if they are old…
Browse files Browse the repository at this point in the history
…er than unix epoch.
  • Loading branch information
pacman82 committed Nov 20, 2024
1 parent cb676d4 commit ad04dc8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,7 @@ pub fn ns_since_epoch(from: &Timestamp) -> Result<i64, MappingError> {
}

pub fn epoch_to_timestamp<const UNIT_FACTOR: i64>(from: i64) -> Timestamp {
let ndt = DateTime::from_timestamp(
from / UNIT_FACTOR,
((from % UNIT_FACTOR) * (1_000_000_000 / UNIT_FACTOR))
.try_into()
.unwrap(),
)
.unwrap();
let ndt = DateTime::from_timestamp_nanos(from * (1_000_000_000 / UNIT_FACTOR));
let date = ndt.date_naive();
let time = ndt.time();
Timestamp {
Expand Down
30 changes: 30 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,36 @@ fn fetch_date_time_ms() {
);
}

/// Fetch a timestamp before unix epoch. See issue:
/// <https://github.com/pacman82/arrow-odbc/issues/111>
#[test]
fn fetch_date_time_ms_before_epoch() {
let table_name = function_name!().rsplit_once(':').unwrap().1;

let array_any = fetch_arrow_data(
table_name,
"DATETIME",
"('1900-01-01 12:43:17.123')",
)
.unwrap();

let array_vals = array_any
.as_any()
.downcast_ref::<TimestampMillisecondArray>()
.unwrap();

// Assert that the correct values are found within the arrow batch
assert_eq!(
Some(
NaiveDate::from_ymd_opt(1900, 1, 1)
.unwrap()
.and_hms_milli_opt(12, 43, 17, 123)
.unwrap()
),
array_vals.value_as_datetime(0)
);
}

/// Fill a record batch of non nullable timestamps with nanoseconds precision
#[test]
fn fetch_non_null_date_time_ns() {
Expand Down

0 comments on commit ad04dc8

Please sign in to comment.