Commit 9e9e6ae1 by Carlos Martín Nieto Committed by Vicent Marti

Add API git_signature_new_now

Most tags will have a timestamp of whenever the code is running and
dealing with time and timezones is error-prone. Optimize for this case
by adding a function which causes the signature to be created with a
current timestamp.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
parent b5c00c6d
...@@ -50,6 +50,17 @@ GIT_BEGIN_DECL ...@@ -50,6 +50,17 @@ GIT_BEGIN_DECL
GIT_EXTERN(git_signature *) git_signature_new(const char *name, const char *email, git_time_t time, int offset); GIT_EXTERN(git_signature *) git_signature_new(const char *name, const char *email, git_time_t time, int offset);
/** /**
* Create a new action signature with a timestamp of 'now'. The
* signature must be freed manually or using git_signature_free
*
* @param name name of the person
* @param email email of the person
* @return the new sig, NULL on out of memory
*/
GIT_EXTERN(git_signature *) git_signature_new_now(const char *name, const char *email);
/**
* Create a copy of an existing signature. * Create a copy of an existing signature.
* *
* All internal strings are also duplicated. * All internal strings are also duplicated.
......
...@@ -65,6 +65,25 @@ git_signature *git_signature_dup(const git_signature *sig) ...@@ -65,6 +65,25 @@ git_signature *git_signature_dup(const git_signature *sig)
return git_signature_new(sig->name, sig->email, sig->when.time, sig->when.offset); return git_signature_new(sig->name, sig->email, sig->when.time, sig->when.offset);
} }
git_signature *git_signature_new_now(const char *name, const char *email)
{
time_t now;
struct tm utc_tm, local_tm;
int offset;
time(&now);
gmtime_r(&now, &utc_tm);
localtime_r(&now, &local_tm);
offset = mktime(&local_tm) - mktime(&utc_tm);
offset /= 60;
/* mktime takes care of setting tm_isdst correctly */
if (local_tm.tm_isdst)
offset += 60;
return git_signature_new(name, email, now, offset);
}
static int parse_timezone_offset(const char *buffer, long *offset_out) static int parse_timezone_offset(const char *buffer, long *offset_out)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment