--- src/lib-imap/imap-date.c
+++ src/lib-imap/imap-date.c
@@ -86,9 +86,17 @@ static bool tm_is_too_large(const struct
 
 	if (max_time == 0) {
 #if TIME_T_MAX_BITS == 32
+#ifdef TIME_T_SIGNED
+		max_time = 0x7fffffffL;
+#else
 		max_time = 0xffffffffUL;
+#endif
 #elif TIME_T_MAX_BITS == 64
+#ifdef TIME_T_SIGNED
+		max_time = 0x7fffffffffffffffLL;
+#else
 		max_time = 0xffffffffffffffffULL;
+#endif
 #else
 		max_time = ((time_t)1 << TIME_T_MAX_BITS) - 1;
 #endif
--- src/lib-imap/test-imap-date.c
+++ src/lib-imap/test-imap-date.c
@@ -15,7 +15,7 @@ static void test_imap_date(void)
 	} tests[] = {
 		{ "01-Jan-1970", 0 },
 		{ "19-Jan-2038", 2147472000 },
-#if TIME_T_MAX_BITS >= 32
+#if TIME_T_MAX_BITS > 32 || (TIME_T_MAX_BITS == 32 && !defined(TIME_T_SIGNED))
 		{ "07-Feb-2106", 4294944000 },
 #endif
 #if TIME_T_MAX_BITS >= 37
@@ -25,7 +25,7 @@ static void test_imap_date(void)
 		{ "31-Dec-9999", 253402214400LL },
 #endif
 		/* conversions to maximum values */
-#if TIME_T_MAX_BITS == 31
+#if TIME_T_MAX_BITS == 31 || (TIME_T_MAX_BITS == 32 && defined(TIME_T_SIGNED))
 		{ "20-Jan-2038", 2147483647 },
 		{ "31-Dec-9999", 2147483647 },
 #elif TIME_T_MAX_BITS == 32
@@ -60,7 +60,7 @@ static void test_imap_datetime(void)
 		{ "01-Jan-1970 00:00:00 +0000", 0, 0 },
 		{ "19-Jan-2038 03:14:07 +0000", 2147483647, 0 },
 		{ "19-Jan-2038 05:14:07 +0200", 2147483647, 2*60 },
-#if TIME_T_MAX_BITS >= 32
+#if TIME_T_MAX_BITS > 32 || (TIME_T_MAX_BITS == 32 && !defined(TIME_T_SIGNED))
 		{ "07-Feb-2106 06:28:15 +0000", 4294967295, 0 },
 #endif
 #if TIME_T_MAX_BITS >= 37
@@ -71,7 +71,7 @@ static void test_imap_datetime(void)
 		{ "31-Dec-9999 23:59:59 -2359", 253402300799LL + 23*60*60 + 59*60, -23*60 - 59 },
 #endif
 		/* conversions to maximum values */
-#if TIME_T_MAX_BITS == 31
+#if TIME_T_MAX_BITS == 31 || (TIME_T_MAX_BITS == 32 && defined(TIME_T_SIGNED))
 		{ "19-Jan-2038 03:14:08 +0000", 2147483647, 0 },
 		{ "31-Dec-9999 23:59:59 -2359", 2147483647, -23*60 - 59 },
 #elif TIME_T_MAX_BITS == 32
--- src/lib/test-event-filter.c
+++ src/lib/test-event-filter.c
@@ -1003,32 +1003,32 @@ static void test_event_filter_interval_v
 		{ "field > 1mins", 60 * 1000 * 1000, FALSE },
 		{ "field < 1mins", 60 * 1000 * 1000, FALSE },
 
-		{ "field = 1hours", 60L * 60 * 1000 * 1000, TRUE },
-		{ "field = 1h", 60L * 60 * 1000 * 1000, TRUE },
-		{ "field = 3600000000", 60L * 60 * 1000 * 1000, TRUE },
-		{ "field >= 1hours", 60L * 60 * 1000 * 1000, TRUE },
-		{ "field <= 1hours", 60L * 60 * 1000 * 1000, TRUE },
-		{ "field > 1mins", 60L * 60 * 1000 * 1000, TRUE },
-		{ "field > 1hours", 60L * 60 * 1000 * 1000, FALSE },
-		{ "field < 1hours", 60L * 60 * 1000 * 1000, FALSE },
+		{ "field = 1hours", 60LL * 60 * 1000 * 1000, TRUE },
+		{ "field = 1h", 60LL * 60 * 1000 * 1000, TRUE },
+		{ "field = 3600000000", 60LL * 60 * 1000 * 1000, TRUE },
+		{ "field >= 1hours", 60LL * 60 * 1000 * 1000, TRUE },
+		{ "field <= 1hours", 60LL * 60 * 1000 * 1000, TRUE },
+		{ "field > 1mins", 60LL * 60 * 1000 * 1000, TRUE },
+		{ "field > 1hours", 60LL * 60 * 1000 * 1000, FALSE },
+		{ "field < 1hours", 60LL * 60 * 1000 * 1000, FALSE },
 
-		{ "field = 1days", 24L * 60 * 60 * 1000 * 1000, TRUE },
-		{ "field = 1d", 24L * 60 * 60 * 1000 * 1000, TRUE },
-		{ "field = 86400000000", 24L * 60 * 60 * 1000 * 1000, TRUE },
-		{ "field >= 1days", 24L * 60 * 60 * 1000 * 1000, TRUE },
-		{ "field <= 1days", 24L * 60 * 60 * 1000 * 1000, TRUE },
-		{ "field > 1hours", 24L * 60 * 60 * 1000 * 1000, TRUE },
-		{ "field > 1days", 24L * 60 * 60 * 1000 * 1000, FALSE },
-		{ "field < 1days", 24L * 60 * 60 * 1000 * 1000, FALSE },
+		{ "field = 1days", 24LL * 60 * 60 * 1000 * 1000, TRUE },
+		{ "field = 1d", 24LL * 60 * 60 * 1000 * 1000, TRUE },
+		{ "field = 86400000000", 24LL * 60 * 60 * 1000 * 1000, TRUE },
+		{ "field >= 1days", 24LL * 60 * 60 * 1000 * 1000, TRUE },
+		{ "field <= 1days", 24LL * 60 * 60 * 1000 * 1000, TRUE },
+		{ "field > 1hours", 24LL * 60 * 60 * 1000 * 1000, TRUE },
+		{ "field > 1days", 24LL * 60 * 60 * 1000 * 1000, FALSE },
+		{ "field < 1days", 24LL * 60 * 60 * 1000 * 1000, FALSE },
 
-		{ "field = 1weeks", 7L * 24 * 60 * 60 * 1000 * 1000, TRUE },
-		{ "field = 1w", 7L * 24 * 60 * 60 * 1000 * 1000, TRUE },
-		{ "field = 604800000000", 7L * 24 * 60 * 60 * 1000 * 1000, TRUE },
-		{ "field >= 1weeks", 7L * 24 * 60 * 60 * 1000 * 1000, TRUE },
-		{ "field <= 1weeks", 7L * 24 * 60 * 60 * 1000 * 1000, TRUE },
-		{ "field > 1days", 7L * 24 * 60 * 60 * 1000 * 1000, TRUE },
-		{ "field > 1weeks", 7L * 24 * 60 * 60 * 1000 * 1000, FALSE },
-		{ "field < 1weeks", 7L * 24 * 60 * 60 * 1000 * 1000, FALSE },
+		{ "field = 1weeks", 7LL * 24 * 60 * 60 * 1000 * 1000, TRUE },
+		{ "field = 1w", 7LL * 24 * 60 * 60 * 1000 * 1000, TRUE },
+		{ "field = 604800000000", 7LL * 24 * 60 * 60 * 1000 * 1000, TRUE },
+		{ "field >= 1weeks", 7LL * 24 * 60 * 60 * 1000 * 1000, TRUE },
+		{ "field <= 1weeks", 7LL * 24 * 60 * 60 * 1000 * 1000, TRUE },
+		{ "field > 1days", 7LL * 24 * 60 * 60 * 1000 * 1000, TRUE },
+		{ "field > 1weeks", 7LL * 24 * 60 * 60 * 1000 * 1000, FALSE },
+		{ "field < 1weeks", 7LL * 24 * 60 * 60 * 1000 * 1000, FALSE },
 	};
 
 	struct event_filter *filter;
