Skip to content

Commit 1974383

Browse files
authored
Merge pull request libgit2#6121 from libgit2/ethomson/date
util: minor cleanup and refactoring to the date class
2 parents 1604be0 + add30a8 commit 1974383

File tree

7 files changed

+76
-61
lines changed

7 files changed

+76
-61
lines changed

src/date.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "util.h"
1414
#include "cache.h"
1515
#include "posix.h"
16+
#include "date.h"
1617

1718
#include <ctype.h>
1819
#include <time.h>
@@ -857,7 +858,7 @@ static git_time_t approxidate_str(const char *date,
857858
return update_tm(&tm, &now, 0);
858859
}
859860

860-
int git__date_parse(git_time_t *out, const char *date)
861+
int git_date_parse(git_time_t *out, const char *date)
861862
{
862863
time_t time_sec;
863864
git_time_t timestamp;
@@ -875,31 +876,24 @@ int git__date_parse(git_time_t *out, const char *date)
875876
return error_ret;
876877
}
877878

878-
int git__date_rfc2822_fmt(char *out, size_t len, const git_time *date)
879+
int git_date_rfc2822_fmt(git_str *out, git_time_t time, int offset)
879880
{
880-
int written;
881-
struct tm gmt;
882881
time_t t;
882+
struct tm gmt;
883883

884884
GIT_ASSERT_ARG(out);
885-
GIT_ASSERT_ARG(date);
886885

887-
t = (time_t) (date->time + date->offset * 60);
886+
t = (time_t) (time + offset * 60);
888887

889-
if (p_gmtime_r (&t, &gmt) == NULL)
888+
if (p_gmtime_r(&t, &gmt) == NULL)
890889
return -1;
891890

892-
written = p_snprintf(out, len, "%.3s, %u %.3s %.4u %02u:%02u:%02u %+03d%02d",
891+
return git_str_printf(out, "%.3s, %u %.3s %.4u %02u:%02u:%02u %+03d%02d",
893892
weekday_names[gmt.tm_wday],
894893
gmt.tm_mday,
895894
month_names[gmt.tm_mon],
896895
gmt.tm_year + 1900,
897896
gmt.tm_hour, gmt.tm_min, gmt.tm_sec,
898-
date->offset / 60, date->offset % 60);
899-
900-
if (written < 0 || (written > (int) len - 1))
901-
return -1;
902-
903-
return 0;
897+
offset / 60, offset % 60);
904898
}
905899

src/date.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
#ifndef INCLUDE_date_h__
8+
#define INCLUDE_date_h__
9+
10+
#include "util.h"
11+
#include "str.h"
12+
13+
/*
14+
* Parse a string into a value as a git_time_t.
15+
*
16+
* Sample valid input:
17+
* - "yesterday"
18+
* - "July 17, 2003"
19+
* - "2003-7-17 08:23"
20+
*/
21+
extern int git_date_parse(git_time_t *out, const char *date);
22+
23+
/*
24+
* Format a git_time as a RFC2822 string
25+
*
26+
* @param out buffer to store formatted date
27+
* @param time the time to be formatted
28+
* @param offset the timezone offset
29+
* @return 0 if successful; -1 on error
30+
*/
31+
extern int git_date_rfc2822_fmt(git_str *out, git_time_t time, int offset);
32+
33+
#endif

src/email.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "diff_generate.h"
1313
#include "diff_stats.h"
1414
#include "patch.h"
15+
#include "date.h"
1516

1617
#include "git2/email.h"
1718
#include "git2/patch.h"
@@ -72,6 +73,19 @@ static int append_prefix(
7273
return git_str_oom(out) ? -1 : 0;
7374
}
7475

