1
0
Fork 0
mirror of git://git.gnupg.org/gnupg.git synced 2025-07-02 22:46:30 +02:00

common: New function scan_secondsstr.

* common/gettime.c (scan_secondsstr): New.

* common/t-gettime.c (test_scan_secondsstr):
(main): Call it.
This commit is contained in:
Werner Koch 2023-10-14 17:06:51 +02:00
parent c45a8b034c
commit f5947f7494
No known key found for this signature in database
GPG key ID: E3FDFF218E45B72B
4 changed files with 76 additions and 2 deletions

View file

@ -37,6 +37,7 @@
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
#include <stdint.h> /* We use uint64_t. */
#include "util.h"
#include "i18n.h"
@ -168,6 +169,28 @@ make_timestamp (void)
}
/* Specialized version of atoi which returns an u32 instead of an int
* and caps the result at 2^32-2. Leading white space is skipped,
* scanning stops at at the first non-convertable byte. Note that we
* do not cap at 2^32-1 because that value is often used as error
* return. */
u32
scan_secondsstr (const char *string)
{
uint64_t value = 0;
while (*string == ' ' || *string == '\t')
string++;
for (; *string >= '0' && *string <= '9'; string++)
{
value *= 10;
value += atoi_1 (string);
if (value >= (u32)(-1))
return (u32)(-1) - 1;
}
return (u32)value;
}
/****************
* Scan a date string and return a timestamp.