From: Christian Grothoff Date: Tue, 19 Jun 2012 21:44:38 +0000 (+0000) Subject: adding API for parsing absolute time X-Git-Tag: initial-import-from-subversion-38251~12962 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=9e130ed3b1863b7bd45192ba5cb87e87ce075ebb;p=oweals%2Fgnunet.git adding API for parsing absolute time --- diff --git a/src/include/gnunet_strings_lib.h b/src/include/gnunet_strings_lib.h index d68ca5c78..a0d261d49 100644 --- a/src/include/gnunet_strings_lib.h +++ b/src/include/gnunet_strings_lib.h @@ -75,6 +75,19 @@ GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time, struct GNUNET_TIME_Relative *rtime); +/** + * Convert a given fancy human-readable time to our internal + * representation. + * + * @param fancy_time human readable string (i.e. %Y-%m-%d %H:%M:%S) + * @param atime set to the absolute time + * @return GNUNET_OK on success, GNUNET_SYSERR on error + */ +int +GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time, + struct GNUNET_TIME_Absolute *atime); + + /** * Convert a given filesize into a fancy human-readable format. * diff --git a/src/util/strings.c b/src/util/strings.c index cc44767d6..9c7f95e7c 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -308,6 +308,38 @@ GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time, return ret; } + +/** + * Convert a given fancy human-readable time to our internal + * representation. + * + * @param fancy_time human readable string (i.e. %Y-%m-%d %H:%M:%S) + * @param atime set to the absolute time + * @return GNUNET_OK on success, GNUNET_SYSERR on error + */ +int +GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time, + struct GNUNET_TIME_Absolute *atime) +{ + struct tm tv; + time_t t; + + if ( (NULL == strptime (fancy_time, "%c", &tv)) && + (NULL == strptime (fancy_time, "%Ec", &tv)) && + (NULL == strptime (fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) && + (NULL == strptime (fancy_time, "%Y-%m-%d %H:%M", &tv)) && + (NULL == strptime (fancy_time, "%x", &tv)) && + (NULL == strptime (fancy_time, "%Ex", &tv)) && + (NULL == strptime (fancy_time, "%Y-%m-%d", &tv)) && + (NULL == strptime (fancy_time, "%Y-%m", &tv)) && + (NULL == strptime (fancy_time, "%Y", &tv)) ) + return GNUNET_SYSERR; + t = mktime (&tv); + atime->abs_value = (uint64_t) ((uint64_t) t * 1000LL); + return GNUNET_OK; +} + + /** * Convert the len characters long character sequence * given in input that is in the given input charset @@ -389,10 +421,11 @@ GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset) return GNUNET_STRINGS_conv (input, len, charset, "UTF-8"); } + /** * Convert the len bytes-long UTF-8 string * given in input to the given charset. - + * * @return the converted string (0-terminated), * if conversion fails, a copy of the orignal * string is returned. @@ -403,6 +436,7 @@ GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset) return GNUNET_STRINGS_conv (input, len, "UTF-8", charset); } + /** * Convert the utf-8 input string to lowercase * Output needs to be allocated appropriately @@ -423,6 +457,7 @@ GNUNET_STRINGS_utf8_tolower(const char* input, char** output) free(tmp_in); } + /** * Convert the utf-8 input string to uppercase * Output needs to be allocated appropriately @@ -455,7 +490,6 @@ char * GNUNET_STRINGS_filename_expand (const char *fil) { char *buffer; - #ifndef MINGW size_t len; size_t n; @@ -982,7 +1016,6 @@ GNUNET_STRINGS_check_filename (const char *filename, } - /** * Tries to convert 'zt_addr' string to an IPv6 address. * The string is expected to have the format "[ABCD::01]:80". @@ -1114,6 +1147,7 @@ GNUNET_STRINGS_to_address_ip (const char *addr, return GNUNET_STRINGS_to_address_ipv4 (addr, addrlen, (struct sockaddr_in *) r_buf); } + /** * Makes a copy of argv that consists of a single memory chunk that can be * freed with a single call to GNUNET_free (); @@ -1139,6 +1173,7 @@ _make_continuous_arg_copy (int argc, char *const *argv) return (char *const *) new_argv; } + /** * Returns utf-8 encoded arguments. * Does nothing (returns a copy of argc and argv) on any platform