Switch to jiff from time

This commit is contained in:
Joscha 2024-12-04 17:08:52 +01:00
parent 0256329f65
commit 4314a24e78
9 changed files with 51 additions and 141 deletions

View file

@ -10,9 +10,9 @@ use std::num::ParseIntError;
use std::str::FromStr;
use std::{error, fmt};
use jiff::Timestamp;
use serde::{de, ser, Deserialize, Serialize};
use serde_json::Value;
use time::{OffsetDateTime, UtcOffset};
/// Describes an account and its preferred name.
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -403,19 +403,15 @@ impl<'de> Deserialize<'de> for Snowflake {
/// Time is specified as a signed 64-bit integer, giving the number of seconds
/// since the Unix Epoch.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Time(#[serde(with = "time::serde::timestamp")] pub OffsetDateTime);
pub struct Time(#[serde(with = "jiff::fmt::serde::timestamp::second::required")] pub Timestamp);
impl Time {
pub fn new(time: OffsetDateTime) -> Self {
let time = time
.to_offset(UtcOffset::UTC)
.replace_millisecond(0)
.unwrap();
pub fn new(time: Timestamp) -> Self {
Self(time)
}
pub fn now() -> Self {
Self::new(OffsetDateTime::now_utc())
Self::new(Timestamp::now())
}
}

View file

@ -1,24 +1,21 @@
use async_trait::async_trait;
use clap::Parser;
use time::macros::format_description;
use time::{Duration, OffsetDateTime, UtcOffset};
use jiff::{Span, Timestamp};
use crate::api::Message;
use crate::bot::command::{ClapCommand, Command, Context};
use crate::conn;
pub fn format_time(t: OffsetDateTime) -> String {
let t = t.to_offset(UtcOffset::UTC);
let format = format_description!("[year]-[month]-[day] [hour]:[minute]:[second] UTC");
t.format(format).unwrap()
pub fn format_time(t: Timestamp) -> String {
t.strftime("%Y-&m-%d %H:%M:%S UTC").to_string()
}
pub fn format_duration(d: Duration) -> String {
pub fn format_duration(d: Span) -> String {
let d_abs = d.abs();
let days = d_abs.whole_days();
let hours = d_abs.whole_hours() % 24;
let mins = d_abs.whole_minutes() % 60;
let secs = d_abs.whole_seconds() % 60;
let days = d_abs.get_days();
let hours = d_abs.get_hours() % 24;
let mins = d_abs.get_minutes() % 60;
let secs = d_abs.get_seconds() % 60;
let mut segments = vec![];
if days > 0 {
@ -48,13 +45,13 @@ pub fn format_duration(d: Duration) -> String {
pub struct Uptime;
pub trait HasStartTime {
fn start_time(&self) -> OffsetDateTime;
fn start_time(&self) -> Timestamp;
}
impl Uptime {
fn formulate_reply<B: HasStartTime>(&self, ctx: &Context, bot: &B, connected: bool) -> String {
let start = bot.start_time();
let now = OffsetDateTime::now_utc();
let now = Timestamp::now();
let mut reply = format!(
"/me has been up since {} ({})",

View file

@ -6,8 +6,8 @@ use std::future::Future;
use std::time::{Duration, Instant};
use std::{error, fmt, result};
use ::time::OffsetDateTime;
use futures_util::SinkExt;
use jiff::Timestamp;
use log::debug;
use tokio::net::TcpStream;
use tokio::select;
@ -75,7 +75,7 @@ pub type Result<T> = result::Result<T, Error>;
#[derive(Debug, Clone)]
pub struct Joining {
pub since: OffsetDateTime,
pub since: Timestamp,
pub hello: Option<HelloEvent>,
pub snapshot: Option<SnapshotEvent>,
pub bounce: Option<BounceEvent>,
@ -84,7 +84,7 @@ pub struct Joining {
impl Joining {
fn new() -> Self {
Self {
since: OffsetDateTime::now_utc(),
since: Timestamp::now(),
hello: None,
snapshot: None,
bounce: None,
@ -122,7 +122,7 @@ impl Joining {
.map(|s| (s.session_id.clone(), SessionInfo::Full(s)))
.collect::<HashMap<_, _>>();
Some(Joined {
since: OffsetDateTime::now_utc(),
since: Timestamp::now(),
session,
account: hello.account.clone(),
listing,
@ -164,7 +164,7 @@ impl SessionInfo {
#[derive(Debug, Clone)]
pub struct Joined {
pub since: OffsetDateTime,
pub since: Timestamp,
pub session: SessionView,
pub account: Option<PersonalAccountView>,
pub listing: HashMap<SessionId, SessionInfo>,
@ -522,10 +522,10 @@ impl Conn {
self.disconnect().await?;
}
let now = OffsetDateTime::now_utc();
let now = Timestamp::now();
// Send new ws ping
let ws_payload = now.unix_timestamp_nanos().to_be_bytes().to_vec();
let ws_payload = now.as_millisecond().to_be_bytes().to_vec();
self.last_ws_ping_payload = Some(ws_payload.clone());
self.last_ws_ping_replied_to = false;
self.ws.send(tungstenite::Message::Ping(ws_payload)).await?;