Skip to content

Commit

Permalink
refactor: update chrono usage
Browse files Browse the repository at this point in the history
Signed-off-by: vados <[email protected]>
  • Loading branch information
t3hmrman committed Sep 29, 2024
1 parent 4bfb662 commit ad017d7
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 32 deletions.
7 changes: 3 additions & 4 deletions src/cuid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::NaiveDateTime;
use chrono::DateTime;
use pgrx::*;

use crate::common::{naive_datetime_to_pg_timestamptz, OrPgrxError};
Expand Down Expand Up @@ -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}]"))
}
Expand Down
7 changes: 3 additions & 4 deletions src/ksuid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::NaiveDateTime;
use chrono::DateTime;
use pgrx::*;
use std::str::FromStr;
use svix_ksuid::{Ksuid, KsuidLike};
Expand Down Expand Up @@ -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}]"),
)
}
Expand Down
7 changes: 3 additions & 4 deletions src/ksuid_ms.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::NaiveDateTime;
use chrono::DateTime;
use pgrx::*;
use std::str::FromStr;
use svix_ksuid::{KsuidLike, KsuidMs};
Expand Down Expand Up @@ -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}]"),
)
}
Expand Down
7 changes: 3 additions & 4 deletions src/timeflake.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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}]"),
)
}
Expand Down
7 changes: 3 additions & 4 deletions src/ulid.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str::FromStr;

use chrono::NaiveDateTime;
use chrono::DateTime;
use pgrx::*;
use ulid::Ulid;
use uuid::Uuid;
Expand Down Expand Up @@ -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}]"),
)
}
Expand Down
7 changes: 3 additions & 4 deletions src/uuid_v6.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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}]"),
)
}
Expand Down
7 changes: 3 additions & 4 deletions src/uuid_v7.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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}]"),
)
}
Expand Down
7 changes: 3 additions & 4 deletions src/xid.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -27,16 +27,15 @@ 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")
.as_millis()
.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}]"),
)
}
Expand Down

0 comments on commit ad017d7

Please sign in to comment.