mirror of
git://git.gnupg.org/gnupg.git
synced 2025-01-03 12:11:33 +01:00
common: New function elapsed_time_string.
* common/gettime.c (elapsed_time_string): New.
This commit is contained in:
parent
f90cfe6b66
commit
04e304278c
@ -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
|
||||
*/
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user