Skip to content

Commit

Permalink
nostr: change Timestamp::to_human_datetime fingerprint
Browse files Browse the repository at this point in the history
Return a `String` instead of `Cow<str>`.

Closes #622

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Nov 13, 2024
1 parent 7da3e14 commit 2b8ead5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

* nostr: change `EventBuilder::gift_wrap` (and linked methods) args to take `extra_tags` instead of `expiration` ([erskingardner])
* nostr: disable all default features except `std` ([Yuki Kishimoto])
* nostr: change `Timestamp::to_human_datetime` fingerprint ([Yuki Kishimoto])
* pool: switch from async to sync message sending for `Relay` ([Yuki Kishimoto])
* sdk: disable all default features ([Yuki Kishimoto])
* sdk: set `Client::from_builder` as private ([Yuki Kishimoto])
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-ffi/src/protocol/types/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ impl Timestamp {

/// Convert [`Timestamp`] to human datetime
pub fn to_human_datetime(&self) -> String {
self.inner.to_human_datetime().into_owned()
self.inner.to_human_datetime()
}
}
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-js/src/protocol/types/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ impl JsTimestamp {
/// Convert `Timestamp` to human datetime
#[wasm_bindgen(js_name = toHumanDatetime)]
pub fn to_human_datetime(&self) -> String {
self.inner.to_human_datetime().into_owned()
self.inner.to_human_datetime()
}
}
11 changes: 5 additions & 6 deletions crates/nostr/src/types/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

//! Time
use alloc::borrow::Cow;
use alloc::string::ToString;
use alloc::string::{String, ToString};
use core::fmt;
use core::ops::{Add, Range, Sub};
use core::str::{self, FromStr};
Expand Down Expand Up @@ -134,12 +133,12 @@ impl Timestamp {
}

/// Convert [`Timestamp`] to human datetime
pub fn to_human_datetime<'a>(&self) -> Cow<'a, str> {
pub fn to_human_datetime(&self) -> String {
let timestamp: u64 = self.as_u64();

if timestamp >= 253_402_300_800 {
// Year 9999
return Cow::Borrowed("Unavailable");
return String::from("Unavailable");
}

let days = (timestamp / 86400) as i64 - LEAPOCH;
Expand Down Expand Up @@ -207,7 +206,7 @@ impl Timestamp {
buf[17] = b'0' + (secs_of_day / 10 % 6) as u8;
buf[18] = b'0' + (secs_of_day % 10) as u8;

Cow::Owned(str::from_utf8(&buf).unwrap_or_default().to_string())
str::from_utf8(&buf).unwrap_or_default().to_string()
}
}

Expand Down Expand Up @@ -294,7 +293,7 @@ mod tests {
let timestamp = Timestamp::from(1682060685);
assert_eq!(
timestamp.to_human_datetime(),
Cow::from("2023-04-21T07:04:45Z")
String::from("2023-04-21T07:04:45Z")
);
}
}
Expand Down

0 comments on commit 2b8ead5

Please sign in to comment.