Skip to content

Commit

Permalink
fixes #165 - add support for iso-style timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Dec 3, 2023
1 parent a767f21 commit 420dd5c
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 14 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ This configuration file holds general application settings. Default content:
proxy_pass=
proxy_port=
proxy_user=
timestamp_iso=0

### attachment_send_type

Expand Down Expand Up @@ -292,6 +293,18 @@ time, it is recommended to first run nchat without arguments (`nchat`) for its
config dir to be created, and then edit proxy settings in `~/.nchat/app.conf`
as needed, before running `nchat -s` to setup an account.

### timestamp_iso

Specifies whether to use ISO-style timestamps (`YYYY-MM-DD HH:MM`) in the UI
and at export of chat history. By default nchat uses a dynamic "human-friendly"
format:

- `HH:MM` for timestamps on same date as today, e.g. `19:00`
- `DAY HH:MM` for timestamps in the last week, e.g. `Mon 19:00`
- `DD MMM HH:MM` for timestamps in the current year, e.g. `14 Nov 19:00`
- `DD MMM YYYY HH:MM` for timestamps in non-current year, e.g. `14 Nov 2022 19:00`
- `DD MMM YYYY HH:MM` for timestamps during export, e.g. `14 Nov 2022 19:00`

~/.nchat/ui.conf
----------------
This configuration file holds general user interface settings. Default content:
Expand Down
2 changes: 1 addition & 1 deletion lib/common/src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

#pragma once

#define NCHAT_VERSION "4.07"
#define NCHAT_VERSION "4.08"
1 change: 1 addition & 0 deletions lib/ncutil/src/appconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void AppConfig::Init()
{ "proxy_pass", "" },
{ "proxy_port", "" },
{ "proxy_user", "" },
{ "timestamp_iso", "0" },
};

const std::string configPath(FileUtil::GetApplicationDir() + std::string("/app.conf"));
Expand Down
2 changes: 1 addition & 1 deletion lib/ncutil/src/messagecache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ void MessageCache::Export(const std::string& p_ExportDir)
std::map<std::string, std::string> messageMap;
for (auto chatMessage = chatMessages.rbegin(); chatMessage != chatMessages.rend(); ++chatMessage)
{
std::string timestr = TimeUtil::GetTimeString(chatMessage->timeSent, false /* p_Short */);
std::string timestr = TimeUtil::GetTimeString(chatMessage->timeSent, true /* p_IsExport */);
std::string year = TimeUtil::GetYearString(chatMessage->timeSent);;
if (year != lastYear)
{
Expand Down
21 changes: 14 additions & 7 deletions lib/ncutil/src/timeutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@

#include <sys/time.h>

#include "appconfig.h"

int64_t TimeUtil::GetCurrentTimeMSec()
{
struct timeval now;
gettimeofday(&now, NULL);
return static_cast<int64_t>((now.tv_sec * 1000) + (now.tv_usec / 1000));
}

std::string TimeUtil::GetTimeString(int64_t p_TimeSent, bool p_Short)
std::string TimeUtil::GetTimeString(int64_t p_TimeSent, bool p_IsExport)
{
time_t timeSent = (time_t)(p_TimeSent / 1000);
struct tm tmSent;
Expand All @@ -30,8 +32,18 @@ std::string TimeUtil::GetTimeString(int64_t p_TimeSent, bool p_Short)
localtime_r(&timeNow, &tmNow);
char tmpstr[32] = { 0 };
static int64_t useWeekdayMaxAge = (6 * 24 * 3600);
static bool isTimestampIso = AppConfig::GetBool("timestamp_iso");

if (p_Short)
if (isTimestampIso)
{
strftime(tmpstr, sizeof(tmpstr), "%Y-%m-%d %H:%M", &tmSent);
}
else if (p_IsExport)
{
int dlen = snprintf(tmpstr, sizeof(tmpstr), "%d ", tmSent.tm_mday);
strftime(tmpstr + dlen, sizeof(tmpstr) - dlen, "%b %Y %H:%M", &tmSent);
}
else
{
if ((tmSent.tm_year == tmNow.tm_year) && (tmSent.tm_mon == tmNow.tm_mon) && (tmSent.tm_mday == tmNow.tm_mday))
{
Expand All @@ -52,11 +64,6 @@ std::string TimeUtil::GetTimeString(int64_t p_TimeSent, bool p_Short)
strftime(tmpstr + dlen, sizeof(tmpstr) - dlen, "%b %Y %H:%M", &tmSent);
}
}
else
{
int dlen = snprintf(tmpstr, sizeof(tmpstr), "%d ", tmSent.tm_mday);
strftime(tmpstr + dlen, sizeof(tmpstr) - dlen, "%b %Y %H:%M", &tmSent);
}

return std::string(tmpstr);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ncutil/src/timeutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TimeUtil
{
public:
static int64_t GetCurrentTimeMSec();
static std::string GetTimeString(int64_t p_TimeSent, bool p_Short);
static std::string GetTimeString(int64_t p_TimeSent, bool p_IsExport);
static std::string GetYearString(int64_t p_TimeSent);
static void Sleep(double p_Sec);
};
2 changes: 1 addition & 1 deletion src/nchat.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" DO NOT MODIFY THIS FILE! It was generated by help2man.
.TH NCHAT "1" "November 2023" "nchat v4.07" "User Commands"
.TH NCHAT "1" "December 2023" "nchat v4.08" "User Commands"
.SH NAME
nchat \- ncurses chat
.SH SYNOPSIS
Expand Down
4 changes: 2 additions & 2 deletions src/uihistoryview.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// uihistoryview.cpp
//
// Copyright (c) 2019-2021 Kristofer Berggren
// Copyright (c) 2019-2023 Kristofer Berggren
// All rights reserved.
//
// nchat is distributed under the MIT license, see LICENSE for details.
Expand Down Expand Up @@ -291,7 +291,7 @@ void UiHistoryView::Draw()
std::wstring wtime;
if (msg.timeSent != std::numeric_limits<int64_t>::max())
{
wtime = L" (" + StrUtil::ToWString(TimeUtil::GetTimeString(msg.timeSent, true /* p_Short */)) + L")";
wtime = L" (" + StrUtil::ToWString(TimeUtil::GetTimeString(msg.timeSent, false /* p_IsExport */)) + L")";
}

if (!msg.isOutgoing && !msg.isRead)
Expand Down
2 changes: 1 addition & 1 deletion src/uimodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2176,7 +2176,7 @@ std::string UiModel::GetChatStatus(const std::string& p_ProfileId, const std::st
break;

default:
chatStatus = "seen " + TimeUtil::GetTimeString(timeSeen, true /* p_Short */);
chatStatus = "seen " + TimeUtil::GetTimeString(timeSeen, false /* p_IsExport */);
break;
}
}
Expand Down

0 comments on commit 420dd5c

Please sign in to comment.