Fix time offset calculation in case of unsigned time_t
authorMike Gorchak <mike.gorchak.qnx@gmail.com>
Mon, 25 Feb 2013 21:53:40 +0000 (23:53 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 25 Feb 2013 22:29:12 +0000 (14:29 -0800)
Fix time offset calculation expression in case if time_t
is unsigned. This code works fine for signed and
unsigned time_t.

Signed-off-by: Mike Gorchak <mike.gorchak.qnx@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
date.c
diff --git a/date.c b/date.c
index 1ac28e5e7bf0c1a63526c3bbaf212588a3ea2253..df20d0ba1d9c5d9fa03316968c1b7038659add05 100644 (file)
--- a/date.c
+++ b/date.c
@@ -694,8 +694,14 @@ int parse_date_basic(const char *date, unsigned long *timestamp, int *offset)
 
        /* mktime uses local timezone */
        *timestamp = tm_to_time_t(&tm);
-       if (*offset == -1)
-               *offset = ((time_t)*timestamp - mktime(&tm)) / 60;
+       if (*offset == -1) {
+               time_t temp_time = mktime(&tm);
+               if ((time_t)*timestamp > temp_time) {
+                       *offset = ((time_t)*timestamp - temp_time) / 60;
+               } else {
+                       *offset = -(int)((temp_time - (time_t)*timestamp) / 60);
+               }
+       }
 
        if (*timestamp == -1)
                return -1;