forked from enlightenment/efl
eina: add API eina_strftime
Summary: Add new API eina_strftime API in eina_str @feature Test Plan: test case and example also updated Reviewers: tasn, cedric Reviewed By: cedric Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D3148 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
This commit is contained in:
parent
599141a96b
commit
0c1bb7d7b3
1
AUTHORS
1
AUTHORS
|
@ -53,6 +53,7 @@ ChunEon Park (Hermet) <hermet@hermet.pe.kr>
|
|||
Rajeev Ranjan (Rajeev) <rajeev.r@samsung.com> <rajeev.jnnce@gmail.com>
|
||||
Subodh Kumar <s7158.kumar@samsung.com>
|
||||
Michelle Legrand <legrand.michelle@outlook.com>
|
||||
Shilpa Singh <shilpa.singh@samsung.com> <shilpasingh.o@gmail.com>
|
||||
|
||||
Eet
|
||||
---
|
||||
|
|
|
@ -14,6 +14,9 @@ int main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
|
|||
char *part2 = "There are many copies. And they have a plan.";
|
||||
char **arr;
|
||||
int i;
|
||||
char *time_arr;
|
||||
time_t curr_time;
|
||||
struct tm *info;
|
||||
|
||||
eina_init();
|
||||
|
||||
|
@ -58,7 +61,13 @@ int main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
|
|||
eina_strlcat(str, "humans", 14);
|
||||
printf("%s\n", str);
|
||||
free(str);
|
||||
|
||||
|
||||
curr_time = time(NULL);
|
||||
info = localtime(&curr_time);
|
||||
time_arr = eina_strftime("%d/%m/%Y", info);
|
||||
printf("Today's Date: %s\n", time_arr);
|
||||
free(time_arr);
|
||||
|
||||
eina_shutdown();
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -374,6 +374,39 @@ eina_strlcat(char *dst, const char *src, size_t siz)
|
|||
return(dlen + (s - src)); /* count does not include NUL */
|
||||
}
|
||||
|
||||
EAPI char *
|
||||
eina_strftime(const char *format, const struct tm *tm)
|
||||
{
|
||||
const size_t flen = strlen(format);
|
||||
size_t buflen = 16; // An arbitrary starting size
|
||||
char *buf = NULL;
|
||||
|
||||
do {
|
||||
char *tmp;
|
||||
size_t len;
|
||||
|
||||
tmp = realloc(buf, buflen * sizeof(char));
|
||||
if (!tmp) goto on_error;
|
||||
buf = tmp;
|
||||
|
||||
len = strftime(buf, buflen, format, tm);
|
||||
// Check if we have the expected result and return it.
|
||||
if ((len > 0 && len < buflen) || (len == 0 && flen == 0))
|
||||
{
|
||||
tmp = realloc(buf, ((len + 1) * sizeof(char)));
|
||||
buf = tmp;
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* Possibly buf overflowed - try again with a bigger buffer */
|
||||
buflen <<= 1; // multiply buffer size by 2
|
||||
} while (buflen < 128 * flen);
|
||||
|
||||
on_error:
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
EAPI Eina_Bool
|
||||
eina_str_has_prefix(const char *str, const char *prefix)
|
||||
{
|
||||
|
|
|
@ -355,6 +355,32 @@ static inline size_t eina_strlen_bounded(const char *str, size_t maxlen) EINA_PU
|
|||
* @since 1.13
|
||||
*/
|
||||
EAPI unsigned char *eina_memdup(unsigned char *mem, size_t size, Eina_Bool terminate);
|
||||
|
||||
/**
|
||||
* @brief Create and update the buffer based on strftime output.
|
||||
*
|
||||
* @param tm Pointer to a tm structure needed by strftime.
|
||||
* @param format String containing format specifiers needed by strftime.
|
||||
* @return Updated buffer based on strftime output
|
||||
*
|
||||
* This will create a buffer of exact required size based on strftime output
|
||||
* once use is complete the buffer has to be freed using free.
|
||||
*
|
||||
* Example usage:
|
||||
* @code
|
||||
* time_t curr_time;
|
||||
* struct tm *info;
|
||||
* char *buf;
|
||||
* curr_time = time(NULL);
|
||||
* info = localtime(&curr_time);
|
||||
* buf = eina_strftime("%I:%M%p", info);
|
||||
* //after use
|
||||
* free(buf);
|
||||
* @endcode
|
||||
*
|
||||
* @since 1.16.0
|
||||
*/
|
||||
EAPI char *eina_strftime(const char *format, const struct tm *tm);
|
||||
#include "eina_inline_str.x"
|
||||
|
||||
/**
|
||||
|
|
|
@ -347,6 +347,24 @@ START_TEST(str_memdup)
|
|||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(str_strftime)
|
||||
{
|
||||
time_t curr_time;
|
||||
struct tm *info;
|
||||
char *buf;
|
||||
eina_init();
|
||||
|
||||
curr_time = time(NULL);
|
||||
info = localtime(&curr_time);
|
||||
|
||||
buf = eina_strftime("%I:%M%p", info);
|
||||
fail_if(buf == NULL);
|
||||
free(buf);
|
||||
|
||||
eina_shutdown();
|
||||
}
|
||||
END_TEST
|
||||
|
||||
#ifdef HAVE_ICONV
|
||||
START_TEST(str_convert)
|
||||
{
|
||||
|
@ -385,6 +403,7 @@ eina_test_str(TCase *tc)
|
|||
tcase_add_test(tc, str_lcat_lcpy);
|
||||
tcase_add_test(tc, str_join_len);
|
||||
tcase_add_test(tc, str_memdup);
|
||||
tcase_add_test(tc, str_strftime);
|
||||
#ifdef HAVE_ICONV
|
||||
tcase_add_test(tc, str_convert);
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue