diff --git a/configure.ac b/configure.ac
index efaac57..ae94151 100644
--- a/configure.ac
+++ b/configure.ac
@@ -63,6 +63,7 @@ dnl *** Checks for programs ***
 dnl ***************************
 
 AC_PROG_CC
+AC_PROG_CC_C99
 AC_PROG_INSTALL
 
 # Initialize libtool
@@ -362,6 +363,56 @@ AM_CONDITIONAL(HAVE_CURL, test "$CURL" != no)
 AC_SUBST(MISSING_REGRESSION_TEST_PACKAGES)
 AM_CONDITIONAL(MISSING_REGRESSION_TEST_PACKAGES, test -n "$MISSING_REGRESSION_TEST_PACKAGES")
 
+dnl *************************************************
+dnl *** Detect whether time_t is integer or float ***
+dnl *************************************************
+
+AC_CACHE_CHECK([whether time_t is real-floating],
+               [ac_cv_time_t_float],
+               [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <time.h>], [
+                                                     time_t t = 0;
+                                                     return t % 1;])],
+                                  [ac_cv_time_t_float=no],
+                                  [ac_cv_time_t_float=yes])])
+
+dnl ************************************
+dnl *** Maximum value of type time_t ***
+dnl ************************************
+
+AC_CHECK_SIZEOF(time_t, [], [#include <time.h>])
+AC_DEFINE(TIME_T_MAX, [], [Maximum value of type time_t])
+
+if test "x$ac_cv_time_t_float" = xyes; then
+    AC_DEFINE(TIME_T_FLOAT, [1], [Define to 1 if time_t is real-floating])
+    AC_DEFINE(TIME_T_MAX, 1e+37)
+else
+    if test "x$ac_cv_prog_cc_c99" != xno; then
+	AC_CHECK_SIZEOF(int64_t)
+	if test "$ac_cv_sizeof_time_t" -ge "$ac_cv_sizeof_int64_t"; then
+	    AC_DEFINE(TIME_T_MAX, INT64_MAX)
+	else
+	    AC_CHECK_SIZEOF(int32_t)
+	    if test "$ac_cv_sizeof_time_t" -ge "$ac_cv_sizeof_int32_t"; then
+		AC_DEFINE(TIME_T_MAX, INT32_MAX)
+	    else
+		AC_MSG_ERROR([time_t size mismatch])
+	    fi
+	fi
+    else
+	AC_CHECK_SIZEOF(long)
+	if test "$ac_cv_sizeof_time_t" -ge "$ac_cv_sizeof_long"; then
+	    AC_DEFINE(TIME_T_MAX, LONG_MAX)
+	else
+	    AC_CHECK_SIZEOF(int)
+	    if test "$ac_cv_sizeof_time_t" -ge "$ac_cv_sizeof_int"; then
+		AC_DEFINE(TIME_T_MAX, INT_MAX)
+	    else
+		AC_MSG_ERROR([time_t size mismatch])
+	    fi
+	fi
+    fi
+fi
+
 dnl *************************
 dnl *** Output Everything ***
 dnl *************************
diff --git a/libsoup/soup-cookie-jar-sqlite.c b/libsoup/soup-cookie-jar-sqlite.c
index 2870e5e..8af016d 100644
--- a/libsoup/soup-cookie-jar-sqlite.c
+++ b/libsoup/soup-cookie-jar-sqlite.c
@@ -11,6 +11,7 @@
 #include <config.h>
 #endif
 
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -193,7 +194,9 @@ callback (void *data, int argc, char **argv, char **colname)
 	SoupCookieJar *jar = SOUP_COOKIE_JAR (data);
 
 	char *name, *value, *host, *path;
-	time_t max_age, now;
+	unsigned long expire_time;
+	long max_age;
+	time_t now;
 	gboolean http_only = FALSE, secure = FALSE;
 
 	now = time (NULL);
@@ -202,10 +205,11 @@ callback (void *data, int argc, char **argv, char **colname)
 	value = argv[COL_VALUE];
 	host = argv[COL_HOST];
 	path = argv[COL_PATH];
-	max_age = strtoul (argv[COL_EXPIRY], NULL, 10) - now;
+	expire_time = strtoul (argv[COL_EXPIRY], NULL, 10);
 
-	if (max_age <= 0)
+	if (now >= expire_time)
 		return 0;
+	max_age = (expire_time - now <= LONG_MAX ? expire_time - now : LONG_MAX);
 
 	http_only = (g_strcmp0 (argv[COL_HTTP_ONLY], "1") == 0);
 	secure = (g_strcmp0 (argv[COL_SECURE], "1") == 0);
diff --git a/libsoup/soup-cookie-jar-text.c b/libsoup/soup-cookie-jar-text.c
index 42280ce..cf8864f 100644
--- a/libsoup/soup-cookie-jar-text.c
+++ b/libsoup/soup-cookie-jar-text.c
@@ -9,6 +9,7 @@
 #include <config.h>
 #endif
 
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -167,7 +168,8 @@ parse_cookie (char *line, time_t now)
 	char **result;
 	SoupCookie *cookie = NULL;
 	gboolean http_only;
-	time_t max_age;
+	unsigned long expire_time;
+	long max_age;
 	char *host, *path, *secure, *expires, *name, *value;
 
 	if (g_str_has_prefix (line, "#HttpOnly_")) {
@@ -184,9 +186,10 @@ parse_cookie (char *line, time_t now)
 
 	/* Check this first */
 	expires = result[4];
-	max_age = strtoul (expires, NULL, 10) - now;
-	if (max_age <= 0)
+	expire_time = strtoul (expires, NULL, 10);
+	if (now >= expire_time)
 		goto out;
+	max_age = (expire_time - now <= LONG_MAX ? expire_time - now : LONG_MAX);
 
 	host = result[0];
 
diff --git a/libsoup/soup-cookie.c b/libsoup/soup-cookie.c
index 5e50043..3b0a2f6 100644
--- a/libsoup/soup-cookie.c
+++ b/libsoup/soup-cookie.c
@@ -341,7 +341,7 @@ parse_one_cookie (const char *header, SoupURI *origin)
 static SoupCookie *
 cookie_new_internal (const char *name, const char *value,
 		     const char *domain, const char *path,
-		     int max_age)
+		     long max_age)
 {
 	SoupCookie *cookie;
 
@@ -384,7 +384,7 @@ cookie_new_internal (const char *name, const char *value,
 SoupCookie *
 soup_cookie_new (const char *name, const char *value,
 		 const char *domain, const char *path,
-		 int max_age)
+		 long max_age)
 {
 	g_return_val_if_fail (name != NULL, NULL);
 	g_return_val_if_fail (value != NULL, NULL);
@@ -575,7 +575,7 @@ soup_cookie_set_path (SoupCookie *cookie, const char *path)
  * Since: 2.24
  **/
 void
-soup_cookie_set_max_age (SoupCookie *cookie, int max_age)
+soup_cookie_set_max_age (SoupCookie *cookie, long max_age)
 {
 	if (cookie->expires)
 		soup_date_free (cookie->expires);
diff --git a/libsoup/soup-cookie.h b/libsoup/soup-cookie.h
index 3a3c388..2aeacd6 100644
--- a/libsoup/soup-cookie.h
+++ b/libsoup/soup-cookie.h
@@ -32,7 +32,7 @@ SoupCookie *soup_cookie_new                     (const char  *name,
 						 const char  *value,
 						 const char  *domain,
 						 const char  *path,
-						 int          max_age);
+						 long         max_age);
 SoupCookie *soup_cookie_parse                   (const char  *header,
 						 SoupURI     *origin);
 SoupCookie *soup_cookie_copy                    (SoupCookie  *cookie);
@@ -50,7 +50,7 @@ const char *soup_cookie_get_path                (SoupCookie  *cookie);
 void        soup_cookie_set_path                (SoupCookie  *cookie,
 						 const char  *path);
 void        soup_cookie_set_max_age             (SoupCookie  *cookie,
-						 int          max_age);
+						 long         max_age);
 SoupDate   *soup_cookie_get_expires             (SoupCookie  *cookie);
 void        soup_cookie_set_expires             (SoupCookie  *cookie,
 						 SoupDate    *expires);
diff --git a/libsoup/soup-date.c b/libsoup/soup-date.c
index 99ba2f0..6fb9906 100644
--- a/libsoup/soup-date.c
+++ b/libsoup/soup-date.c
@@ -10,6 +10,13 @@
 #include <config.h>
 #endif
 
+#ifndef TIME_T_FLOAT
+# ifdef HAVE_STDINT_H
+#  include <stdint.h>
+# else
+#  include <limits.h>
+# endif
+#endif /* TIME_T_FLOAT */
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
@@ -203,9 +210,14 @@ soup_date_new (int year, int month, int day,
  * Return value: a new #SoupDate
  **/
 SoupDate *
-soup_date_new_from_now (int offset_seconds)
+soup_date_new_from_now (long offset_seconds)
 {
-	return soup_date_new_from_time_t (time (NULL) + offset_seconds);
+	time_t now = time(NULL);
+
+	if (now > TIME_T_MAX - offset_seconds)
+		return soup_date_new_from_time_t (TIME_T_MAX);
+
+	return soup_date_new_from_time_t (now + offset_seconds);
 }
 
 static gboolean
diff --git a/libsoup/soup-date.h b/libsoup/soup-date.h
index 0924607..e1cfe76 100644
--- a/libsoup/soup-date.h
+++ b/libsoup/soup-date.h
@@ -46,7 +46,7 @@ SoupDate *soup_date_new             (int             year,
 				     int             second);
 SoupDate *soup_date_new_from_string (const char     *date_string);
 SoupDate *soup_date_new_from_time_t (time_t          when);
-SoupDate *soup_date_new_from_now    (int             offset_seconds);
+SoupDate *soup_date_new_from_now    (long            offset_seconds);
 
 char     *soup_date_to_string       (SoupDate       *date,
 				     SoupDateFormat  format);
