Rusqlite + Chrono: How do I simplify code to obtain chrono datetime value
@conqp Richa
·
2026-04-23
·
via The Rust Programming Language Forum - Latest posts
eechris: In the database the dates are of type text. That's not a good precondition. Stringly typed data will always cause troubles, because you'll need conversion. I suggest you either Redesign your database to use actual date types, or If you cannot change the DB structure, introduce a wrapper type around a date string you're using SQLite, so you're SOL on data types. You can then implement appropriate conversion traits on the wrapper type like struct StringlyDate(String) to convert it into/from an actual Date data type. As for the four offsets, you can implement an extension trait on e.g. Date that provides const OFFSET_DAYS: [Days; 4] = [Days::new(90), Days::new(180), Days::new(365), Days::new(3650)]; fn offsets(self) -> Vec<Self> { OFFSET_DAYS.filter_map(|offset | self.checked_sub_days(offset)).collect() } You can then use the conversion traits on the resulting elements to convert it back to your stringly wrapper type when necessary.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。