Skip to content

Commit

Permalink
jiff
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub committed Oct 10, 2024
1 parent 5fe8bf0 commit a6fe78c
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 57 deletions.
55 changes: 31 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ image = { version = "0.25.2", default-features = false, features = ["jpeg", "png
img-parts = "0.3.0"
indexmap = "2"
jieba-rs = "0.7.0"
jiff = { version = "0.1.13", default-features = false, features = ["std"] }
latex2mathml = "0.2.3"
mozjpeg = "0.10.10"
nanoid = "0.4.0"
Expand Down
6 changes: 3 additions & 3 deletions src/controller/db_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::meta_handler::ParamsPage;
use crate::error::AppError;
use bincode::{config::standard, Decode, Encode};
use chrono::Utc;
use jiff::Timestamp;
use nanoid::nanoid;
use sled::{Db, IVec, Iter, Tree};
use std::iter::Rev;
Expand All @@ -18,7 +18,7 @@ pub async fn clear_invalid(db: &Db, tree_name: &str) -> Result<(), AppError> {
.split_once('_')
.and_then(|s| i64::from_str_radix(s.0, 16).ok());
if let Some(time_stamp) = time_stamp {
if time_stamp < Utc::now().timestamp() {
if time_stamp < Timestamp::now().as_second() {
tree.remove(k)?;
}
}
Expand Down Expand Up @@ -382,7 +382,7 @@ pub(super) fn get_id_by_name(
/// ```
pub(super) fn generate_nanoid_ttl(seconds: i64) -> String {
let nanoid = nanoid!();
let exp = Utc::now().timestamp() + seconds;
let exp = Timestamp::now().as_second() + seconds;
format!("{exp:x}_{nanoid}")
}

Expand Down
12 changes: 6 additions & 6 deletions src/controller/feed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use axum_extra::{
TypedHeader,
};
use cached::proc_macro::cached;
use chrono::{DateTime, Utc};
use garde::Validate;
use jiff::Timestamp;
use reqwest::Client;
use serde::Deserialize;
use sled::Db;
Expand All @@ -48,13 +48,13 @@ impl TryFrom<rss::Item> for SourceItem {
type Error = AppError;
fn try_from(rss: rss::Item) -> Result<Self, Self::Error> {
let updated = if let Some(ref pub_date) = rss.pub_date {
if let Ok(ts) = DateTime::parse_from_rfc2822(pub_date) {
ts.timestamp()
if let Ok(ts) = pub_date.parse::<Timestamp>() {
ts.as_second()
} else {
Utc::now().timestamp()
Timestamp::now().as_second()
}
} else {
Utc::now().timestamp()
Timestamp::now().as_second()
};

let Some(link) = rss.link else {
Expand Down Expand Up @@ -791,7 +791,7 @@ pub(crate) async fn feed_star(
if star_tree.contains_key(&k)? {
star_tree.remove(&k)?;
} else {
let now = Utc::now().timestamp();
let now = Timestamp::now().as_second();
star_tree.insert(&k, i64_to_ivec(now))?;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/controller/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::sync::LazyLock;

use chrono::DateTime;
use jiff::Timestamp;
use pulldown_cmark::{html, CodeBlockKind, Event, Options, Tag};
use syntect::{highlighting::ThemeSet, html::highlighted_html_for_string, parsing::SyntaxSet};

/// convert a `i64` timestamp to a date [`String`]
pub(super) fn ts_to_date(timestamp: i64) -> String {
DateTime::from_timestamp(timestamp, 0)
Timestamp::from_second(timestamp)
.unwrap()
.format("%Y-%m-%d")
.strftime("%Y-%m-%d")
.to_string()
}

Expand Down
14 changes: 8 additions & 6 deletions src/controller/inn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use bincode::config::standard;
use cached::proc_macro::cached;
use chrono::{DateTime, Utc};
use garde::Validate;
use jiff::Timestamp;
use serde::Deserialize;
use sled::{transaction::ConflictableTransactionError, Transactional};
use sled::{Batch, Db};
Expand Down Expand Up @@ -308,7 +309,7 @@ pub(crate) async fn mod_inn_post(
topics,
inn_type: inn_type as u8,
early_birds: input.early_birds,
created_at: Utc::now().timestamp(),
created_at: Timestamp::now().as_second(),
limit_edit_seconds: input.limit_edit_seconds,
};

Expand Down Expand Up @@ -579,7 +580,7 @@ pub(crate) async fn edit_post(
let mut post: Post = get_one(&DB, "posts", pid)?;
let inn: Inn = get_one(&DB, "inns", post.iid)?;

if (post.created_at + (inn.limit_edit_seconds as i64) < Utc::now().timestamp())
if (post.created_at + (inn.limit_edit_seconds as i64) < Timestamp::now().as_second())
&& inn.limit_edit_seconds != 0
{
return Err(AppError::Unauthorized);
Expand Down Expand Up @@ -691,7 +692,7 @@ pub(crate) async fn edit_post_post(
return Err(AppError::Unauthorized);
}

let mut created_at = Utc::now().timestamp();
let mut created_at = Timestamp::now().as_second();
if created_at - claim.last_write < site_config.post_interval {
return Err(AppError::WriteInterval);
}
Expand Down Expand Up @@ -1580,7 +1581,8 @@ pub(crate) async fn post(

if is_author
&& (inn.limit_edit_seconds == 0
|| post.created_at + (inn.limit_edit_seconds as i64) >= Utc::now().timestamp())
|| post.created_at + (inn.limit_edit_seconds as i64)
>= Timestamp::now().as_second())
{
can_edit = true;
}
Expand Down Expand Up @@ -1619,7 +1621,7 @@ pub(crate) async fn post(
PostStatus::HiddenByMod => "<p><i>Hidden by mod.</i></p>".into(),
PostStatus::HiddenByUser => "<p><i>Hidden by user.</i></p>".into(),
_ => {
let diff = (Utc::now().timestamp() - post.created_at) / 24 / 3600;
let diff = (Timestamp::now().as_second() - post.created_at) / 24 / 3600;
if diff > 30 {
let mut content = format!(
r#"
Expand Down Expand Up @@ -1799,7 +1801,7 @@ pub(crate) async fn comment_post(
return Err(AppError::NotFound);
}

let created_at = Utc::now().timestamp();
let created_at = Timestamp::now().as_second();
if created_at - claim.last_write < site_config.comment_interval {
return Err(AppError::WriteInterval);
}
Expand Down
16 changes: 9 additions & 7 deletions src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ use crate::error::AppError;
use ::tantivy::TantivyDocument;
use bincode::config::standard;
use bincode::{Decode, Encode};
use chrono::{Days, Utc};
use garde::Validate;
use jiff::{RoundMode, Timestamp, TimestampRound, ToSpan, Unit};
use serde::{Deserialize, Serialize};
use sled::Db;
use std::fmt::Display;
Expand Down Expand Up @@ -204,14 +204,16 @@ impl User {
}

fn update_stats(db: &Db, uid: u32, stat_type: &str) -> Result<(), AppError> {
let expire = Utc::now()
.date_naive()
.checked_add_days(Days::new(3))
let expire = Timestamp::now()
.round(
TimestampRound::new()
.smallest(Unit::Day)
.mode(RoundMode::Trunc),
)
.unwrap()
.and_hms_opt(0, 0, 0)
.checked_add(3.day())
.unwrap()
.and_utc()
.timestamp();
.as_second();
let key = format!("{expire:x}_{uid}_{stat_type}");
incr_id(&db.open_tree("user_stats")?, key)?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/controller/solo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use axum_extra::{
TypedHeader,
};
use axum_garde::WithValidation;
use chrono::Utc;
use garde::Validate;
use jiff::Timestamp;
use serde::Deserialize;
use sled::Db;
use tracing::warn;
Expand Down Expand Up @@ -395,7 +395,7 @@ pub(crate) async fn solo_post(
}
}

let created_at = Utc::now().timestamp();
let created_at = Timestamp::now().as_second();
if created_at - claim.last_write < site_config.solo_interval {
return Err(AppError::WriteInterval);
}
Expand Down
Loading

0 comments on commit a6fe78c

Please sign in to comment.