diff --git a/time/Makefile b/time/Makefile
index 486fb02ecb..7de2ce0196 100644
--- a/time/Makefile
+++ b/time/Makefile
@@ -51,7 +51,7 @@ tests := test_time clocktest tst-posixtz tst-strptime tst_wcsftime \
tst-clock tst-clock2 tst-clock_nanosleep tst-cpuclock1 \
tst-adjtime tst-clock-y2038 tst-clock2-y2038 \
tst-cpuclock1-y2038 tst-clock_nanosleep-y2038 tst-clock_settime \
- tst-clock_adjtime tst-ctime
+ tst-clock_adjtime tst-ctime tst-difftime
include ../Rules
diff --git a/time/tst-difftime.c b/time/tst-difftime.c
new file mode 100644
index 0000000000..eb3024c949
--- /dev/null
+++ b/time/tst-difftime.c
@@ -0,0 +1,57 @@
+/* Test for difftime
+ Copyright (C) 2021 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ . */
+
+#include
+#include
+
+static void test_difftime_helper (time_t start, time_t t1,
+ time_t t0, double exp_val)
+{
+ time_t time1, time0;
+
+ time1 = time0 = start;
+ time1 += t1;
+ time0 += t0;
+
+ double sub = difftime (time1, time0);
+ if (sub != exp_val)
+ FAIL_EXIT1 ("*** Difftime returned %f (expected %f)\n", sub, exp_val);
+}
+
+static int
+do_test (void)
+{
+ /* Check if difftime works with current time. */
+ test_difftime_helper (time (NULL), +1800, -1800, 3600.0);
+ test_difftime_helper (time (NULL), -1800, +1800, -3600.0);
+
+ /* Check if difftime works correctly near 32 bit time_t overflow. */
+ if (sizeof (time_t) > 4)
+ {
+ test_difftime_helper (0x7FFFFFFF, +1800, -1800, 3600.0);
+ test_difftime_helper (0x80000000, +1800, -1800, 3600.0);
+ test_difftime_helper (0x80000000, -1800, +1800, -3600.0);
+ test_difftime_helper (0x7FFFFFFF, -1800, +1800, -3600.0);
+ }
+ else
+ FAIL_UNSUPPORTED ("32-bit time_t");
+
+ return 0;
+}
+
+#include