This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-rust.git


The following commit(s) were added to refs/heads/main by this push:
     new 497a1b5  Replace i64 with DateTime (#94)
497a1b5 is described below

commit 497a1b59f60020325a689c2e7d2527a57c231705
Author: Farooq Qaiser <[email protected]>
AuthorDate: Wed Nov 22 10:30:07 2023 -0500

    Replace i64 with DateTime (#94)
    
    * Replace i64 with DateTime
    
    * Expose TimestampMillis only via public APIs
    
    * Avoid changing fields on structs and remove TimestampMillis
    
    * Pin UUID version
---
 Cargo.toml                                |  3 ++-
 crates/catalog/rest/src/catalog.rs        |  6 +++++-
 crates/iceberg/src/spec/snapshot.rs       | 11 ++++++++---
 crates/iceberg/src/spec/table_metadata.rs | 15 ++++++++++++---
 4 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 4ddb51f..f6efbc3 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -55,4 +55,5 @@ tokio = { version = "1", features = ["macros"] }
 typed-builder = "^0.18"
 url = "2"
 urlencoding = "2"
-uuid = "1.5.0"
+# We pin uuid's version to 1.5.0 because this bug: 
https://github.com/uuid-rs/uuid/issues/720
+uuid = "~1.5.0"
diff --git a/crates/catalog/rest/src/catalog.rs 
b/crates/catalog/rest/src/catalog.rs
index 653cfd4..efa11de 100644
--- a/crates/catalog/rest/src/catalog.rs
+++ b/crates/catalog/rest/src/catalog.rs
@@ -590,6 +590,7 @@ mod _serde {
 
 #[cfg(test)]
 mod tests {
+    use chrono::{TimeZone, Utc};
     use iceberg::spec::ManifestListLocation::ManifestListFile;
     use iceberg::spec::{
         FormatVersion, NestedField, Operation, PrimitiveType, Schema, 
Snapshot, SnapshotLog,
@@ -984,7 +985,10 @@ mod tests {
             uuid!("b55d9dda-6561-423a-8bfc-787980ce421f"),
             table.metadata().uuid()
         );
-        assert_eq!(1646787054459, table.metadata().last_updated_ms());
+        assert_eq!(
+            Utc.timestamp_millis_opt(1646787054459).unwrap(),
+            table.metadata().last_updated_ms()
+        );
         assert_eq!(
             vec![&Arc::new(
                 Schema::builder()
diff --git a/crates/iceberg/src/spec/snapshot.rs 
b/crates/iceberg/src/spec/snapshot.rs
index a04bb99..c52ee3b 100644
--- a/crates/iceberg/src/spec/snapshot.rs
+++ b/crates/iceberg/src/spec/snapshot.rs
@@ -18,6 +18,7 @@
 /*!
  * Snapshots
 */
+use chrono::{DateTime, TimeZone, Utc};
 use serde::{Deserialize, Serialize};
 use std::collections::HashMap;
 use std::sync::Arc;
@@ -116,8 +117,8 @@ impl Snapshot {
     }
     /// Get the timestamp of when the snapshot was created
     #[inline]
-    pub fn timestamp(&self) -> i64 {
-        self.timestamp_ms
+    pub fn timestamp(&self) -> DateTime<Utc> {
+        Utc.timestamp_millis_opt(self.timestamp_ms).unwrap()
     }
     /// Create snapshot builder
     pub fn builder() -> SnapshotBuilder {
@@ -309,6 +310,7 @@ pub enum SnapshotRetention {
 
 #[cfg(test)]
 mod tests {
+    use chrono::{TimeZone, Utc};
     use std::collections::HashMap;
 
     use crate::spec::snapshot::{
@@ -334,7 +336,10 @@ mod tests {
             .try_into()
             .unwrap();
         assert_eq!(3051729675574597004, result.snapshot_id());
-        assert_eq!(1515100955770, result.timestamp());
+        assert_eq!(
+            Utc.timestamp_millis_opt(1515100955770).unwrap(),
+            result.timestamp()
+        );
         assert_eq!(
             Summary {
                 operation: Operation::Append,
diff --git a/crates/iceberg/src/spec/table_metadata.rs 
b/crates/iceberg/src/spec/table_metadata.rs
index 6abe959..a5af58f 100644
--- a/crates/iceberg/src/spec/table_metadata.rs
+++ b/crates/iceberg/src/spec/table_metadata.rs
@@ -32,6 +32,8 @@ use super::{
 
 use _serde::TableMetadataEnum;
 
+use chrono::{DateTime, TimeZone, Utc};
+
 static MAIN_BRANCH: &str = "main";
 static DEFAULT_SPEC_ID: i32 = 0;
 static DEFAULT_SORT_ORDER_ID: i64 = 0;
@@ -133,8 +135,8 @@ impl TableMetadata {
 
     /// Returns last updated time.
     #[inline]
-    pub fn last_updated_ms(&self) -> i64 {
-        self.last_updated_ms
+    pub fn last_updated_ms(&self) -> DateTime<Utc> {
+        Utc.timestamp_millis_opt(self.last_updated_ms).unwrap()
     }
 
     /// Returns schemas
@@ -242,7 +244,7 @@ impl TableMetadata {
 
     /// Append snapshot to table
     pub fn append_snapshot(&mut self, snapshot: Snapshot) {
-        self.last_updated_ms = snapshot.timestamp();
+        self.last_updated_ms = snapshot.timestamp().timestamp_millis();
         self.last_sequence_number = snapshot.sequence_number();
 
         self.refs
@@ -771,6 +773,13 @@ pub struct SnapshotLog {
     pub timestamp_ms: i64,
 }
 
+impl SnapshotLog {
+    /// Returns the last updated timestamp as a DateTime<Utc> with millisecond 
precision
+    pub fn timestamp(self) -> DateTime<Utc> {
+        Utc.timestamp_millis_opt(self.timestamp_ms).unwrap()
+    }
+}
+
 #[cfg(test)]
 mod tests {
 

Reply via email to