diff --git a/src/youtube/types/get_live_chat.rs b/src/youtube/types/get_live_chat.rs index 03d9cfc..566593d 100644 --- a/src/youtube/types/get_live_chat.rs +++ b/src/youtube/types/get_live_chat.rs @@ -17,7 +17,7 @@ use serde::{Deserialize, Serialize}; use serde_aux::prelude::*; use url::Url; -use super::{Accessibility, CommandMetadata, Icon, ImageContainer, LocalizedText, UnlocalizedText}; +use super::{deserialize_datetime_utc_from_microseconds, Accessibility, CommandMetadata, Icon, ImageContainer, LocalizedText, UnlocalizedText}; use crate::youtube::{ get_http_client, util::{SimdJsonRequestBody, SimdJsonResponseBody}, @@ -183,7 +183,7 @@ pub struct MessageRendererBase { pub author_badges: Option>, pub context_menu_endpoint: ContextMenuEndpoint, pub id: String, - #[serde(deserialize_with = "deserialize_datetime_utc_from_milliseconds")] + #[serde(deserialize_with = "deserialize_datetime_utc_from_microseconds")] pub timestamp_usec: DateTime, pub author_external_channel_id: String, pub context_menu_accessibility: Accessibility diff --git a/src/youtube/types/mod.rs b/src/youtube/types/mod.rs index 01898b8..96845e2 100644 --- a/src/youtube/types/mod.rs +++ b/src/youtube/types/mod.rs @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use serde::Deserialize; +use serde::{de::Error, Deserialize, Deserializer}; +use serde_aux::field_attributes::deserialize_number_from_string; use simd_json::OwnedValue; pub mod get_live_chat; @@ -104,3 +105,17 @@ pub struct Emoji { pub struct Icon { pub icon_type: String } + +pub fn deserialize_datetime_utc_from_microseconds<'de, D>(deserializer: D) -> Result, D::Error> +where + D: Deserializer<'de> +{ + use chrono::prelude::*; + + let number = deserialize_number_from_string::(deserializer)?; + let seconds = number / 1_000_000; + let micros = (number % 1_000_000) as u32; + let nanos = micros * 1_000; + + Ok(Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(seconds, nanos).ok_or_else(|| D::Error::custom("Couldn't parse the timestamp"))?)) +}