libc: get rid of deprecated chrono functions

This commit is contained in:
Mark Poliakov 2024-11-20 00:58:43 +02:00
parent e5aef02981
commit efb4909fd5

View File

@ -6,7 +6,10 @@ use core::{
use chrono::{DateTime, Datelike, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Timelike, Utc};
use super::{sys_time::__ygg_timespec_t, sys_types::{clock_t, clockid_t, time_t}};
use super::{
sys_time::__ygg_timespec_t,
sys_types::{clock_t, clockid_t, time_t},
};
mod convert;
mod string;
@ -53,11 +56,8 @@ pub static mut timezone: c_long = 0;
pub static mut tzname: *mut *mut c_char = null_mut();
pub trait CDateTime {
fn to_naive_datetime(&self) -> Option<NaiveDateTime>;
fn to_datetime<Tz: TimeZone>(&self, tz: Tz) -> Option<DateTime<Tz>> {
self.to_naive_datetime()?.and_local_timezone(tz).latest()
}
fn to_utc_datetime(&self) -> Option<DateTime<Utc>>;
fn to_datetime<Tz: TimeZone>(&self, tz: Tz) -> Option<DateTime<Tz>>;
}
impl tm {
@ -79,17 +79,24 @@ impl tm {
}
impl CDateTime for tm {
fn to_naive_datetime(&self) -> Option<NaiveDateTime> {
let date = self.naive_date()?;
let time = self.naive_time()?;
fn to_utc_datetime(&self) -> Option<DateTime<Utc>> {
let dt = NaiveDateTime::new(self.naive_date()?, self.naive_time()?);
Some(dt.and_utc())
}
Some(NaiveDateTime::new(date, time))
fn to_datetime<Tz: TimeZone>(&self, tz: Tz) -> Option<DateTime<Tz>> {
let dt = NaiveDateTime::new(self.naive_date()?, self.naive_time()?);
dt.and_local_timezone(tz).latest()
}
}
impl CDateTime for time_t {
fn to_naive_datetime(&self) -> Option<NaiveDateTime> {
NaiveDateTime::from_timestamp_opt(self.0, 0)
fn to_utc_datetime(&self) -> Option<DateTime<Utc>> {
DateTime::from_timestamp(self.0, 0)
}
fn to_datetime<Tz: TimeZone>(&self, tz: Tz) -> Option<DateTime<Tz>> {
tz.timestamp_opt(self.0, 0).latest()
}
}