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

[PATCH 1/1] Fix time offset calculation in case of unsigned time_t

From
Mike Gorchak <mike.gorchak.qnx@gmail.com>
Date
Feb 25, 2013, 21:53 UTC
Message-ID
<CAHXAxrO3nSVmALnefXWbvjR-=dOgVHDDVhyhJCeu9fsco=vjOw@mail.gmail.com>

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>
---
 date.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/date.c b/date.c
index 1ac28e5..11ee7b4 100644
--- a/date.c
+++ b/date.c
@@ -648,6 +648,7 @@ int parse_date_basic(const char *date, unsigned
long *timestamp, int *offset)
 	struct tm tm;
 	int tm_gmt;
 	unsigned long dummy_timestamp;
+	time_t temp_time;
 	int dummy_offset;

 	if (!timestamp)
@@ -694,8 +695,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) {
+		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;
-- 
1.8.2-rc0
Message 1 of 1 in “Fix time offset calculation in case of unsigned time_t”
  1. 1/1 Fix time offset calculation in case of unsigned time_tMike Gorchak, Feb 25, 2013

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.