refactor: Clean up timestamp handling logic and avoid a conversion (#4988)

* refactor: Clean up timestamp handling logic

* fix: Remove unused timestamp function

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
pull/24376/head
Andrew Lamb 2022-06-30 21:07:21 -04:00 committed by GitHub
parent 1ef9c7ceda
commit 0c705fecf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 21 deletions

View File

@ -82,14 +82,6 @@ impl Time {
Self(Utc.timestamp_millis(millis))
}
/// Makes a new `DateTime` from the number of non-leap milliseconds
/// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp").
///
/// Returns None if out of range
pub fn from_timestamp_millis_opt(millis: i64) -> Option<Self> {
Some(Self(Utc.timestamp_millis_opt(millis).single()?))
}
/// Makes a new `Time` from the number of non-leap seconds
/// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp")
/// and the number of nanoseconds since the last whole non-leap second.
@ -324,8 +316,6 @@ mod test {
assert!(chrono::Duration::from_std(duration).is_err());
assert!(time.checked_add(duration).is_none());
assert!(time.checked_sub(duration).is_none());
assert!(Time::from_timestamp_millis_opt(i64::MAX).is_none())
}
#[test]

View File

@ -191,18 +191,10 @@ impl WriteBufferStreamHandler for RSKafkaStreamHandler {
sequence_number: SequenceNumber::new(record.offset),
};
let timestamp_millis =
i64::try_from(record.record.timestamp.unix_timestamp_nanos() / 1_000_000)
.map_err(WriteBufferError::invalid_data)?;
let timestamp_nanos = i64::try_from(record.record.timestamp.unix_timestamp_nanos())
.map_err(WriteBufferError::invalid_data)?;
let timestamp = Time::from_timestamp_millis_opt(timestamp_millis)
.ok_or_else::<WriteBufferError, _>(|| {
format!(
"Cannot parse timestamp for milliseconds: {}",
timestamp_millis
)
.into()
})?;
let timestamp = Time::from_timestamp_nanos(timestamp_nanos);
let value = record
.record