Introduce git__date_rfc2822_fmt. Allows for RFC2822 date headers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
diff --git a/src/date.c b/src/date.c
index 7849c2f..0e1b31a 100644
--- a/src/date.c
+++ b/src/date.c
@@ -874,3 +874,31 @@ int git__date_parse(git_time_t *out, const char *date)
*out = approxidate_str(date, time_sec, &error_ret);
return error_ret;
}
+
+int git__date_rfc2822_fmt(char *out, size_t len, const git_time *date)
+{
+ int written;
+ struct tm gmt;
+ time_t t;
+
+ assert(out && date);
+
+ t = (time_t) (date->time + date->offset * 60);
+
+ if (p_gmtime_r (&t, &gmt) == NULL)
+ return -1;
+
+ written = p_snprintf(out, len, "%.3s, %u %.3s %.4u %02u:%02u:%02u %+03d%02d",
+ weekday_names[gmt.tm_wday],
+ gmt.tm_mday,
+ month_names[gmt.tm_mon],
+ gmt.tm_year + 1900,
+ gmt.tm_hour, gmt.tm_min, gmt.tm_sec,
+ date->offset / 60, date->offset % 60);
+
+ if (written < 0 || (written > (int) len - 1))
+ return -1;
+
+ return 0;
+}
+
diff --git a/src/util.h b/src/util.h
index e378786..5c2c563 100644
--- a/src/util.h
+++ b/src/util.h
@@ -20,6 +20,8 @@
# define max(a,b) ((a) > (b) ? (a) : (b))
#endif
+#define GIT_DATE_RFC2822_SZ 32
+
/*
* Custom memory allocation wrappers
* that set error code and error message
@@ -329,6 +331,16 @@ extern int git__parse_bool(int *out, const char *value);
extern int git__date_parse(git_time_t *out, const char *date);
/*
+ * Format a git_time as a RFC2822 string
+ *
+ * @param out buffer to store formatted date; a '\\0' terminator will automatically be added.
+ * @param len size of the buffer; should be atleast `GIT_DATE_RFC2822_SZ` in size;
+ * @param date the date to be formatted
+ * @return 0 if successful; -1 on error
+ */
+extern int git__date_rfc2822_fmt(char *out, size_t len, const git_time *date);
+
+/*
* Unescapes a string in-place.
*
* Edge cases behavior: