Introduce prof_date_time_format_iso8601().

Instead of repeating the same pattern over and over, introduce a helper
function that either outputs a formatted timestamp if one is given as
argument or returns the current time if no argument is given.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
This commit is contained in:
Steffen Jaeckel
2026-03-17 17:29:18 +01:00
parent f55f7611c2
commit 349de02b8c
6 changed files with 22 additions and 35 deletions

View File

@@ -241,13 +241,7 @@ _chat_log_chat(const char* const login, const char* const other, const char* msg
g_string_free(other_str, TRUE);
}
if (timestamp == NULL) {
timestamp = g_date_time_new_now_local();
} else {
g_date_time_ref(timestamp);
}
auto_gchar gchar* date_fmt = g_date_time_format_iso8601(timestamp);
auto_gchar gchar* date_fmt = prof_date_time_format_iso8601(timestamp);
FILE* chatlogp = fopen(dated_log->filename, "a");
g_chmod(dated_log->filename, S_IRUSR | S_IWUSR);
if (chatlogp) {
@@ -278,8 +272,6 @@ _chat_log_chat(const char* const login, const char* const other, const char* msg
log_error("Error closing file %s, errno = %d", dated_log->filename, errno);
}
}
g_date_time_unref(timestamp);
}
void
@@ -347,9 +339,7 @@ _groupchat_log_chat(const gchar* const login, const gchar* const room, const gch
g_hash_table_replace(logs, strdup(room), dated_log);
}
GDateTime* dt_tmp = g_date_time_new_now_local();
auto_gchar gchar* date_fmt = g_date_time_format_iso8601(dt_tmp);
auto_gchar gchar* date_fmt = prof_date_time_format_iso8601(NULL);
FILE* grpchatlogp = fopen(dated_log->filename, "a");
g_chmod(dated_log->filename, S_IRUSR | S_IWUSR);
@@ -366,8 +356,6 @@ _groupchat_log_chat(const gchar* const login, const gchar* const room, const gch
log_error("Error closing file %s, errno = %d", dated_log->filename, errno);
}
}
g_date_time_unref(dt_tmp);
}
static char*

View File

@@ -864,6 +864,20 @@ unique_filename_from_url(const char* url, const char* path)
return unique_filename;
}
/* This returns a timestamp formatted in ISO8601 format.
* Either it returns the current time if `dt` is NULL, or
* it returns the formatted time value passed in `dt`.
*/
gchar*
prof_date_time_format_iso8601(GDateTime* dt)
{
GDateTime* dt_ = (dt == NULL) ? g_date_time_new_now_local() : dt;
gchar* ret = g_date_time_format_iso8601(dt_);
if (dt == NULL)
g_date_time_unref(dt_);
return ret;
}
/* build profanity version string.
* example: 0.13.1dev.master.69d8c1f9
*/

View File

@@ -187,6 +187,8 @@ gchar* get_expanded_path(const char* path);
char* basename_from_url(const char* url);
gchar* prof_date_time_format_iso8601(GDateTime* dt);
gchar* prof_get_version(void);
void prof_add_shutdown_routine(void (*routine)(void));

View File

@@ -306,8 +306,7 @@ log_database_get_previous_chat(const gchar* const contact_barejid, const char* s
// Flip order when querying older pages
gchar* sort1 = from_start ? "ASC" : "DESC";
gchar* sort2 = !flip ? "ASC" : "DESC";
GDateTime* now = g_date_time_new_now_local();
auto_gchar gchar* end_date_fmt = end_time ? end_time : g_date_time_format_iso8601(now);
auto_gchar gchar* end_date_fmt = end_time ? end_time : prof_date_time_format_iso8601(NULL);
auto_sqlite gchar* query = sqlite3_mprintf("SELECT * FROM ("
"SELECT COALESCE(B.`message`, A.`message`) AS message, "
"A.`timestamp`, A.`from_jid`, A.`from_resource`, A.`to_jid`, A.`to_resource`, A.`type`, A.`encryption`, A.`stanza_id` FROM `ChatLogs` AS A "
@@ -320,8 +319,6 @@ log_database_get_previous_chat(const gchar* const contact_barejid, const char* s
"ORDER BY `timestamp` %s;",
contact_barejid, myjid->barejid, myjid->barejid, contact_barejid, end_date_fmt, start_time, start_time, sort1, MESSAGES_TO_RETRIEVE, sort2);
g_date_time_unref(now);
if (!query) {
log_error("Could not allocate memory");
return NULL;
@@ -448,16 +445,7 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji
}
char* err_msg;
auto_gchar gchar* date_fmt = NULL;
if (message->timestamp) {
date_fmt = g_date_time_format_iso8601(message->timestamp);
} else {
GDateTime* dt = g_date_time_new_now_local();
date_fmt = g_date_time_format_iso8601(dt);
g_date_time_unref(dt);
}
auto_gchar gchar* date_fmt = prof_date_time_format_iso8601(message->timestamp);
const char* enc = _get_message_enc_str(message->enc);
if (!type) {

View File

@@ -204,14 +204,11 @@ log_close(void)
static void
_log_msg(log_level_t level, const char* const area, const char* const msg)
{
GDateTime* dt = g_date_time_new_now_local();
char* level_str = _log_abbreviation_string_from_level(level);
auto_gchar gchar* date_fmt = g_date_time_format_iso8601(dt);
auto_gchar gchar* date_fmt = prof_date_time_format_iso8601(NULL);
fprintf(logp, "%s: %08d: %s: %s: %s\n", date_fmt, prof_pid, area, level_str, msg);
g_date_time_unref(dt);
fflush(logp);

View File

@@ -682,9 +682,7 @@ win_page_down(ProfWin* window, int scroll_size)
if (bf_size != 0) {
ProfBuffEntry* last_entry = buffer_get_entry(window->layout->buffer, bf_size - 1);
auto_gchar gchar* start = g_date_time_format_iso8601(last_entry->time);
GDateTime* now = g_date_time_new_now_local();
gchar* end_date = g_date_time_format_iso8601(now);
g_date_time_unref(now);
gchar* end_date = prof_date_time_format_iso8601(NULL);
if (*scroll_state != WIN_SCROLL_REACHED_BOTTOM && !chatwin_db_history((ProfChatWin*)window, start, end_date, FALSE)) {
*scroll_state = WIN_SCROLL_REACHED_BOTTOM;
} else if (*scroll_state == WIN_SCROLL_REACHED_BOTTOM) {