From 5cf2b7c2db104df5bc2897d68fe4bc1c26957aa5 Mon Sep 17 00:00:00 2001
From: ouwou <26526779+ouwou@users.noreply.github.com>
Date: Sat, 2 Jan 2021 01:02:23 -0500
Subject: format timestamps and place them on the right (#17)
---
components/chatmessage.cpp | 6 +++++-
discord/snowflake.cpp | 12 ++++++++++++
discord/snowflake.hpp | 1 +
3 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/components/chatmessage.cpp b/components/chatmessage.cpp
index 44f172e..58b8197 100644
--- a/components/chatmessage.cpp
+++ b/components/chatmessage.cpp
@@ -1080,7 +1080,10 @@ ChatMessageHeader::ChatMessageHeader(const Message *data) {
else if (data->WebhookID.has_value())
m_extra->set_markup("Webhook");
- m_timestamp->set_text(data->Timestamp);
+ m_timestamp->set_text(data->ID.GetLocalTimestamp());
+ m_timestamp->set_hexpand(true);
+ m_timestamp->set_halign(Gtk::ALIGN_END);
+ m_timestamp->set_ellipsize(Pango::ELLIPSIZE_END);
m_timestamp->set_opacity(0.5);
m_timestamp->set_single_line_mode(true);
m_timestamp->set_margin_start(12);
@@ -1090,6 +1093,7 @@ ChatMessageHeader::ChatMessageHeader(const Message *data) {
m_main_box->set_vexpand(true);
m_main_box->set_can_focus(true);
+ m_meta_box->set_hexpand(true);
m_meta_box->set_can_focus(false);
m_content_box->set_can_focus(false);
diff --git a/discord/snowflake.cpp b/discord/snowflake.cpp
index 5528819..70dee2e 100644
--- a/discord/snowflake.cpp
+++ b/discord/snowflake.cpp
@@ -1,4 +1,6 @@
#include "snowflake.hpp"
+#include
+#include
Snowflake::Snowflake()
: m_num(Invalid) {}
@@ -23,6 +25,16 @@ bool Snowflake::IsValid() const {
return m_num != Invalid;
}
+std::string Snowflake::GetLocalTimestamp() const {
+ const time_t secs_since_epoch = (m_num / 4194304000) + 1420070400;
+ const std::tm tm = *localtime(&secs_since_epoch);
+ std::stringstream ss;
+ const static std::locale locale("");
+ ss.imbue(locale);
+ ss << std::put_time(&tm, "%X %x");
+ return ss.str();
+}
+
void from_json(const nlohmann::json &j, Snowflake &s) {
if (j.is_string()) {
std::string tmp;
diff --git a/discord/snowflake.hpp b/discord/snowflake.hpp
index 4cfdc35..2719f98 100644
--- a/discord/snowflake.hpp
+++ b/discord/snowflake.hpp
@@ -10,6 +10,7 @@ struct Snowflake {
Snowflake(const Glib::ustring &str);
bool IsValid() const;
+ std::string GetLocalTimestamp() const;
bool operator==(const Snowflake &s) const noexcept {
return m_num == s.m_num;
--
cgit v1.2.3