Skip to content

Commit

Permalink
fix(DateTime): is_3_months_old being year aware
Browse files Browse the repository at this point in the history
  • Loading branch information
GeopJr committed Jan 1, 2024
1 parent 8a4708d commit 71f6c57
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 43 deletions.
9 changes: 1 addition & 8 deletions src/Utils/DateTime.vala
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,7 @@ public class Tuba.DateTime {
var now = new GLib.DateTime.now_local ();
if (now.difference (date) < 0) return false;

var date_month = date.get_month ();
var now_month = now.get_month ();

if (date.get_year () == now.get_year ()) {
return now_month - date_month > 3;
} else {
return now_month + 12 - date_month > 3;
}
return now.get_month () + 12 * (now.get_year () - date.get_year ()) - date.get_month () > 3;
}

public static bool is_same_day (GLib.DateTime d1, GLib.DateTime d2) {
Expand Down
48 changes: 13 additions & 35 deletions tests/DateTime.test.vala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ struct TestDate {
public string left;
public string ago;
public string human;
public string? old;
}

TestDate[] get_dates () {
Expand All @@ -19,8 +18,7 @@ TestDate[] get_dates () {
).to_string (),
"Jun 29, 2002",
"expired on Jun 29, 2002",
"Jun 29, 2002",
null // variable
"Jun 29, 2002"
}
};

Expand All @@ -31,71 +29,63 @@ TestDate[] get_dates () {
iso8601 = one_day.to_string (),
left = "23h left",
ago = one_day.format ("expires on %b %-e, %Y %H:%m"),
human = one_day.format ("%b %-e, %Y %H:%m"),
old = "in the future"
human = one_day.format ("%b %-e, %Y %H:%m")
};

var m_one_day = time_now.add_days (-1);
res += TestDate () {
iso8601 = m_one_day.to_string (),
left = "Yesterday",
ago = "expired yesterday",
human = "Yesterday",
old = "a day old"
human = "Yesterday"
};

var one_hour = time_now.add_hours (1);
res += TestDate () {
iso8601 = one_hour.to_string (),
left = "59m left",
ago = one_hour.format ("expires on %b %-e, %Y %H:%m"),
human = one_hour.format ("%b %-e, %Y %H:%m"),
old = "in the future"
human = one_hour.format ("%b %-e, %Y %H:%m")
};

var m_one_hour = time_now.add_hours (-1);
res += TestDate () {
iso8601 = m_one_hour.to_string (),
left = "1h",
ago = "expired 1h ago",
human = "1h",
old = "an hour old"
human = "1h"
};

var two_minutes = time_now.add_minutes (2);
res += TestDate () {
iso8601 = two_minutes.to_string (),
left = "1m left",
ago = two_minutes.format ("expires on %b %-e, %Y %H:%m"),
human = two_minutes.format ("%b %-e, %Y %H:%m"),
old = "in the future"
human = two_minutes.format ("%b %-e, %Y %H:%m")
};

var m_two_minutes = time_now.add_minutes (-2);
res += TestDate () {
iso8601 = m_two_minutes.to_string (),
left = "2m",
ago = "expired 2m ago",
human = "2m",
old = "2 minutes old"
human = "2m"
};

var twenty_seconds = time_now.add_seconds (20);
res += TestDate () {
iso8601 = twenty_seconds.to_string (),
left = "expires soon",
ago = twenty_seconds.format ("expires on %b %-e, %Y %H:%m"),
human = twenty_seconds.format ("%b %-e, %Y %H:%m"),
old = "in the future"
human = twenty_seconds.format ("%b %-e, %Y %H:%m")
};

var m_twenty_seconds = time_now.add_seconds (-20);
res += TestDate () {
iso8601 = m_twenty_seconds.to_string (),
left = "Just now",
ago = "expired on just now",
human = "Just now",
old = "less than a minute old"
human = "Just now"
};

var some_date = new DateTime.local (
Expand All @@ -109,19 +99,17 @@ TestDate[] get_dates () {
var m_four_months = some_date.add_months (-4);
res += TestDate () {
iso8601 = m_four_months.to_string (),
left = "Aug 8",
ago = "expired on Aug 8",
human = "Aug 8",
old = "4 months old"
left = "Aug 8, 2023",
ago = "expired on Aug 8, 2023",
human = "Aug 8, 2023"
};

var m_fourteen_months = some_date.add_months (-14);
res += TestDate () {
iso8601 = m_fourteen_months.to_string (),
left = "Oct 8, 2022",
ago = "expired on Oct 8, 2022",
human = "Oct 8, 2022",
old = "a year old"
human = "Oct 8, 2022"
};

return res;
Expand Down Expand Up @@ -151,15 +139,6 @@ public void test_humanize () {
}
}

public void test_old () {
foreach (var test_date in get_dates ()) {
if (test_date.old == null) continue;
var old_date = Tuba.DateTime.humanize_old (test_date.iso8601);

assert_cmpstr (old_date, CompareOperator.EQ, test_date.old);
}
}

struct Test3Months {
public string iso8601;
public bool res;
Expand Down Expand Up @@ -217,7 +196,6 @@ public int main (string[] args) {
Test.add_func ("/test_left", test_left);
Test.add_func ("/test_ago", test_ago);
Test.add_func ("/test_humanize", test_humanize);
Test.add_func ("/test_old", test_old);
Test.add_func ("/test_3_months", test_3_months);
return Test.run ();
}

0 comments on commit 71f6c57

Please sign in to comment.