From ad017d768f11908f5d38e7fe22f6a832b39ac52c Mon Sep 17 00:00:00 2001 From: vados Date: Sun, 29 Sep 2024 21:57:58 +0900 Subject: [PATCH] refactor: update chrono usage Signed-off-by: vados --- src/cuid.rs | 7 +++---- src/ksuid.rs | 7 +++---- src/ksuid_ms.rs | 7 +++---- src/timeflake.rs | 7 +++---- src/ulid.rs | 7 +++---- src/uuid_v6.rs | 7 +++---- src/uuid_v7.rs | 7 +++---- src/xid.rs | 7 +++---- 8 files changed, 24 insertions(+), 32 deletions(-) diff --git a/src/cuid.rs b/src/cuid.rs index 843fef7..3aa89e4 100644 --- a/src/cuid.rs +++ b/src/cuid.rs @@ -1,4 +1,4 @@ -use chrono::NaiveDateTime; +use chrono::DateTime; use pgrx::*; use crate::common::{naive_datetime_to_pg_timestamptz, OrPgrxError}; @@ -41,9 +41,8 @@ fn idkit_cuid_extract_timestamptz(val: String) -> pgrx::TimestampWithTimeZone { .or_pgrx_error("failed to convert u128 timestamp to i64"); // Convert to a UTC timestamp - let now = NaiveDateTime::from_timestamp_millis(millis) - .or_pgrx_error("failed to parse timestamp from millis") - .and_utc(); + let now = DateTime::from_timestamp_millis(millis) + .or_pgrx_error("failed to parse timestamp from millis"); naive_datetime_to_pg_timestamptz(now, format!("failed to convert timestamp for CUID [{val}]")) } diff --git a/src/ksuid.rs b/src/ksuid.rs index 047b82f..d9587f3 100644 --- a/src/ksuid.rs +++ b/src/ksuid.rs @@ -1,4 +1,4 @@ -use chrono::NaiveDateTime; +use chrono::DateTime; use pgrx::*; use std::str::FromStr; use svix_ksuid::{Ksuid, KsuidLike}; @@ -26,9 +26,8 @@ fn idkit_ksuid_generate_text() -> String { fn idkit_ksuid_extract_timestamptz(val: String) -> pgrx::TimestampWithTimeZone { let ksuid = Ksuid::from_str(val.as_ref()).or_pgrx_error(format!("[{val}] is an invalid KSUID")); naive_datetime_to_pg_timestamptz( - NaiveDateTime::from_timestamp_opt(ksuid.timestamp_seconds(), 0) - .or_pgrx_error("failed to create timestamp from KSUID [{val}]") - .and_utc(), + DateTime::from_timestamp(ksuid.timestamp_seconds(), 0) + .or_pgrx_error("failed to create timestamp from KSUID [{val}]"), format!("failed to convert timestamp for KSUID [{val}]"), ) } diff --git a/src/ksuid_ms.rs b/src/ksuid_ms.rs index 1f620f5..da0a4b8 100644 --- a/src/ksuid_ms.rs +++ b/src/ksuid_ms.rs @@ -1,4 +1,4 @@ -use chrono::NaiveDateTime; +use chrono::DateTime; use pgrx::*; use std::str::FromStr; use svix_ksuid::{KsuidLike, KsuidMs}; @@ -31,9 +31,8 @@ fn idkit_ksuidms_extract_timestamptz(val: String) -> pgrx::TimestampWithTimeZone KsuidMs::from_str(val.as_ref()).or_pgrx_error(format!("[{val}] is an invalid KSUID")); naive_datetime_to_pg_timestamptz( - NaiveDateTime::from_timestamp_opt(ksuid.timestamp_seconds(), 0) - .or_pgrx_error("failed to create timestamp from KSUID [{val}]") - .and_utc(), + DateTime::from_timestamp(ksuid.timestamp_seconds(), 0) + .or_pgrx_error("failed to create timestamp from KSUID [{val}]"), format!("failed to convert timestamp for KSUID [{val}]"), ) } diff --git a/src/timeflake.rs b/src/timeflake.rs index 7f5d8ef..563f5c3 100644 --- a/src/timeflake.rs +++ b/src/timeflake.rs @@ -1,4 +1,4 @@ -use chrono::NaiveDateTime; +use chrono::DateTime; use pgrx::pg_extern; use std::io::{Error as IoError, ErrorKind}; use timeflake_rs::Timeflake; @@ -42,15 +42,14 @@ fn idkit_timeflake_extract_timestamptz(val: String) -> pgrx::TimestampWithTimeZo let timeflake = Timeflake::parse(val.as_ref()).or_pgrx_error(format!("[{val}] is an invalid Timeflake")); naive_datetime_to_pg_timestamptz( - NaiveDateTime::from_timestamp_millis( + DateTime::from_timestamp_millis( timeflake .timestamp .as_millis() .try_into() .or_pgrx_error("failed to convert timeflake timestamp milliseconds"), ) - .or_pgrx_error("failed to create timestamp from Timeflake [{val}]") - .and_utc(), + .or_pgrx_error("failed to create timestamp from Timeflake [{val}]"), format!("failed to convert timestamp for Timeflake [{val}]"), ) } diff --git a/src/ulid.rs b/src/ulid.rs index a780083..c3e0953 100644 --- a/src/ulid.rs +++ b/src/ulid.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -use chrono::NaiveDateTime; +use chrono::DateTime; use pgrx::*; use ulid::Ulid; use uuid::Uuid; @@ -42,13 +42,12 @@ fn idkit_ulid_from_uuid_text(uuid: String) -> String { fn idkit_ulid_extract_timestamptz(val: String) -> pgrx::TimestampWithTimeZone { let ulid = Ulid::from_string(val.as_ref()).or_pgrx_error(format!("[{val}] is an invalid ULID")); naive_datetime_to_pg_timestamptz( - NaiveDateTime::from_timestamp_millis( + DateTime::from_timestamp_millis( ulid.timestamp_ms() .try_into() .or_pgrx_error("failed to convert ulid timestamp milliseconds"), ) - .or_pgrx_error("failed to create timestamp from ULID [{val}]") - .and_utc(), + .or_pgrx_error("failed to create timestamp from ULID [{val}]"), format!("failed to convert timestamp for ULID [{val}]"), ) } diff --git a/src/uuid_v6.rs b/src/uuid_v6.rs index 35f864d..23422d1 100644 --- a/src/uuid_v6.rs +++ b/src/uuid_v6.rs @@ -1,7 +1,7 @@ use std::io::{Error as IoError, ErrorKind}; use std::str::FromStr; -use chrono::NaiveDateTime; +use chrono::DateTime; use getrandom::getrandom; use pgrx::pg_extern; use uuid::Uuid; @@ -54,9 +54,8 @@ fn idkit_uuidv6_extract_timestamptz(val: String) -> pgrx::TimestampWithTimeZone ); } naive_datetime_to_pg_timestamptz( - NaiveDateTime::from_timestamp_opt(secs as i64, nanos) - .or_pgrx_error("failed to create timestamp from UUIDV6 [{val}]") - .and_utc(), + DateTime::from_timestamp(secs as i64, nanos) + .or_pgrx_error("failed to create timestamp from UUIDV6 [{val}]"), format!("failed to convert timestamp for UUIDV6 [{val}]"), ) } diff --git a/src/uuid_v7.rs b/src/uuid_v7.rs index 5082c13..009658f 100644 --- a/src/uuid_v7.rs +++ b/src/uuid_v7.rs @@ -1,7 +1,7 @@ use std::io::{Error as IoError, ErrorKind}; use std::str::FromStr; -use chrono::NaiveDateTime; +use chrono::DateTime; use pgrx::pg_extern; use uuid::Uuid; @@ -51,9 +51,8 @@ fn idkit_uuidv7_extract_timestamptz(val: String) -> pgrx::TimestampWithTimeZone ); } naive_datetime_to_pg_timestamptz( - NaiveDateTime::from_timestamp_opt(secs as i64, nanos) - .or_pgrx_error("failed to create timestamp from UUIDV7 [{val}]") - .and_utc(), + DateTime::from_timestamp(secs as i64, nanos) + .or_pgrx_error("failed to create timestamp from UUIDV7 [{val}]"), format!("failed to convert timestamp for UUIDV7 [{val}]"), ) } diff --git a/src/xid.rs b/src/xid.rs index 90aa841..4c90f7c 100644 --- a/src/xid.rs +++ b/src/xid.rs @@ -1,6 +1,6 @@ use std::{str::FromStr, time::UNIX_EPOCH}; -use chrono::NaiveDateTime; +use chrono::DateTime; use pgrx::pg_extern; use xid::{new as generate_xid, Id as Xid}; @@ -27,7 +27,7 @@ fn idkit_xid_generate_text() -> String { fn idkit_xid_extract_timestamptz(val: String) -> pgrx::TimestampWithTimeZone { let xid = Xid::from_str(val.as_ref()).or_pgrx_error(format!("[{val}] is an invalid XID")); naive_datetime_to_pg_timestamptz( - NaiveDateTime::from_timestamp_millis( + DateTime::from_timestamp_millis( xid.time() .duration_since(UNIX_EPOCH) .or_pgrx_error("failed to convert XID type to timestamp milliseconds") @@ -35,8 +35,7 @@ fn idkit_xid_extract_timestamptz(val: String) -> pgrx::TimestampWithTimeZone { .try_into() .or_pgrx_error("failed to convert unix timestamp milliseconds"), ) - .or_pgrx_error("failed to create timestamp from XID [{val}]") - .and_utc(), + .or_pgrx_error("failed to create timestamp from XID [{val}]"), format!("failed to convert timestamp for XID [{val}]"), ) }