76+
static int append_date(
77+
git_str *out,
78+
const git_time *date)
79+
{
80+
int error;
81+
82+
if ((error = git_str_printf(out, "Date: ")) == 0 &&
83+
(error = git_date_rfc2822_fmt(out, date->time, date->offset)) == 0)
84+
error = git_str_putc(out, '\n');
85+
86+
return error;
87+
}
88+
7589
static int append_subject(
7690
git_str *out,
7791
size_t patch_idx,
@@ -117,14 +131,12 @@ static int append_header(
117131
git_email_create_options *opts)
118132
{
119133
char id[GIT_OID_HEXSZ];
120-
char date[GIT_DATE_RFC2822_SZ];
121134
int error;
122135

123136
if ((error = git_oid_fmt(id, commit_id)) < 0 ||
124137
(error = git_str_printf(out, "From %.*s %s\n", GIT_OID_HEXSZ, id, EMAIL_TIMESTAMP)) < 0 ||
125138
(error = git_str_printf(out, "From: %s <%s>\n", author->name, author->email)) < 0 ||
126-
(error = git__date_rfc2822_fmt(date, sizeof(date), &author->when)) < 0 ||
127-
(error = git_str_printf(out, "Date: %s\n", date)) < 0 ||
139+
(error = append_date(out, &author->when)) < 0 ||
128140
(error = append_subject(out, patch_idx, patch_count, summary, opts)) < 0)
129141
return error;
130142

src/revparse.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "tree.h"
1212
#include "refdb.h"
1313
#include "regexp.h"
14+
#include "date.h"
1415

1516
#include "git2.h"
1617

@@ -344,7 +345,7 @@ static int handle_at_syntax(git_object **out, git_reference **ref, const char *s
344345
goto cleanup;
345346
}
346347

347-
if (git__date_parse(&timestamp, curly_braces_content) < 0)
348+
if (git_date_parse(&timestamp, curly_braces_content) < 0)
348349
goto cleanup;
349350

350351
error = retrieve_revobject_from_reflog(out, ref, repo, git_str_cstr(&identifier), (size_t)timestamp);

src/util.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
# define GIT_CONTAINER_OF(ptr, type, member) (type *)(ptr)
3838
#endif
3939

40-
#define GIT_DATE_RFC2822_SZ 32
41-
4240
/**
4341
* Return the length of a constant string.
4442
* We are aware that `strlen` performs the same task and is usually
@@ -294,26 +292,6 @@ GIT_INLINE(bool) git__isxdigit(int c)
294292
*/
295293
extern int git__parse_bool(int *out, const char *value);
296294

297-
/*
298-
* Parse a string into a value as a git_time_t.
299-
*
300-
* Sample valid input:
301-
* - "yesterday"
302-
* - "July 17, 2003"
303-
* - "2003-7-17 08:23"
304-
*/
305-
extern int git__date_parse(git_time_t *out, const char *date);
306-
307-
/*
308-
* Format a git_time as a RFC2822 string
309-
*
310-
* @param out buffer to store formatted date; a '\\0' terminator will automatically be added.
311-
* @param len size of the buffer; should be atleast `GIT_DATE_RFC2822_SZ` in size;
312-
* @param date the date to be formatted
313-
* @return 0 if successful; -1 on error
314-
*/
315-
extern int git__date_rfc2822_fmt(char *out, size_t len, const git_time *date);
316-
317295
/*
318296
* Unescapes a string in-place.
319297
*

tests/date/date.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#include "clar_libgit2.h"
22

3-
#include "util.h"
3+
#include "date.h"
44

55
void test_date_date__overflow(void)
66
{
77
#ifdef __LP64__
88
git_time_t d2038, d2039;
99

1010
/* This is expected to fail on a 32-bit machine. */
11-
cl_git_pass(git__date_parse(&d2038, "2038-1-1"));
12-
cl_git_pass(git__date_parse(&d2039, "2039-1-1"));
11+
cl_git_pass(git_date_parse(&d2038, "2038-1-1"));
12+
cl_git_pass(git_date_parse(&d2039, "2039-1-1"));
1313
cl_assert(d2038 < d2039);
1414
#endif
1515
}

tests/date/rfc2822.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
#include "clar_libgit2.h"
22

3-
#include "util.h"
3+
#include "date.h"
44

55
void test_date_rfc2822__format_rfc2822_no_offset(void)
66
{
77
git_time t = {1397031663, 0};
8-
char buf[GIT_DATE_RFC2822_SZ];
8+
git_str buf = GIT_STR_INIT;
99

10-
cl_git_pass(git__date_rfc2822_fmt(buf, sizeof(buf), &t));
11-
cl_assert(strcmp(buf, "Wed, 9 Apr 2014 08:21:03 +0000") == 0);
10+
cl_git_pass(git_date_rfc2822_fmt(&buf, t.time, t.offset));
11+
cl_assert_equal_s("Wed, 9 Apr 2014 08:21:03 +0000", buf.ptr);
12+
13+
git_str_dispose(&buf);
1214
}
1315

1416
void test_date_rfc2822__format_rfc2822_positive_offset(void)
1517
{
1618
git_time t = {1397031663, 120};
17-
char buf[GIT_DATE_RFC2822_SZ];
19+
git_str buf = GIT_STR_INIT;
20+
21+
cl_git_pass(git_date_rfc2822_fmt(&buf, t.time, t.offset));
22+
cl_assert_equal_s("Wed, 9 Apr 2014 10:21:03 +0200", buf.ptr);
1823

19-
cl_git_pass(git__date_rfc2822_fmt(buf, sizeof(buf), &t));
20-
cl_assert(strcmp(buf, "Wed, 9 Apr 2014 10:21:03 +0200") == 0);
24+
git_str_dispose(&buf);
2125
}
2226

2327
void test_date_rfc2822__format_rfc2822_negative_offset(void)
2428
{
2529
git_time t = {1397031663, -120};
26-
char buf[GIT_DATE_RFC2822_SZ];
30+
git_str buf = GIT_STR_INIT;
2731

28-
cl_git_pass(git__date_rfc2822_fmt(buf, sizeof(buf), &t));
29-
cl_assert(strcmp(buf, "Wed, 9 Apr 2014 06:21:03 -0200") == 0);
30-
}
31-
32-
void test_date_rfc2822__format_rfc2822_buffer_too_small(void)
33-
{
34-
/* "Wed, 10 Apr 2014 08:21:03 +0000" */
35-
git_time t = {1397031663 + 86400, 0};
36-
char buf[GIT_DATE_RFC2822_SZ-1];
32+
cl_git_pass(git_date_rfc2822_fmt(&buf, t.time, t.offset));
33+
cl_assert_equal_s("Wed, 9 Apr 2014 06:21:03 -0200", buf.ptr);
3734

38-
cl_git_fail(git__date_rfc2822_fmt(buf, sizeof(buf), &t));
35+
git_str_dispose(&buf);
3936
}
4037

0 commit comments

Comments
 (0)