[fractal/fractal-next] room: Replace chrono::DateTime with glib::DateTime
- From: Julian Sparber <jsparber src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal/fractal-next] room: Replace chrono::DateTime with glib::DateTime
- Date: Tue, 11 May 2021 14:34:38 +0000 (UTC)
commit ca07e240fa29770aee82e51754c9df8bef6b7606
Author: Kévin Commaille <zecakeh tedomum fr>
Date: Tue May 11 14:16:40 2021 +0200
room: Replace chrono::DateTime with glib::DateTime
Cargo.lock | 2 --
Cargo.toml | 1 -
src/session/content/item_row.rs | 5 ++---
src/session/room/event.rs | 19 +++++++++++--------
src/session/room/item.rs | 9 ++++-----
src/session/room/timeline.rs | 5 +++--
6 files changed, 20 insertions(+), 21 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 9ca23aad..5ed72023 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -374,7 +374,6 @@ dependencies = [
"libc",
"num-integer",
"num-traits",
- "time 0.1.43",
"winapi",
]
@@ -672,7 +671,6 @@ dependencies = [
name = "fractal"
version = "0.1.0"
dependencies = [
- "chrono",
"comrak",
"futures",
"gettext-rs",
diff --git a/Cargo.toml b/Cargo.toml
index 80df81b2..8bbb11b0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -18,7 +18,6 @@ tokio = { version = "1.2", features = ["rt", "rt-multi-thread"] }
url = "2.2"
secret-service = "2.0"
html2pango = "0.4"
-chrono = "0.4"
futures = "0.3"
comrak = "0.10"
rand = "0.8"
diff --git a/src/session/content/item_row.rs b/src/session/content/item_row.rs
index cfb86606..9254a404 100644
--- a/src/session/content/item_row.rs
+++ b/src/session/content/item_row.rs
@@ -1,5 +1,4 @@
use adw::{prelude::*, subclass::prelude::*};
-use chrono::{offset::Local, Datelike};
use gettextrs::gettext;
use gtk::{glib, prelude::*, subclass::prelude::*};
@@ -147,14 +146,14 @@ impl ItemRow {
}
},
ItemType::DayDivider(date) => {
- let fmt = if date.year() == Local::today().year() {
+ let fmt = if date.year() == glib::DateTime::new_now_local().unwrap().year() {
// Translators: This is a date format in the day divider without the year
gettext("%A, %B %e")
} else {
// Translators: This is a date format in the day divider with the year
gettext("%A, %B %e, %Y")
};
- let date = date.format(&fmt).to_string();
+ let date = date.format(&fmt).unwrap().to_string();
if let Some(Ok(child)) = self.child().map(|w| w.downcast::<DividerRow>()) {
child.set_label(&date);
diff --git a/src/session/room/event.rs b/src/session/room/event.rs
index 41b3603c..61da9939 100644
--- a/src/session/room/event.rs
+++ b/src/session/room/event.rs
@@ -1,5 +1,4 @@
-use chrono::{offset::Local, DateTime};
-use gtk::{glib, prelude::*, subclass::prelude::*};
+use gtk::{glib, glib::DateTime, prelude::*, subclass::prelude::*};
use matrix_sdk::{
events::{
room::message::MessageType, room::message::Relation, AnyMessageEvent,
@@ -11,7 +10,7 @@ use matrix_sdk::{
use crate::fn_event;
use crate::session::User;
-use std::cell::RefCell;
+use std::{cell::RefCell, time::SystemTime};
#[derive(Clone, Debug, glib::GBoxed)]
#[gboxed(type_name = "BoxedAnyRoomEvent")]
@@ -175,25 +174,29 @@ impl Event {
fn_event!(event, event_id).clone()
}
- pub fn timestamp(&self) -> DateTime<Local> {
+ pub fn timestamp(&self) -> DateTime {
let priv_ = imp::Event::from_instance(&self);
let event = &*priv_.event.get().unwrap().borrow();
- fn_event!(event, origin_server_ts).clone().into()
+ let ts = fn_event!(event, origin_server_ts).clone();
+
+ DateTime::from_unix_utc(ts.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as i64)
+ .and_then(|t| t.to_local())
+ .unwrap()
}
pub fn time(&self) -> String {
let datetime = self.timestamp();
// FIXME Is there a cleaner way to do that?
- let local_time = datetime.format("%X").to_string().to_ascii_lowercase();
+ let local_time = datetime.format("%X").unwrap().as_str().to_ascii_lowercase();
if local_time.ends_with("am") || local_time.ends_with("pm") {
// Use 12h time format (AM/PM)
- datetime.format("%l∶%M %p").to_string()
+ datetime.format("%l∶%M %p").unwrap().to_string()
} else {
// Use 24 time format
- datetime.format("%R").to_string()
+ datetime.format("%R").unwrap().to_string()
}
}
diff --git a/src/session/room/item.rs b/src/session/room/item.rs
index abc56765..59ebfa4f 100644
--- a/src/session/room/item.rs
+++ b/src/session/room/item.rs
@@ -1,5 +1,4 @@
-use chrono::{offset::Local, DateTime};
-use gtk::{glib, prelude::*, subclass::prelude::*};
+use gtk::{glib, glib::DateTime, prelude::*, subclass::prelude::*};
use matrix_sdk::{
events::AnyRoomEvent,
identifiers::{EventId, UserId},
@@ -12,7 +11,7 @@ use crate::session::room::Event;
pub enum ItemType {
Event(Event),
// TODO: Add item type for grouped events
- DayDivider(DateTime<Local>),
+ DayDivider(DateTime),
NewMessageDivider,
}
@@ -123,7 +122,7 @@ impl Item {
glib::Object::new(&[("type", &type_)]).expect("Failed to create Item")
}
- pub fn for_day_divider(day: DateTime<Local>) -> Self {
+ pub fn for_day_divider(day: DateTime) -> Self {
let type_ = BoxedItemType(ItemType::DayDivider(day));
glib::Object::new(&[("type", &type_)]).expect("Failed to create Item")
}
@@ -179,7 +178,7 @@ impl Item {
}
}
- pub fn event_timestamp(&self) -> Option<DateTime<Local>> {
+ pub fn event_timestamp(&self) -> Option<DateTime> {
let priv_ = imp::Item::from_instance(&self);
if let ItemType::Event(event) = priv_.type_.get().unwrap() {
diff --git a/src/session/room/timeline.rs b/src/session/room/timeline.rs
index 9f70f6d3..6a7a9228 100644
--- a/src/session/room/timeline.rs
+++ b/src/session/room/timeline.rs
@@ -118,8 +118,9 @@ impl Timeline {
let mut index = position;
for current in list.range(position..position + added) {
if let Some(current_timestamp) = current.event_timestamp() {
- if Some(current_timestamp.date()) != previous_timestamp.map(|t| t.date()) {
- divider.push((index, Item::for_day_divider(current_timestamp)));
+ if Some(current_timestamp.ymd()) != previous_timestamp.as_ref().map(|t| t.ymd())
+ {
+ divider.push((index, Item::for_day_divider(current_timestamp.clone())));
previous_timestamp = Some(current_timestamp);
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]