summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2021-11-19 08:17:28 -0500
committerGitHub <noreply@github.com>2021-11-19 08:17:28 -0500
commit19743830e651bc67701cf99caf540093b62a35fa (patch)
tree7266dc18143c766f6242c6dc05835ea5c2793746 /src
parent1604be06685312f659675f3bc17b287108e00af1 (diff)
parentadd30a838867820788c871c1d034fcf68c2ad12c (diff)
downloadlibgit2-19743830e651bc67701cf99caf540093b62a35fa.tar.gz
Merge pull request #6121 from libgit2/ethomson/date
util: minor cleanup and refactoring to the date class
Diffstat (limited to 'src')
-rw-r--r--src/date.c22
-rw-r--r--src/date.h33
-rw-r--r--src/email.c18
-rw-r--r--src/revparse.c3
-rw-r--r--src/util.h22
5 files changed, 58 insertions, 40 deletions
diff --git a/src/date.c b/src/date.c
index 2297ee66c..52cc30824 100644
--- a/src/date.c
+++ b/src/date.c
@@ -13,6 +13,7 @@
#include "util.h"
#include "cache.h"
#include "posix.h"
+#include "date.h"
#include <ctype.h>
#include <time.h>
@@ -857,7 +858,7 @@ static git_time_t approxidate_str(const char *date,
return update_tm(&tm, &now, 0);
}
-int git__date_parse(git_time_t *out, const char *date)
+int git_date_parse(git_time_t *out, const char *date)
{
time_t time_sec;
git_time_t timestamp;
@@ -875,31 +876,24 @@ int git__date_parse(git_time_t *out, const char *date)
return error_ret;
}
-int git__date_rfc2822_fmt(char *out, size_t len, const git_time *date)
+int git_date_rfc2822_fmt(git_str *out, git_time_t time, int offset)
{
- int written;
- struct tm gmt;
time_t t;
+ struct tm gmt;
GIT_ASSERT_ARG(out);
- GIT_ASSERT_ARG(date);
- t = (time_t) (date->time + date->offset * 60);
+ t = (time_t) (time + offset * 60);
- if (p_gmtime_r (&t, &gmt) == NULL)
+ if (p_gmtime_r(&t, &gmt) == NULL)
return -1;
- written = p_snprintf(out, len, "%.3s, %u %.3s %.4u %02u:%02u:%02u %+03d%02d",
+ return git_str_printf(out, "%.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;
+ offset / 60, offset % 60);
}
diff --git a/src/date.h b/src/date.h
new file mode 100644
index 000000000..7ebd3c30e
--- /dev/null
+++ b/src/date.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_date_h__
+#define INCLUDE_date_h__
+
+#include "util.h"
+#include "str.h"
+
+/*
+ * Parse a string into a value as a git_time_t.
+ *
+ * Sample valid input:
+ * - "yesterday"
+ * - "July 17, 2003"
+ * - "2003-7-17 08:23"
+ */
+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
+ * @param time the time to be formatted
+ * @param offset the timezone offset
+ * @return 0 if successful; -1 on error
+ */
+extern int git_date_rfc2822_fmt(git_str *out, git_time_t time, int offset);
+
+#endif
diff --git a/src/email.c b/src/email.c
index 3459c0540..e19a2928c 100644
--- a/src/email.c
+++ b/src/email.c
@@ -12,6 +12,7 @@
#include "diff_generate.h"
#include "diff_stats.h"
#include "patch.h"
+#include "date.h"
#include "git2/email.h"
#include "git2/patch.h"
@@ -72,6 +73,19 @@ static int append_prefix(
return git_str_oom(out) ? -1 : 0;
}
+static int append_date(
+ git_str *out,
+ const git_time *date)
+{
+ int error;
+
+ if ((error = git_str_printf(out, "Date: ")) == 0 &&
+ (error = git_date_rfc2822_fmt(out, date->time, date->offset)) == 0)
+ error = git_str_putc(out, '\n');
+
+ return error;
+}
+
static int append_subject(
git_str *out,
size_t patch_idx,
@@ -117,14 +131,12 @@ static int append_header(
git_email_create_options *opts)
{
char id[GIT_OID_HEXSZ];
- char date[GIT_DATE_RFC2822_SZ];
int error;
if ((error = git_oid_fmt(id, commit_id)) < 0 ||
(error = git_str_printf(out, "From %.*s %s\n", GIT_OID_HEXSZ, id, EMAIL_TIMESTAMP)) < 0 ||
(error = git_str_printf(out, "From: %s <%s>\n", author->name, author->email)) < 0 ||
- (error = git__date_rfc2822_fmt(date, sizeof(date), &author->when)) < 0 ||
- (error = git_str_printf(out, "Date: %s\n", date)) < 0 ||
+ (error = append_date(out, &author->when)) < 0 ||
(error = append_subject(out, patch_idx, patch_count, summary, opts)) < 0)
return error;
diff --git a/src/revparse.c b/src/revparse.c
index cf39936a5..52dd0720b 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -11,6 +11,7 @@
#include "tree.h"
#include "refdb.h"
#include "regexp.h"
+#include "date.h"
#include "git2.h"
@@ -344,7 +345,7 @@ static int handle_at_syntax(git_object **out, git_reference **ref, const char *s
goto cleanup;
}
- if (git__date_parse(&timestamp, curly_braces_content) < 0)
+ if (git_date_parse(&timestamp, curly_braces_content) < 0)
goto cleanup;
error = retrieve_revobject_from_reflog(out, ref, repo, git_str_cstr(&identifier), (size_t)timestamp);
diff --git a/src/util.h b/src/util.h
index 30cdd0ddf..141779ade 100644
--- a/src/util.h
+++ b/src/util.h
@@ -37,8 +37,6 @@
# define GIT_CONTAINER_OF(ptr, type, member) (type *)(ptr)
#endif
-#define GIT_DATE_RFC2822_SZ 32
-
/**
* Return the length of a constant string.
* We are aware that `strlen` performs the same task and is usually
@@ -295,26 +293,6 @@ GIT_INLINE(bool) git__isxdigit(int c)
extern int git__parse_bool(int *out, const char *value);
/*
- * Parse a string into a value as a git_time_t.
- *
- * Sample valid input:
- * - "yesterday"
- * - "July 17, 2003"
- * - "2003-7-17 08:23"
- */
-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: