diff options
author | Werner Koch <wk@gnupg.org> | 2014-03-18 11:02:50 +0100 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2014-03-18 11:02:50 +0100 |
commit | 04e304278c9302831bc81e7fe9049c588ead029a (patch) | |
tree | 5f6d5ba7a51479fa4ec4f38db7f21fe2076ac3c6 | |
parent | gpg: Reject signatures made with MD5. (diff) | |
download | gnupg2-04e304278c9302831bc81e7fe9049c588ead029a.tar.xz gnupg2-04e304278c9302831bc81e7fe9049c588ead029a.zip |
common: New function elapsed_time_string.
* common/gettime.c (elapsed_time_string): New.
-rw-r--r-- | common/gettime.c | 39 | ||||
-rw-r--r-- | common/gettime.h | 2 |
2 files changed, 41 insertions, 0 deletions
diff --git a/common/gettime.c b/common/gettime.c index 6bdf3c51b..788743f9a 100644 --- a/common/gettime.c +++ b/common/gettime.c @@ -430,6 +430,45 @@ strtimevalue( u32 value ) } + +/* Return a malloced string with the time elapsed between NOW and + SINCE. May return NULL on error. */ +char * +elapsed_time_string (time_t since, time_t now) +{ + char *result; + double diff; + unsigned long value; + unsigned int days, hours, minutes, seconds; + + if (!now) + now = gnupg_get_time (); + + diff = difftime (now, since); + if (diff < 0) + return xtrystrdup ("time-warp"); + + seconds = (unsigned long)diff % 60; + value = (unsigned long)(diff / 60); + minutes = value % 60; + value /= 60; + hours = value % 24; + value /= 24; + days = value % 365; + + if (days) + result = xtryasprintf ("%ud%uh%um%us", days, hours, minutes, seconds); + else if (hours) + result = xtryasprintf ("%uh%um%us", hours, minutes, seconds); + else if (minutes) + result = xtryasprintf ("%um%us", minutes, seconds); + else + result = xtryasprintf ("%us", seconds); + + return result; +} + + /* * Note: this function returns GMT */ diff --git a/common/gettime.h b/common/gettime.h index bc914ad7f..736eb4168 100644 --- a/common/gettime.h +++ b/common/gettime.h @@ -33,6 +33,8 @@ void gnupg_get_isotime (gnupg_isotime_t timebuf); void gnupg_set_time (time_t newtime, int freeze); int gnupg_faked_time_p (void); u32 make_timestamp (void); +char *elapsed_time_string (time_t since, time_t now); + u32 scan_isodatestr (const char *string); int isotime_p (const char *string); int isotime_human_p (const char *string); |