diff options
author | Carlos Martín Nieto <cmn@elego.de> | 2012-08-28 18:02:12 +0200 |
---|---|---|
committer | Carlos Martín Nieto <cmn@elego.de> | 2012-08-28 18:02:12 +0200 |
commit | d03d309b1082af7002e82c4b7028b23836d7e905 (patch) | |
tree | 669836392d35fe21f26d63020e5eab04eec09283 | |
parent | 0d5dce268d47c4ecfb3f8cdda3379cd606630105 (diff) | |
download | libgit2-d03d309b1082af7002e82c4b7028b23836d7e905.tar.gz |
signature: make the OS give us the offset for git_signature_now
There is a better and less fragile way to calculate time offsets. Let
the OS take care of dealing with DST and simply take the the offset
between the local time and UTC that it gives us.
-rw-r--r-- | src/signature.c | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/src/signature.c b/src/signature.c index 1f788356..84c3f499 100644 --- a/src/signature.c +++ b/src/signature.c @@ -125,24 +125,26 @@ int git_signature_now(git_signature **sig_out, const char *name, const char *ema { time_t now; time_t offset; - struct tm *utc_tm, *local_tm; + struct tm *utc_tm; git_signature *sig; - struct tm _utc, _local; + struct tm _utc; *sig_out = NULL; + /* + * Get the current time as seconds since the epoch and + * transform that into a tm struct containing the time at + * UTC. Give that to mktime which considers it a local time + * (tm_isdst = -1 asks it to take DST into account) and gives + * us that time as seconds since the epoch. The difference + * between its return value and 'now' is our offset to UTC. + */ time(&now); - utc_tm = p_gmtime_r(&now, &_utc); - local_tm = p_localtime_r(&now, &_local); - - offset = mktime(local_tm) - mktime(utc_tm); + utc_tm->tm_isdst = -1; + offset = difftime(now, mktime(utc_tm)); offset /= 60; - /* mktime takes care of setting tm_isdst correctly */ - if (local_tm->tm_isdst) - offset += 60; - if (git_signature_new(&sig, name, email, now, (int)offset) < 0) return -1; |