Commit 83821678 authored by Guido Gunther's avatar Guido Gunther Committed by Sebastian Krzyszkowiak

timestamp-label: Introduce phosh_time_diff_in_words

This is mostly to make things easily testable
Signed-off-by: Guido Gunther's avatarGuido Günther <guido.gunther@puri.sm>
parent 0098cdfc
/*
* Copyright © 2020 Lugsole
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include <glib.h>
G_BEGIN_DECLS
char *phosh_time_diff_in_words (GDateTime *dt, GDateTime *dt_now);
G_END_DECLS
......@@ -7,6 +7,7 @@
#define G_LOG_DOMAIN "phosh-timestamp-label"
#include "timestamp-label.h"
#include "timestamp-label-priv.h"
#include "config.h"
#include <glib/gi18n.h>
......@@ -45,22 +46,21 @@ G_DEFINE_TYPE (PhoshTimestampLabel, phosh_timestamp_label, GTK_TYPE_LABEL)
#define SECONDS_PER_YEAR 31536000.0
/**
* phosh_time_ago_in_words:
* @time_stamp: the time to represent
* phosh_time_diff_in_words:
* @dt: The target time
* @dt_now: the current time
*
* Generate a string to represent a datetime
* Generate a string to represent a #GDateTime difference. Currently @dt must be in the past of @dt_now.
* Times in the future aren't supported.
*
* Based on [ChattyListRow](https://source.puri.sm/Librem5/chatty/blob/master/src/chatty-list-row.c#L47)
* itself based on the ruby on rails method 'distance_of_time_in_words'
*
* Returns: (transfer full): the generated string
*/
static char *
phosh_time_ago_in_words (GDateTime *time_stamp)
char *
phosh_time_diff_in_words (GDateTime *dt, GDateTime *dt_now)
{
g_autoptr (GDateTime) time_now = g_date_time_new_now_local ();
g_autofree char *fallback = NULL;
const char *unit;
......@@ -103,9 +103,9 @@ phosh_time_ago_in_words (GDateTime *time_stamp)
str_years = C_("timestamp-suffix-years", "y");
/* Translators: this is the date in (short) number only format */
fallback = g_date_time_format (time_stamp, _("%d.%m.%y"));
fallback = g_date_time_format (dt, C_("timestamp-date", "%x"));
dist_in_seconds = g_date_time_difference (time_now, time_stamp) / G_TIME_SPAN_SECOND;
dist_in_seconds = g_date_time_difference (dt_now, dt) / G_TIME_SPAN_SECOND;
seconds = (int) dist_in_seconds;
minutes = (int) (dist_in_seconds / SECONDS_PER_MINUTE);
......@@ -211,6 +211,24 @@ phosh_time_ago_in_words (GDateTime *time_stamp)
}
}
/**
* phosh_time_ago_in_words:
* @dt: the #GDateTime to represent
*
* Generate a string to represent a #GDateTime. Note that @dt must be in the past.
*
* Based on [ChattyListRow](https://source.puri.sm/Librem5/chatty/blob/master/src/chatty-list-row.c#L47)
* itself based on the ruby on rails method 'distance_of_time_in_words'
*
* Returns: (transfer full): the generated string
*/
static char *
phosh_time_ago_in_words (GDateTime *dt)
{
g_autoptr (GDateTime) dt_now = g_date_time_new_now_local ();
return phosh_time_diff_in_words (dt, dt_now);
}
static GTimeSpan
phosh_timestamp_label_calc_timeout (PhoshTimestampLabel *self)
......
/*
* Copyright © 2020 Lugsole
* Copyright (C) 2021 Purism SPC
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "notifications/timestamp-label.h"
#include "notifications/timestamp-label-priv.h"
#include <locale.h>
static void
test_phosh_timestamp_label_new (void)
......@@ -64,6 +68,50 @@ test_phosh_timestamp_label_destroy (void)
g_object_ref_sink (widget);
}
static void
test_phosh_time_diff_in_words (void)
{
g_autofree char *str = NULL;
g_autoptr (GDateTime) dt = NULL;
g_autoptr (GDateTime) dt_now = g_date_time_new_local (2020, 12, 31, 23, 00, 00);
locale_t locale;
locale_t save_locale;
locale = newlocale (LC_ALL_MASK, "C", (locale_t) 0);
g_assert_true (locale != (locale_t)0);
save_locale = uselocale (locale);
dt = g_date_time_new_local (2020, 12, 31, 23, 00, 00);
str = phosh_time_diff_in_words (dt, dt_now);
g_assert_cmpstr (str, ==, "now");
g_date_time_unref (dt);
dt = g_date_time_new_local (2020, 12, 31, 22, 59, 31);
str = phosh_time_diff_in_words (dt, dt_now);
g_assert_cmpstr (str, ==, "<30s");
g_date_time_unref (dt);
dt = g_date_time_new_local (2020, 12, 31, 22, 59, 29);
str = phosh_time_diff_in_words (dt, dt_now);
g_assert_cmpstr (str, ==, "<1m");
g_date_time_unref (dt);
dt = g_date_time_new_local (2020, 12, 31, 22, 30, 00);
str = phosh_time_diff_in_words (dt, dt_now);
g_assert_cmpstr (str, ==, "30m");
g_date_time_unref (dt);
dt = g_date_time_new_local (2020, 12, 31, 21, 00, 00);
str = phosh_time_diff_in_words (dt, dt_now);
g_assert_cmpstr (str, ==, "~2h");
/* Restore previous locale */
uselocale (save_locale);
freelocale (locale);
}
int
main (int argc,
char *argv[])
......@@ -74,5 +122,6 @@ main (int argc,
g_test_add_func ("/phosh/timestamp-label/get-set-timestamp", test_phosh_timestamp_label_get_set_timestamp);
g_test_add_func ("/phosh/timestamp-label/test_phosh_timestamp_label_destroy", test_phosh_timestamp_label_destroy);
g_test_add_func ("/phosh/timestamp-label/test_phosh_time_diff_in_words", test_phosh_time_diff_in_words);
return g_test_run ();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment