Skip to content

Instantly share code, notes, and snippets.

View silmeth's full-sized avatar

Benedykt Jaworski silmeth

  • Poznań, Poland
View GitHub Profile
use axum::{
async_trait,
extract::{FromRequestParts, Request},
http::{request::Parts, StatusCode},
middleware::Next,
routing::get,
Router,
};
#[derive(Debug)]
@silmeth
silmeth / chrono_format_parsed_and_interned.rs
Created February 15, 2026 16:00
macro to optimize chrono DateTime formatting by lazilly parsing format string and memoization/interning of the result
use std::sync::OnceLock;
use chrono::{DateTime, Utc, format::StrftimeItems};
pub fn format_date(ts: DateTime<Utc>) -> String {
static FORMAT: OnceLock<Vec<chrono::format::Item<'static>>> = OnceLock::new();
let items = FORMAT.get_or_init(|| StrftimeItems::new("%Y-%m-%d").collect());
ts.format_with_items(items.iter()).to_string()
}
/// Creates functions like the one above