git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] tm_to_time_t(): allow times in year 1969

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 11, 2009, 03:20 UTC
Message-ID
<7vfxd3rjmh.fsf_-_@alter.siamese.dyndns.org>
In-Reply-To
<7vk52frjv9.fsf@alter.siamese.dyndns.org>

We used to reject anything older than January 1st 1970 regardless of the timezone, but in order to parse UNIX epoch in zones west of GMT, we need to allow the time without zone to be a little bit into 1969.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 date.c |   16 ++++++++--------
 1 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/date.c b/date.c
index 409a17d..5d259af 100644
--- a/date.c
+++ b/date.c
@@ -6,6 +6,7 @@
 
 #include "cache.h"
 
+#define BAD_STRUCT_TM (-400*24*60*60)
 /*
  * This is like mktime, but without normalization of tm_wday and tm_yday.
  */
@@ -18,10 +19,10 @@ time_t tm_to_time_t(const struct tm *tm)
 	int month = tm->tm_mon;
 	int day = tm->tm_mday;
 
-	if (year < 0 || year > 129) /* algo only works for 1970-2099 */
-		return -1;
+	if (year < -1 || year > 129) /* algo only works for 1969-2099 */
+		return BAD_STRUCT_TM;
 	if (month < 0 || month > 11) /* array bounds */
-		return -1;
+		return BAD_STRUCT_TM;
 	if (month < 2 || (year + 2) % 4)
 		day--;
 	return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
@@ -337,7 +338,7 @@ static int is_date(int year, int month, int day, struct tm *now_tm, time_t now,
 				return 1;
 			r->tm_year = now_tm->tm_year;
 		}
-		else if (year >= 1970 && year < 2100)
+		else if (year >= 1969 && year < 2100)
 			r->tm_year = year - 1900;
 		else if (year > 70 && year < 100)
 			r->tm_year = year;
@@ -611,12 +612,11 @@ int parse_date(const char *date, char *result, int maxlen)
 
 	/* mktime uses local timezone */
 	then = tm_to_time_t(&tm);
-	if (offset == -1)
-		offset = (then - mktime(&tm)) / 60;
-
-	if (then == -1)
+	if (then == BAD_STRUCT_TM)
 		return -1;
 
+	if (offset == -1)
+		offset = (then - mktime(&tm)) / 60;
 	if (!tm_gmt)
 		then -= offset * 60;
 	return date_string(then, offset, result, maxlen);
-- 
1.6.3.3.412.gf581d
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 7 in “Re: What's happening with vr41xx_giu.c?”
  1. Ralf BaechleJul 10, 2009
  2. Junio C HamanoJul 10, 2009
  3. Junio C HamanoJul 11, 2009
  4. 1/2 tm_to_time_t(): allow times in year 1969Junio C Hamano, Jul 11, 2009
  5. 2/2 apply: notice creation/removal patches produced by GNU diffJunio C Hamano, Jul 11, 2009
  6. Linus TorvaldsJul 11, 2009
  7. Junio C HamanoJul 11, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.