refactor: replace chrono with time-rs

This commit is contained in:
ThetaDev 2022-10-18 22:53:09 +02:00
parent b5f6b7a174
commit 3c1cc92461
16 changed files with 195 additions and 112 deletions

View file

@ -1,6 +1,7 @@
use std::borrow::Cow;
use serde::Serialize;
use time::OffsetDateTime;
use url::Url;
use crate::{
@ -191,7 +192,8 @@ impl MapResponse<Channel<ChannelInfo>> for response::Channel {
lang,
&meta.joined_date_text,
&mut warnings,
),
)
.map(OffsetDateTime::date),
view_count: meta
.view_count_text
.and_then(|txt| util::parse_numeric_or_warn(&txt, &mut warnings)),

View file

@ -18,13 +18,13 @@ mod channel_rss;
use std::sync::Arc;
use std::{borrow::Cow, fmt::Debug};
use chrono::{DateTime, Duration, Utc};
use fancy_regex::Regex;
use log::{debug, error, warn};
use once_cell::sync::Lazy;
use rand::Rng;
use reqwest::{header, Client, ClientBuilder, Request, RequestBuilder, Response};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use time::{Duration, OffsetDateTime};
use tokio::sync::RwLock;
use crate::{
@ -240,7 +240,8 @@ enum CacheEntry<T> {
#[default]
None,
Some {
last_update: DateTime<Utc>,
#[serde(with = "time::serde::rfc3339")]
last_update: OffsetDateTime,
data: T,
},
}
@ -254,7 +255,7 @@ impl<T> CacheEntry<T> {
fn get(&self) -> Option<&T> {
match self {
CacheEntry::Some { last_update, data } => {
if last_update < &(Utc::now() - Duration::hours(24)) {
if last_update < &(OffsetDateTime::now_utc() - Duration::hours(24)) {
None
} else {
Some(data)
@ -268,7 +269,7 @@ impl<T> CacheEntry<T> {
impl<T> From<T> for CacheEntry<T> {
fn from(f: T) -> Self {
Self::Some {
last_update: Utc::now(),
last_update: OffsetDateTime::now_utc(),
data: f,
}
}

View file

@ -1,5 +1,7 @@
use std::{borrow::Cow, convert::TryFrom};
use time::OffsetDateTime;
use crate::{
deobfuscate::Deobfuscator,
error::{Error, ExtractionError},
@ -152,9 +154,9 @@ impl MapResponse<Playlist> for response::Playlist {
.and_then(|link| ChannelId::try_from(link).ok());
let mut warnings = video_items.warnings;
let last_update = last_update_txt
.as_ref()
.and_then(|txt| timeago::parse_textual_date_or_warn(lang, txt, &mut warnings));
let last_update = last_update_txt.as_ref().and_then(|txt| {
timeago::parse_textual_date_or_warn(lang, txt, &mut warnings).map(OffsetDateTime::date)
});
Ok(MapResult {
c: Playlist {

View file

@ -1,5 +1,5 @@
use chrono::{DateTime, Utc};
use serde::Deserialize;
use time::OffsetDateTime;
use super::Thumbnail;
@ -9,8 +9,8 @@ pub(crate) struct ChannelRss {
pub channel_id: String,
#[serde(rename = "$unflatten=title")]
pub title: String,
#[serde(rename = "$unflatten=published")]
pub create_date: DateTime<Utc>,
#[serde(rename = "$unflatten=published", with = "time::serde::rfc3339")]
pub create_date: OffsetDateTime,
pub entry: Vec<Entry>,
}
@ -20,10 +20,10 @@ pub(crate) struct Entry {
pub video_id: String,
#[serde(rename = "$unflatten=title")]
pub title: String,
#[serde(rename = "$unflatten=published")]
pub published: DateTime<Utc>,
#[serde(rename = "$unflatten=updated")]
pub updated: DateTime<Utc>,
#[serde(rename = "$unflatten=published", with = "time::serde::rfc3339")]
pub published: OffsetDateTime,
#[serde(rename = "$unflatten=updated", with = "time::serde::rfc3339")]
pub updated: OffsetDateTime,
#[serde(rename = "$unflatten=media:group")]
pub media_group: MediaGroup,
}

View file

@ -1,6 +1,6 @@
use chrono::TimeZone;
use serde::Deserialize;
use serde_with::{json::JsonString, serde_as, DefaultOnError, VecSkipError};
use time::OffsetDateTime;
use super::{ChannelBadge, ContinuationEndpoint, Thumbnails};
use crate::{
@ -342,12 +342,7 @@ impl<T> YouTubeListMapper<T> {
publish_date: video
.upcoming_event_data
.as_ref()
.map(|upc| {
chrono::Local.from_utc_datetime(&chrono::NaiveDateTime::from_timestamp(
upc.start_time,
0,
))
})
.and_then(|upc| OffsetDateTime::from_unix_timestamp(upc.start_time).ok())
.or_else(|| {
video
.published_time_text

View file

@ -143,7 +143,7 @@ Channel(
),
],
content: ChannelInfo(
create_date: Some("2009-04-04T00:00:00+02:00"),
create_date: Some("2009-04-04"),
view_count: Some(186854342),
links: [
("EEVblog Web Site", "http://www.eevblog.com/"),

View file

@ -160,7 +160,7 @@ Channel(
),
],
channel: None,
publish_date: Some("2022-09-27T18:00:00+02:00"),
publish_date: Some("2022-09-27T16:00:00Z"),
publish_date_txt: None,
view_count: Some(237),
is_live: false,